Git šŸŒ™
Chapters ā–¾ 2nd Edition

3.5 Git Branching - Remote Branches

Remote Branches

Remote branches are references (pointers) to the state of branches in your remote repositories. Theyā€™re local branches that you canā€™t move; theyā€™re moved automatically for you whenever you do any network communication. Remote branches act as bookmarks to remind you where the branches on your remote repositories were the last time you connected to them.

They take the form (remote)/(branch). For instance, if you wanted to see what the master branch on your origin remote looked like as of the last time you communicated with it, you would check the origin/master branch. If you were working on an issue with a partner and they pushed up an iss53 branch, you might have your own local iss53 branch; but the branch on the server would point to the commit at origin/iss53.

This may be a bit confusing, so letā€™s look at an example. Letā€™s say you have a Git server on your network at git.ourcompany.com. If you clone from this, Gitā€™s clone command automatically names it origin for you, pulls down all its data, creates a pointer to where its master branch is, and names it origin/master locally. Git also gives you your own local master branch starting at the same place as originā€™s master branch, so you have something to work from.

Nota
ā€œoriginā€ is not special

Just like the branch name ā€œmasterā€ does not have any special meaning in Git, neither does ā€œoriginā€. While ā€œmasterā€ is the default name for a starting branch when you run git init which is the only reason itā€™s widely used, ā€œoriginā€ is the default name for a remote when you run git clone. If you run git clone -o booyah instead, then you will have booyah/master as your default remote branch.

Server and local repositories after cloning.
Figura 30. Server and local repositories after cloning

If you do some work on your local master branch, and, in the meantime, someone else pushes to git.ourcompany.com and updates its master branch, then your histories move forward differently. Also, as long as you stay out of contact with your origin server, your origin/master pointer doesnā€™t move.

Local and remote work can diverge.
Figura 31. Local and remote work can diverge

To synchronize your work, you run a git fetch origin command. This command looks up which server ā€œoriginā€ is (in this case, itā€™s git.ourcompany.com), fetches any data from it that you donā€™t yet have, and updates your local database, moving your origin/master pointer to its new, more up-to-date position.

`git fetch` updates your remote references.
Figura 32. git fetch updates your remote references

To demonstrate having multiple remote servers and what remote branches for those remote projects look like, letā€™s assume you have another internal Git server that is used only for development by one of your sprint teams. This server is at git.team1.ourcompany.com. You can add it as a new remote reference to the project youā€™re currently working on by running the git remote add command as we covered in Git Basics. Name this remote teamone, which will be your shortname for that whole URL.

Adding another server as a remote.
Figura 33. Adding another server as a remote

Now, you can run git fetch teamone to fetch everything the remote teamone server has that you donā€™t have yet. Because that server has a subset of the data your origin server has right now, Git fetches no data but sets a remote branch called teamone/master to point to the commit that teamone has as its master branch.

Remote tracking branch for `teamone/master`.
Figura 34. Remote tracking branch for teamone/master

Pushing

When you want to share a branch with the world, you need to push it up to a remote that you have write access to. Your local branches arenā€™t automatically synchronized to the remotes you write to ā€“ you have to explicitly push the branches you want to share. That way, you can use private branches for work you donā€™t want to share, and push up only the topic branches you want to collaborate on.

If you have a branch named serverfix that you want to work on with others, you can push it up the same way you pushed your first branch. Run git push (remote) (branch):

$ git push origin serverfix
Counting objects: 24, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (15/15), done.
Writing objects: 100% (24/24), 1.91 KiB | 0 bytes/s, done.
Total 24 (delta 2), reused 0 (delta 0)
To https://github.com/schacon/simplegit
 * [new branch]      serverfix -> serverfix

This is a bit of a shortcut. Git automatically expands the serverfix branchname out to refs/heads/serverfix:refs/heads/serverfix, which means, ā€œTake my serverfix local branch and push it to update the remoteā€™s serverfix branch.ā€ Weā€™ll go over the refs/heads/ part in detail in [ch10-git-internals], but you can generally leave it off. You can also do git push origin serverfix:serverfix, which does the same thing ā€“ it says, ā€œTake my serverfix and make it the remoteā€™s serverfix.ā€ You can use this format to push a local branch into a remote branch that is named differently. If you didnā€™t want it to be called serverfix on the remote, you could instead run git push origin serverfix:awesomebranch to push your local serverfix branch to the awesomebranch branch on the remote project.

Nota
Donā€™t type your password every time

If youā€™re using an HTTPS URL to push over, the Git server will ask you for your username and password for authentication. By default it will prompt you on the terminal for this information so the server can tell if youā€™re allowed to push.

If you donā€™t want to type it every single time you push, you can set up a ā€œcredential cacheā€. The simplest is just to keep it in memory for a few mintues, which you can easily set up by running git config --global credential.helper cache.

For more information on the various credential caching options available, see Credential Storage.

The next time one of your collaborators fetches from the server, they will get a reference to where the serverā€™s version of serverfix is under the remote branch origin/serverfix:

$ git fetch origin
remote: Counting objects: 7, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0)
Unpacking objects: 100% (3/3), done.
From https://github.com/schacon/simplegit
 * [new branch]      serverfix    -> origin/serverfix

Itā€™s important to note that when you do a fetch that brings down new remote branches, you donā€™t automatically have local, editable copies of them. In other words, in this case, you donā€™t have a new serverfix branch ā€“ you only have an origin/serverfix pointer that you canā€™t modify.

To merge this work into your current working branch, you can run git merge origin/serverfix. If you want your own serverfix branch that you can work on, you can base it off your remote branch:

$ git checkout -b serverfix origin/serverfix
Branch serverfix set up to track remote branch serverfix from origin.
Switched to a new branch 'serverfix'

This gives you a local branch that you can work on that starts where origin/serverfix is.

Tracking Branches

Checking out a local branch from a remote branch automatically creates what is called a ā€œtracking branchā€ (or sometimes an ā€œupstream branchā€). Tracking branches are local branches that have a direct relationship to a remote branch. If youā€™re on a tracking branch and type git pull, Git automatically knows which server to fetch from and branch to merge into.

When you clone a repository, it generally automatically creates a master branch that tracks origin/master. However, you can set up other tracking branches if you wish ā€“ ones that track branches on other remotes, or donā€™t track the master branch. The simple case is the example you just saw, running git checkout -b [branch] [remotename]/[branch]. This is a common enough operation that git provides the --track shorthand:

$ git checkout --track origin/serverfix
Branch serverfix set up to track remote branch serverfix from origin.
Switched to a new branch 'serverfix'

To set up a local branch with a different name than the remote branch, you can easily use the first version with a different local branch name:

$ git checkout -b sf origin/serverfix
Branch sf set up to track remote branch serverfix from origin.
Switched to a new branch 'sf'

Now, your local branch sf will automatically pull from origin/serverfix.

If you already have a local branch and want to set it to a remote branch you just pulled down, or want to change the upstream branch youā€™re tracking, you can use the -u or --set-upstream-to option to git branch to explicitly set it at any time.

$ git branch -u origin/serverfix
Branch serverfix set up to track remote branch serverfix from origin.
Nota
Upstream shorthand

When you have an tracking branch set up, you can reference it with the @{upstream} or @{u} shorthand. So if youā€™re on the master branch and itā€™s tracking origin/master, you can say something like git merge @{u} instead of git merge origin/master if you wish.

If you want to see what tracking branches you have set up, you can use the -vv option to git branch. This will list out your local branches with more information including what each branch is tracking and if your local branch is ahead, behind or both.

$ git branch -vv
  iss53     7e424c3 [origin/iss53: ahead 2] forgot the brackets
  master    1ae2a45 [origin/master] deploying index fix
* serverfix f8674d9 [teamone/server-fix-good: ahead 3, behind 1] this should do it
  testing   5ea463a trying something new

So here we can see that our iss53 branch is tracking origin/iss53 and is ā€œaheadā€ by two, meaning that we have two commits locally that are not pushed to the server. We can also see that our master branch is tracking origin/master and is up to date. Next we can see that our serverfix branch is tracking the server-fix-good branch on our teamone server and is ahead by three and behind by one, meaning that there is one commit on the server we havenā€™t merged in yet and three commits locally that we havenā€™t pushed. Finally we can see that our testing branch is not tracking any remote branch.

Itā€™s important to note that these numbers are only since the last time you fetched from each server. This command does not reach out to the servers, itā€™s telling you about what it has cached from these servers locally. If you want totally up to date ahead and behind numbers, youā€™ll need to fetch from all your remotes right before running this. You could do that like this: $ git fetch --all; git branch -vv

Pulling

While the git fetch command will fetch down all the changes on the server that you donā€™t have yet, it will not modify your working directory at all. It will simply get the data for you and let you merge it yourself. However, there is a command called git pull which is essentially a git fetch immediately followed by a git merge in most cases. If you have a tracking branch set up as demonstrated in the last section, either by explicitly setting it or by having it created for you by the clone or checkout commands, git pull will look up what server and branch your current branch is tracking, fetch from that server and then try to merge in that remote branch.

Generally itā€™s better to simply use the fetch and merge commands explicitly as the magic of git pull can often be confusing.

Deleting Remote Branches

Suppose youā€™re done with a remote branch ā€“ say you and your collaborators are finished with a feature and have merged it into your remoteā€™s master branch (or whatever branch your stable codeline is in). You can delete a remote branch using the --delete option to git push. If you want to delete your serverfix branch from the server, you run the following:

$ git push origin --delete serverfix
To https://github.com/schacon/simplegit
 - [deleted]         serverfix

Basically all this does is remove the pointer from the server. The Git server will generally keep the data there for a while until a garbage collection runs, so if it was accidentally deleted, itā€™s often easy to recover.

scroll-to-top