What is the difference between ‘git push – set-usptream origin ‘ and ‘git push’

Difference Between git push --set-upstream origin and git push

The difference between git push --set-upstream origin <branch> and git push lies in how they handle the relationship between your local branch and the remote branch.

1. git push --set-upstream origin <branch>

  • Purpose: This command pushes your local branch to the remote repository and establishes a tracking relationship between the local branch and the remote branch.
  • Upstream Tracking: It sets the upstream branch for the current local branch. After this, future git push and git pull commands can be used without specifying the remote or branch name.
  • Example:
    git push --set-upstream origin feature-branch

    This pushes the feature-branch to the origin remote and sets it as the upstream branch for the current local branch.

  • Use case: If you’re pushing a branch to the remote for the first time or if the local branch does not yet have an upstream branch set, this command ensures that future pushes/pulls can be done with fewer commands.

2. git push

  • Purpose: This command pushes your current branch to the remote repository, but only if an upstream branch is already set.
  • No Upstream Tracking: If the current branch doesn’t have an upstream (tracking) branch set, Git will return an error and ask you to specify where to push.
  • Example:
    git push

    If you’re on feature-branch and an upstream branch is already set (e.g., from a previous --set-upstream command), it pushes to the appropriate remote and branch.

  • Use case: Use this when the upstream branch is already set, and you just want to push your changes without specifying details.

So, in short:

  • git push --set-upstream origin <branch>: Pushes the branch and sets up a connection between your local and remote branch for future operations.
  • git push: Pushes to the remote branch, but only if the upstream branch is already set.