What's the Difference Between "git push --set-upstream" and "git branch --set-upstream-to"?

git branch --set-upstream-to

By using this command you can make an existing local branch track a specified remote branch. For example:

git branch --set-upstream-to remote_name/branch_name

git push --set-upstream

By using this command you can setup tracking information for the current branch during the push. For example:

git push --set-upstream remote_name remote_branch_name

This may especially be useful in case where you created a new branch on your local that has no upstream branch setup. For example:

# create new local branch
git checkout -b new_local_branch

# stage all files in the current directory
git add .

# commit staged files
git commit

# fatal: The current branch new_local_branch has no upstream branch.
git push

In such a case, to push the current branch and set the remote as upstream, it would make sense to use git push command with the --set-upstream option.


This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.