+16 votes
1 view
in Devops and Agile by (23.1k points)

I'm apparently terrible at using git, despite my best attempts to understand it.

From kernel.org for git 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).

Here's branch.<name>.merge from git config:

branch.<name>.merge

Defines, together with branch.<name>.remote, the upstream branch for the given branch. It tells git fetch/git pull which branch to merge and can also affect git push (see push.default). When in branch <name>, it tells git fetch the default refspec to be marked for merging in FETCH_HEAD. The value is handled like the remote part of a refspec, and must match a ref which is fetched from the remote given by "branch.<name>.remote". The merge information is used by git pull (which at first calls git fetch) to lookup the default branch for merging. Without this option, git pull defaults to merge the first refspec fetched. Specify multiple values to get an octopus merge. If you wish to setup git pull so that it merges into <name> from another branch in the local repository, you can point branch.<name>.merge to the desired branch, and use the special setting . (a period) for branch.<name>.remote.

I successfully set up a remote repository with github, and I successfully pushed my first commit to it with:

git push -u origin master

Then, I unwittingly successfully pushed my second commit to my remote repository using:

git commit -m '[...]'

However, incorrectly thinking I would have to push again to origin from master, I ran:

# note: no -u

git push origin master

What did that do? It didn't seem to have any effect at all. Did I "undo" git push -u origin master?

1 Answer

+10 votes
by (58.7k points)

In more simple terms:

Technically, the -u flag adds a tracking reference to the upstream server you're pushing to.

What is necessary here is that this allows you to do a git pull without supplying any more arguments.

For example, once you are doing a git push -u origin master, you'll be able to later call git pull and git will understand that you indeed meant git pull origin master.

Otherwise, you'd have to type in the whole command.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...