git checkout a Remote Branch
One of the first Git commands you've learned was certainly "git checkout":
$ git checkout development
In its simplest form, it allows you to switch (and even create) local branches - something you need countless times in your day-to-day work.
However, git checkout's power is not limited to local branches: it can also be used to create a new local branch from a remote one.
The Git Cheat Sheet
No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!
Collaborating with Branches
Remember that branches are the main way of collaboration in Git. Let's say that one of your colleagues wants you to collaborate on (or review) a piece of code:
- She will push the corresponding branch to your common remote server.
- In order to see this newly published branch, you will have to perform a simple "git fetch" for the remote.
- Using the "git checkout" command, you can then create a local version of this branch - and start collaborating!
git checkout for Remote Branches
The syntax for making git checkout "remote-ready" is rather easy: simply add the "--track" flag and the remote branch's ref like in the following example:
$ git checkout --track origin/newsletter
Branch newsletter set up to track remote branch newsletter from origin.
Switched to a new branch 'newsletter'
Based on the remote branch "origin/newsletter", we now have a new local branch named "newsletter".
Note that, by default, Git uses the same name for the local branch. Being a good convention, there's rarely the need to change this.
Tip
Checking Out Remote Branches in Tower
In case you are using the Tower Git client, tracking a remote branch is as easy as drag and drop:
Committing and Exchanging Data
From here on, you can make changes and commit them to your new local branch like you're used to.
Remember to publish your changes to the remote by using "git push" from time to time. Only then will your colleague(s) be able to see and understand your changes.
Learn More
- Check out the chapter Inspecting Remote Data in our free online book
- More frequently asked questions about Git & version control