Home > Software engineering >  What is the difference between git push -u origin and git push --set-upstream origin
What is the difference between git push -u origin and git push --set-upstream origin

Time:02-26

I'm learning how to use git and I need to clone from a master github repo, branch it, make changes, and push the branch to the github repo - but I'm finding myself a bit confused on what the best way to do that would be. I know there are so many resources on git online but I can't find one that is this exact problem (although happy to use that if I'm wrong).

I've been testing on my own private github repo to see the differences and there doesn't seem to be any.

  1. clone from repo.
git clone [email protected]:organization/repo
  1. create a new branch
git checkout -b newbranch
  1. make changes, add, and commit
touch file
git add file
git commit -m "added file"
  1. when I try to push there is no upstream
git push 

I get an error saying "fatal: The current branch newbranch has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream origin newbranch

but I also see a lot of online tutorials using git push -u origin newbranch

Is there any functional difference between this? I'm sure there is some resource online about the difference, but git is a bit confusing to me and I though i'd ask this one direct question to see what people thought.

CodePudding user response:

When you set your upstream (or tracking) branches, you can simply execute pulls and pushes without having to specify the target branch using -u instead of --set-upstream. Git automatically knows that it has to fetch the new commits to the remote tracking branch.

Here's an article about it here

CodePudding user response:

They're exactly the same thing. Lots of options to unix commands have both a short and a long form. You can see this for yourself by running git help push.

       -u, --set-upstream
           For every branch that is up to date or successfully pushed, add
           upstream (tracking) reference, used by argument-less git-pull(1)
           and other commands. For more information, see branch.<name>.merge
           in git-config(1).
  • Related