Introduction
This post is part of a series about Git and GitHub.
In the previous post we looked at how to start working with Git as simply as possible. We did that by focusing on only Git, working with it only locally, and using only the most basic commands.
After installing and setting up Git, the commands we looked at were those for establishing a Git repository and completing the Version Control Cycle. Those are the most critical commands needed for using Git.
In this post we’re going to expand on that understanding to include the next most important feature of Git: branching and merging.
What is branching?
Branching is Git’s way of allowing you to work with more than one version of your files at the same time.
It’s like having two (or more) parallel worlds where your files start the same at the moment you branch them, but diverge as you make changes to one branch or the other. This allows you to experiment and try things out in one world while keeping the other unchanged. If you don’t like your changes, you can instantly discard them and go back to the other branch. If you do like the changes you made you can merge one branch into the other.
Furthermore, since you can have more than two branches, you can experiment with multiple types of changes to your files at the same time without those various changes overlapping or conflicting with one another.
This means you can always have your files in a known format in one context even while you experiment with making changes to them in another context.
For files that contain code, that means you can always have a stable, working version of your code even as you experiment with changes to that code in a separate version.
How to branch in Git
As a quick outline, the process for branching in Git involves just a few simple actions:
- Create a new branch.
- Switch to a branch (and make some changes).
- Merge one branch into another.
Create a branch
To use branching in Git you first create one (or more) new branches.
You can create as many branches as you need. Typically, you’ll create a branch for each type of change you wish to try.
Switch between branches
You switch between branches whenever you want to work in the context of a specific branch.
In Git, there is only one active branch for a repository at a time. The active branch is also called the HEAD branch or the checked out branch.
Whatever changes you make on one branch will not effect any other branch.
If you decide you don’t like the changes you can discard the branch and it won’t affect any other.
Merge branches
Often the point of branching is to try out changes to your files while keeping the original files intact.
Once you are satisfied with the changes you’ve made to your files, you can then merge them back into your main branch.
When to branch
If you are not at all familiar with branching it may not be clear when to create a branch or what type of branches to create.
The video Branching Strategies Explained by DevOps Toolkit provides an overview of several popular approaches for using branching in Git as well as some of the pros and cons of each approach. If you want more information on the topic you can follow-up with a web search for “Git branching strategies” to read more on the topic. But as Farcic points out in the video, many Git branching strategies are unnecessarily complicated for most situations.
When getting started with Git branching you should likely just use the Feature branch strategy, that is, creating a branch for each new feature or change you want to make.
You’ll develop more of an intuition for when to create a branch as you gain more experience, but as you get started, if you’re wondering if you should create a branch, the answer is most likely ‘yes’. Git is very efficient at creating, switching between, and merging branches, so there’s virtually no cost in using them.
Branching in action
Let’s now work through the steps of creating, using, and merging branches so you can see the process in action.
Creating a branch in Git
To create a new branch in Git, first navigate in the terminal to the directory containing the Git repository in which you want to create a new branch.
To start, you can confirm the presence of a Git repository by running the command:
git status
If you are in a directory with a Git repository you should see output like the following (though it might differ in some details):
On branch master
nothing to commit, working tree clean
One thing to note is that the first line will tell you the currently active branch. And there will always be some branch, even if you never explicitly created one, because Git creates a default branch when it initializes the repository.
You can also see a list of branches for the repository using the following command:
git branch
If you haven’t created any additional branches, you should see output like the following:
* master
To create a new branch use the git branch
command followed by your desired branch name. For this example we’ll use development:
git branch development
You likely won’t see any output, so run the git branch
command again to see the change. You’ll see something like the following:
development
* master
Now, there are two branches, but notice that there is an asterisk (*) next to the master branch. This indicates that the master branch is still the active (or “HEAD”) branch. Though we created the new development branch, we’re still on the master branch.
Switching to a branch in Git
To switch to another branch use the git checkout
command followed by the name of the branch you want to switch to.
git checkout development
You should see the message:
Switched to branch 'development'
Run the git branch
command again to see what happened.
* development
master
Now, development is the active (or “HEAD”) branch.
You can also see this by running the git status
command again, and you’ll see output like:
On branch development
nothing to commit, working tree clean
That is all that’s required to create and switch between branches in Git.
Making changes
Now, let’s create a new file in the development branch.
touch test.js
Run the ls
command to see the files in the directory:
ls -1
You should see output like:
index.html
style.css
test.js
Now, let’s add and commit our changes.
git add test.js
git commit -m "Add test feature"
Next, let’s switch back to the master branch:
git checkout master
And run the ls
command again:
ls -1
Now the output is:
index.html
style.css
As expected, the new test.js file isn’t there.
Let’s switch back to the development branch again:
git checkout development
And again run the ls
command:
ls -1
Now the output is:
index.html
style.css
test.js
And once again, the test.js file is there.
This is what branching allows you to do. You can have different working sets of files that are isolated from each other in separate contexts.
Additional resources
If you’d like to review these steps, the Creating & Checking Out Branches video by Tower illustrates this process.
Merging a branch in Git
Let’s say we like the changes we made on the development branch and we want to include them in the master branch. We can merge them into the master branch from the development branch.
To get started, we need to confirm that the master branch is the active branch, because changes are always merged into the active branch.
Run the git checkout
command to confirm the correct branch is active:
git checkout master
You’ll either see the message:
Already on 'master'
Or:
Switched to branch 'master'
Either way, the master branch is now the active branch.
Now you can run the git merge
command with the name of the branch you want to merge into the active branch:
git merge development
You should see output like:
Updating 00375a8..3751218
Fast-forward
test.js | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.js
That’s it - the branches are merged.
Once more run the ls
command:
ls -1
Now the master branch has the test.js file as well:
index.html
style.css
test.js
NOTE: This is a simple example that doesn’t include a merge conflict. Nevertheless, this is reflective of the typical process that you’re likely to experience, especially if you’re working with Git locally. If you continue working with Git, especially with others, you’ll eventually encounter merge conflicts. You can learn how to handle that as you advance.
Additional resources
If you’d like to review these steps, the Merging Branches video by Tower illustrates this process.
And to see the process for handling merge conflicts, take a look at the Dealing with Merge Conflicts video by Tower.
Finally, the entire “Git Branching - Basic Branching and Merging” section from Pro Git by Scott Chacon and Ben Straub provides another helpful walkthrough with additional details.
Delete a branch
After creating, working with, and merging branches, you’re likely to eventually want to delete a branch. Like many things in Git, it’s pretty straightforward.
To delete the development branch:
git branch --delete development
More useful commands for branching and merging
While you may not need them immediately, there are some additional commands that can be helpful when branching and merging with Git.
Abort an in-progress merge
If you start to merge a branch and decide you don’t want to complete it you can use the following to revert the merge:
git merge --abort
Stash your current work
If you want to save your current work without committing it you can use the stash
command:
git stash
Additional resources
Watch the videos Git Stash In 5 Minutes by Colt Steele and Using the Stash by Tower and read the “Git Tools - Stashing and Cleaning” section from Pro Git to learn more about using the stash command.
Rebase your commit history
If you want to apply all the file changes of one branch to another without also applying the commit history of that branch you can use the rebase
command:
git rebase new-feature-branch
This is a command where it’s important to know what you’re doing, especially if you are working with remote repositories. So, understand the impact of this command before using it.
Additional resources
Watch the video Rebase as an Alternative to Merge by Tower and read the “Git Branching - Rebasing” section from Pro Git to learn more about using the rebase command.
Next steps in learning Git
As mentioned in the previous post, there is much more to learn in Git. We’ve touched on only the introductory features of this powerful tool.
To find a curated list of many more resources for learning about Git you can review our Git and GitHub Topic post.
To move to the next topic in this series take a look at Getting Started with Git Remote Repositories (Like GitHub).