Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How long does 'Git push -u' remember the parameters for?

I just got started with a tutorial on Git.

There, they mentioned a command

git push -u origin master

where the changes made on local branch master are pushed to origin repository(on Github). And the -u tells git to remember the parameters so that next time we can just write git push

Can someone tell me whether git remembers the parameters only for the very next time we use git push, or every time henceforth till a command to tell git to forget the parameters is written? Also, is any there such command?

Thanks in advance!

like image 807
GothamCityRises Avatar asked Dec 26 '22 01:12

GothamCityRises


2 Answers

In the command

git push -u origin master

The -u flag means that your local branch will become a tracking branch. That is, a branch that tracks a remote branch, so that future git pull will know which branch to merge from and git push will be directed to the correct remote branch.

Technically, the tracking adds the following information about the master branch to your .git/config file:

[branch "master"]
    remote = origin
    merge = refs/heads/master

and it creates a file here .git/refs/remotes/origin/master, representing the remote branch.

These settings are local to the current repository, so they will not apply to other repositories.

The changes in .git/config are permanent (until you explicitly change them), so the effects of git push -u are permanent.

like image 128
Klas Mellbourn Avatar answered Jan 05 '23 17:01

Klas Mellbourn


git push -u tells git to track the remote branch locally (an 'upstream tracking reference'), so git push whilst on the local branch will then always push to the remote branch specified in the initial git push -u. This will persist on the branch (or master in your case) until the next push -u you do (which will cause it to track another remote branch).

It's also used so that other git commands know where to retrieve data from e.g. git pull uses it to pull changes made to the remote being tracked to the local repo.

like image 33
Utopia Avatar answered Jan 05 '23 16:01

Utopia