Skip to content

Instantly share code, notes, and snippets.

@mtilson
Last active April 9, 2020 03:27
Show Gist options
  • Save mtilson/a0d5f38ac7b2f60bf5178f6c69b2e999 to your computer and use it in GitHub Desktop.
Save mtilson/a0d5f38ac7b2f60bf5178f6c69b2e999 to your computer and use it in GitHub Desktop.
what are useful operations with Git remote branches [git]

All the details are based on Git Remote Branches unless otherwise stated


Sharing a Branch with the World

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

Tracking Branches

  • 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 tracks origin/master the following is equivalent
        • git merge origin/master
        • git merge @{u}
  • 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 option
    • git branch -u <remote>/<r-branch> <l-branch>
  • Tracking branches are shown with help of the -vv option to git branch command
    • master 1ae2a45 [origin/master: ahead 2] add feature #3
      • local master branch tracks origin/master 'upstream branch'
      • local master branch is 'ahead' by two - we have two commits locally that are not pushed to the server
    • testing 5ea463a Try something new
      • local testing branch is not tracking branch
    • fix f8674d9 [gitserver/fix: ahead 3, behind 1] some changes in fix
      • local fix branch tracks gitserver/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
  • 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 above git branch -vv command output
[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

Pulling from Remote Branches

  • 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 yourself
  • git pull command is essentially a git fetch immediately followed by a git 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 and git merge commands explicitly as the magic of git pull can often be confusing

Deleting Remote Branches

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 branch X on the local server
  • The actual remote branch X: the branch X on the server <remote>
  • The local remote-tracking branch <remote>/X: the branch X on the local server that tracks the branch X on the server <remote>
  • To delete actual remote branch X and also local remote-tracking branch origin/X with a single command you have to use git 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 the git 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment