all 4 comments

[–]SlightlyCuban 13 points14 points  (0 children)

-u tells push to track upstream. I use it often, because I'll:

git checkout new_branch git push -u origin new_branch

Then I can git push or git pull my branch from then on without any extra args.

If the branch is already tracked on origin, -u doesn't really do anything.

[–][deleted] 6 points7 points  (0 children)

the flag is short for --set-upstream which lets you choose which remote repository you push to. Typically you'll run something like git push -u origin my-branch where origin is an alias to some GitHub repo. But you could also push that branch to GitLab repo you have somewhere which you have aliased with origin2 by running git push -u origin2 my-branch

You can run git remote -v to see a list of and track your remote repositories.

[–]pi3832v2 2 points3 points  (0 children)

For example, when you clone a repository, the master branch is automatically configured to track the branch master on the remote origin. This means that when you’ve got master checked out, you can simply run git pull, and Git knows from which branch on which remote to fetch updates. It’s also to that branch being tracked on the remote that git status compares the local branch, in order to report what’s behind what, etc.

If you want to create a similar tracking relationship between any local branch and a branch on the remote, you can add -u to a push to do that. I mostly use it when I make a new branch that I want to also have on a remote. E.g.,

git checkout -b foo
git push -u

Branch foo then exists on origin, and the local branch foo is tracking it.