12 Branching (git branch)

Key terms/command

  • Branch: independent line of development typically used to develop new features without disturbing the rest of the repository. The default branch is called the “Master” branch.

  • git branch: handles the creation, renaming and deletion of branches. Note that git checkout is used to switch between branches.

Want to create or edit a new script or function but not ready to make it live (available to everyone) just yet?

Create a new branch!



12.1 Check what branch you’re on

If you’ve never changed a branch. You’re likely using the master or main branch.

It’s always a good idea to check what branch you’re using:

git branch
## * master

The one marked with the * is the name of branch that you’re currently on, also known as the active branch.


12.2 Create new branch

To create a new branch, we can enter:

git branch <branch_name>

For example, if we want to make a test a new function on a new branch, we can create the new branch by entering:

git branch cool_new_function

To make sure the branch exists, we can enter:

git branch
##   cool_new_function
## * master


12.3 Switch between branches (git checkout)

To switch to the new branch, use git checkout <branch_name>

Example:

git checkout cool_new_function
## Switched to branch 'cool_new_function'

Let’s check that we actually switched.

git branch
## * cool_new_function
##   master

Notice that the * is now on cool_new_function.

Note: You can also create and switch to a branch in one command using:

git checkout -b <branch_name>

Now that you’re in your new branch, you can make, edit, and commit script(s) as you want without affecting anything on the master branch.

touch cool_new_function.sh
git add cool_new_function.sh
git commit -m "added cool new function"
ls
## [cool_new_function ba877bb] added cool new function
##  1 file changed, 0 insertions(+), 0 deletions(-)
##  create mode 100644 cool_new_function.sh
## cool_new_function.sh
## foo.txt
## ignore_this.txt


12.4 Merge branch

Ready to make the modification available to everyone? Let’s merge the change back to the master branch.

First, we have to switch back to the master branch git checkout master and then merge the commit git merge <branch_name>

git checkout master
ls
## Switched to branch 'master'
## foo.txt
## ignore_this.txt

Notice that there is no cool_new_function.sh file in the master branch.

git merge cool_new_function
ls
## Updating 90f22fd..ba877bb
## Fast-forward
##  cool_new_function.sh | 0
##  1 file changed, 0 insertions(+), 0 deletions(-)
##  create mode 100644 cool_new_function.sh
## cool_new_function.sh
## foo.txt
## ignore_this.txt

The cool_new_function.sh file is now available on the master branch after merging.


12.5 Delete a branch

Done using a branch or created one by accident?

Delete the branch by entering:

git branch -d <branch_name>
git branch -d cool_new_function
## Deleted branch cool_new_function (was ba877bb).
git branch
## * master

BONUS (Advanced x 2 topic) - detached HEAD

Remember the previous chapter mentioned a concept about ‘detached HEAD’? Now you have tried branching, you could understand a bit more how detached HEAD can occur, and how branching can help preventing commit loss.

12.5.1 Purposely enter a detached HEAD and create a commit

git checkout HEAD~2
## Note: checking out 'HEAD~2'.
##
## You are in 'detached HEAD' state. You can look around, make experimental
## changes and commit them, and you can discard any commits you make in this
## state without impacting any branches by performing another checkout.
##
## If you want to create a new branch to retain commits you create, you may
## do so (now or later) by using -b with the checkout command again. Example:
##
##   git checkout -b <new-branch-name>
##
## HEAD is now at [commit identifier] [commit message]
touch loss.txt
git add loss.txt
git commit -m "Adding loss.txt while in detached HEAD"
## [master 196a84c] Adding loss.txt while in detached HEAD
##  1 file changed, 0 insertions(+), 0 deletions(-)
##  create mode 100644 loss.txt

12.5.2 Return to master branch

git checkout master
## Warning: you are leaving 1 commit behind, not connected to
## any of your branches:
##
##  2d8a023 Adding loss.txt while in detached HEAD
##
## If you want to keep it by creating a new branch, this may be a good time
## to do so with:
##
##  git branch <new-branch-name> 2d8a023
##
## Switched to branch 'master'

The output would warn you that you are “leaving 1 commit behind”.

12.5.3 You are now back in the ‘master’ branch, and loss.txt is gone (check with ls)

Luckily, the output message actually tells you how to recover those changes (for now, before Git eventually deletes it).

git branch recover_loss_branch <commit identifier>

Now if you switch to that branch you just created (use git checkout), you will see loss.txt.


You have started using branches in Git!