Functionally, they both result in the same thing. When I try both styles, they result in the same configuration.
$ git push -u origin 1st:master
Counting objects: 3, done.
Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To /Users/schwern/tmp/test-repo
71f7d8f..6729320 1st -> master
Branch 1st set up to track remote branch master from origin.
$ git branch -u origin/master
Branch 2nd set up to track remote branch master from origin.
$ cat .git/config
...
[remote "origin"]
url = /Users/schwern/tmp/test-repo
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[branch "2nd"]
remote = origin
merge = refs/heads/master
[branch "1st"]
remote = origin
merge = refs/heads/master
There is no "direct" tracking of remote branches. Git remote branches are regular branches in your repository that happen to be updated from a remote repository. "Tracking" tells one branch to push and pull from another.
git push
sends changes to the remote. git branch -u
tells a branch what to track. git push -u
is basically git push
plus git branch -u
.
From an interface perspective, git branch -u remote/branch
is a bit safer. It's more explicit, less magical. Case in point, when I was setting up this example I tried this...
$ git branch
* 1st
2nd
master
$ git push -u origin master
Branch master set up to track remote branch master from origin.
Everything up-to-date
1st was ahead of master and origin/master and should have pushed changes, but it didn't. It took me awhile to realize that git push -u origin master
was pushing master and not 1st. Whoa, it wasn't pushing the current branch! This is because the master
part refers to the remote destination branch, and Git guessed the local source branch was also master, not my current branch. This behavior will change depending on what version of Git you have and how you have it configured.
This is why I did the more explict git push -u remote local_source_branch:remote_destination_branch
syntax. git push -u origin 1st:master
.
In general, recent versions of Git should take care of this for you IF you branch directly from a remote. This is controlled by branch.autosetupmerge
which defaults to true
.
$ git co -b 3rd origin/master
Branch 3rd set up to track remote branch master from origin.
Switched to a new branch '3rd'
IMO that is the safest way to set up remote tracking branch.
PS You can play around with remotes without needing a new server. A remote doesn't have to be a URL, it can be a directory. It just has to be a bare repository (ie. no checked out files). git init --bare /path/to/somedir
and then git clone /path/to/somedir
.