Git Checkout - Working with branches

Git

In this tutorial we will learn to checkout branches in Git.

In the previous tutorial Git Branch we learned how to create new branches.

Lets say we have a project and we want to list all the branches in that project. So we will use the git branch command.

$ git branch
  dev
* master

From the above output we can tell that there are two branches in the project and we are currently on the master branch.

Checkout branch

To switch (checkout) to a branch we use the git checkout [branch-name] command.

In the following example we are checking out the dev branch.

$ git checkout dev
Switched to branch 'dev'

After we checkout a branch all the changes saved (committed) using the git add and git commit command are saved in that branch.

What happens when we checkout a branch?

When we checkout a branch the files in the working directory are updated to match the version stored in that branch.

So, if we add a new file in the lets say, dev branch then when we switch back to the master branch it will not be there.

Lets understand this with an example. In the following output we are listing the files in the master branch.

$ git branch
  dev
* master

$ ls -la
total 0
drwxr-xr-x   5 yusufshakeel  staff  160 Oct  5 14:15 .
drwxr-xr-x  30 yusufshakeel  staff  960 Oct  2 22:48 ..
drwxr-xr-x  15 yusufshakeel  staff  480 Oct  5 19:39 .git
-rw-r--r--   1 yusufshakeel  staff    0 Sep 18 21:10 index.php
drwxr-xr-x   3 yusufshakeel  staff   96 Sep 12 19:46 js

Now lets switch to the dev branch and create a new sample.php file.

$ git checkout dev
Switched to branch 'dev'

$ touch sample.php

Now, we will list the files and check the status of the repository.

$ ls -la
total 0
drwxr-xr-x   6 yusufshakeel  staff  192 Oct  5 19:41 .
drwxr-xr-x  30 yusufshakeel  staff  960 Oct  2 22:48 ..
drwxr-xr-x  15 yusufshakeel  staff  480 Oct  5 19:41 .git
-rw-r--r--   1 yusufshakeel  staff    0 Sep 18 21:10 index.php
drwxr-xr-x   3 yusufshakeel  staff   96 Sep 12 19:46 js
-rw-r--r--   1 yusufshakeel  staff    0 Oct  5 19:41 sample.php

$ git status
On branch dev
Untracked files:
  (use "git add ..." to include in what will be committed)

	sample.php

nothing added to commit but untracked files present (use "git add" to track)

In the above output we can see that we have an untracked file in the dev branch. So, we will stage (git add) and commit (git commit) the file.

$ git add .

$ git commit -m 'initial commit'
[dev 510d006] initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 sample.php

 $ git status
On branch dev
nothing to commit, working tree clean

$ ls -la
total 0
drwxr-xr-x   6 yusufshakeel  staff  192 Oct  5 19:41 .
drwxr-xr-x  30 yusufshakeel  staff  960 Oct  2 22:48 ..
drwxr-xr-x  15 yusufshakeel  staff  480 Oct  5  2017 .git
-rw-r--r--   1 yusufshakeel  staff    0 Sep 18 21:10 index.php
drwxr-xr-x   3 yusufshakeel  staff   96 Sep 12 19:46 js
-rw-r--r--   1 yusufshakeel  staff    0 Oct  5 19:41 sample.php

Now if we checkout the master branch we will see that the sample.php is not there in the master branch.

$ git checkout master
Switched to branch 'master'

$ ls -la
total 0
drwxr-xr-x   5 yusufshakeel  staff  160 Oct  5 19:47 .
drwxr-xr-x  30 yusufshakeel  staff  960 Oct  2 22:48 ..
drwxr-xr-x  15 yusufshakeel  staff  480 Oct  5 19:47 .git
-rw-r--r--   1 yusufshakeel  staff    0 Sep 18 21:10 index.php
drwxr-xr-x   3 yusufshakeel  staff   96 Sep 12 19:46 js

We don't see the sample.php file because it was committed in the dev branch.

Creating and checking out branch

We can create and checkout a new branch by using the git checkout -b [branch-name] command.

This will create a new branch off of a current branch we are on and then checkout the new branch.

Example:

If we are on master branch and then create a new branch sample using the git checkout -b sample command. The sample branch will have a pointer pointing at the last commit in the master branch.

We can also create a new branch off of an existing branch and then checkout using the git checkout -b [new-branch] [existing-branch] command

Example:

If we are on master branch and we want to create a new sample branch but this time we want the pointer for the sample branch to point to an existing dev branch. So, we will use the git checkout -b sample dev command.