Friday, 28 December 2018

GIT: 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.

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:
  1. She will push the corresponding branch to your common remote server.
  2. In order to see this newly published branch, you will have to perform a simple "git fetch" for the remote.
  3. 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.

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.

0 comments:

Post a Comment