You have seen this command in many tutorials. Maybe you use it everyday, or every time you push:
$ git push -u origin master
This pushes changes in your local repository up to the master
branch of the remote repository origin
.
But do you need to do this every time you push?
The -u
in this command is a short-hand for --set-upstream
. Using this flag you are basically telling git to automatically link your local master to the remote master. Therefore you only need to do this once. After that you can just write git pull
and git push
on master.
Just remember to do this the first time you create a branch locally and are ready to push it to the remote:
# Create a branch and switch to it
$ git checkout -b bugFix
# Work on bugFix and then commit the changes
$ git add .
$ git commit -m "Put some print statements. Found the bug."
# Since this is the first time you push to origin
$ git push -u origin bugFix
This creates a branch called bugFix
on your remote and sets your local bugFix
branch to track it. So from now on, when you switch to that branch locally, you can just pull and push without any further argument or flag.
Pro tip:
If you want to see how your local branches are connected to remote branches, take a look at your repository's config file. The -u
flag sets the merge property of the branch:
./my_repo$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://github.com/<your-user-name>/my_repo.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[branch "bugFix"]
remote = origin
merge = refs/heads/bugFix
Top comments (0)