All the details are based on Git Remote Branches unless otherwise stated
Create and push a branch on one workplace (WP), and fetch and check out the branch on another workplace
- Create and push on workplace 1
WP1> git branch fix
WP1> git checkout fix
WP1> # some changes
WP1> git commit -m 'some changes in fix'
WP1> git push origin fix
- Fetch and check out on workplace 2
WP2> git fetch origin # updates your remote-tracking branches (of the form <remote>/<branch>)
...
* [new branch] fix -> origin/fix'
WP2> git merge origin/fix # option 1: merge remote-tracking branch into your current working branch
WP2> git checkout -b fix origin/fix # option 2: base your own 'fix' branch off your remote-tracking branch
- Checking out a local branch from a remote-tracking branch (like the above
git checkout -b <branch> <remote>/<branch>
) automatically creates what is called a 'tracking branch' (and the branch it tracks is called an 'upstream branch')- Tracking branches are local branches that have a direct relationship to a remote branches. If you’re on a tracking branch and type
git pull
, Git automatically knows which server to fetch from and which branch to merge in - Upstream branch can be referenced with upstream shorthand:
@{upstream}
or@{u}
- If you’re on the
master
branch and it tracksorigin/master
the following is equivalentgit merge origin/master
git merge @{u}
- If you’re on the
- Tracking branches are local branches that have a direct relationship to a remote branches. If you’re on a tracking branch and type
- Here are shortcuts:
git checkout -b <branch> <remote>/<branch>
git checkout --track <remote>/<branch>
- the same as above; local branch will be<branch>
git checkout <branch>
- the same as above if (a)<branch>
doesn’t exist and (b)<branch>
exactly matches a name on only one remote; local branch will be<branch>
,<remote>
will be the matched remote
- If you already have a local branch
<l-branch>
and want to set it to a remote branch<remote>/<r-branch>
(e.g. if you just pulled down a remote branch but didn't make it tracking branch, or want to change the upstream branch you’re tracking, you can use the-u
or--set-upstream-to
optiongit branch -u <remote>/<r-branch> <l-branch>
- Tracking branches are shown with help of the
-vv
option togit branch
commandmaster 1ae2a45 [origin/master: ahead 2] add feature #3
- local
master
branch tracksorigin/master
'upstream branch' - local
master
branch is 'ahead' by two - we have two commits locally that are not pushed to the server
- local
testing 5ea463a Try something new
- local
testing
branch is not tracking branch
- local
fix f8674d9 [gitserver/fix: ahead 3, behind 1] some changes in fix
- local
fix
branch tracksgitserver/fix
'upstream branch' - local
fix
branch is 'ahead' by three - we have three commits locally that are not pushed to the server - local
fix
branch is 'behind' by one - there is one commit on the server we haven’t merged in yet
- local
- The above 'ahead', and 'behind' numbers are only since the last time you fetched from each server
- Tracking branches configuration details are stored in
.git/config
- The following part of
.git/config
correspond to the abovegit branch -vv
command output
- The following part of
[remote "origin"]
url = https://github.com/txxxx-xx/gxx-xxxx.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[remote "gitserver"]
url = https://gitserver.io/txxxx-xx/gxx-xxxx.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "fix"]
remote = gitserver
merge = refs/heads/fix
git fetch
command fetchs all the changes on the server that you don’t have yet, but it does not modify your working directory - you have to merge the fetched changes yourselfgit pull
command is essentially agit fetch
immediately followed by agit merge
in most cases- If you have a tracking branch set up as demonstrated above
git pull
will look up what server and branch your current branch is tracking, fetch from that server and then try to merge that remote branch in - It’s better to simply use the
git fetch
andgit merge
commands explicitly as the magic ofgit pull
can often be confusing
Delete remote branch as you finished with it and have merged it into your remote’s
master
branch
git push <remote> --delete <branch>
Deleting Branches of One of 3 Different Types - based on StackOverflow's - How to Delete Remote Branch
There are 3 different types of branches
- The local branch
X
: the branchX
on the local server- The actual remote branch
X
: the branchX
on the server<remote>
- The local remote-tracking branch
<remote>/X
: the branchX
on the local server that tracks the branchX
on the server<remote>
- To delete actual remote branch
X
and also local remote-tracking branchorigin/X
with a single command you have to usegit push
. Here are different options of such a command (the command does communicate with remote server)git push origin --delete X
git push origin -d X # short version of the above command
git push origin :X
- To detele all obsolete local remote-tracking branches for any remote branches that no longer exist on the
origin
remote (this can happen if you deleted an actual remote branch directly through GitHub's web interface, for example), the--prune
flag of thegit fetch
should be used (the command does communicate with remote server)git fetch origin --prune
git fetch origin -p # short version of the above command
- To detele local remote-tracking branches
origin/X
manually, avoiding making the extra network operation, the following commands can be used (the command does not communicate with remote server)git branch --delete --remotes origin/X
git branch -dr origin/X # short version of the above command
- To delete local branch
X
the following commands can be used (the command does not communicate with remote server)git branch --delete X
git branch -d X # short version of the above command
git branch -D X # force-delete un-merged branch X