Git Branching and Merging Made Simple

Version Control ~ 8 minutes

# Why branches are useful

Hello again! In this tutorial, we will learn branching and merging. These are two of the most powerful ideas in Git. Do not worry. We will keep it very simple.

Imagine your project is a main road. You want to try a new idea, but you do not want to block the road. So you build a small side road. You can test your idea there. If it works, you join the side road back to the main road. If it fails, you just throw the side road away. In Git, the side road is a branch, and joining it back is a merge.

Before you start

  • Git must be installed
  • You should have a repository with at least one commit
  • If you do not, please read our tutorial Git Basics: Your First Repository first
List your branches
Code Copied to Clipboard

# Read the branch list

The command above lists all branches in your repository. You will see one name with a star, for example * main. The star shows the branch you are on now. Older repositories may call this branch master. It is the same idea, only the name is different.

The main branch should hold your stable, working project. We do not want to break it. So for each new feature or fix, we will make a new branch. Let us make one now. We will name it feature-greeting. Good branch names are short and describe the work.

Create and switch to a new branch
Code Copied to Clipboard

# What just happened

That one command did two jobs. The word switch moves you to another branch. The option -c means create, so Git first made the branch, and then moved you onto it. Git printed a message such as Switched to a new branch.

Now you are on feature-greeting. Any commit you make here will not touch main. Please create a file named greeting.txt in your project folder, and write the lines shown in the next block.

greeting.txt
Code Copied to Clipboard

Branches are cheap

In Git, a branch is only a small pointer to a commit. It does not copy your whole project. That is why making a branch is very fast and takes almost no space. So please do not be shy. Make a new branch for every idea, bug fix or experiment. Professional teams often have many branches at the same time.

# Stage the new file

Let us save our work on this branch. As you learned before, saving takes two steps. First we stage the file with git add. Then we commit it. Please run the staging command now. If you forget the file name, you can run git status to see it. Git tells you which files are new and which are waiting to be saved.

Stage greeting.txt
Code Copied to Clipboard

# Commit your work

Now we close the box with a commit. Remember to write a short, clear message. A good message says what you did, for example "Add greeting file". This commit lives only on the branch feature-greeting. The main branch does not know about it yet.

Commit on the feature branch
Code Copied to Clipboard

# Go back to main

Now let us see the magic of branches. We will return to the main branch. Please run the next command, and then look at your project folder.

You will notice that greeting.txt is gone! Do not worry. It is not deleted. It is safe inside the feature-greeting branch. Git changes the files in your folder to match the branch you are on. When you switch back later, the file will return.

Switch to main
Code Copied to Clipboard
Always merge into the branch you are standing on

A merge brings changes into your current branch. So first, switch to the branch that should receive the changes. In our case, we want the new feature inside main. That is why we switched to main first.

Merge rule to remember

  1. Switch to the receiving branch, usually main
  2. Run git merge with the name of the feature branch
  3. Check the result with git log

If you merge from the wrong branch, you will put changes in the wrong place. Please check your branch with git branch before every merge.

# Merge the feature

We are on main now, so we are ready to merge. Please type git merge followed by the name of the branch to join. Git will take the commits from feature-greeting and add them to main.

Because main had no new commits of its own, Git does a simple fast-forward. It just moves the main pointer forward. No extra merge commit is needed. After this step, greeting.txt appears in your folder again, this time on main.

Merge the feature branch
Code Copied to Clipboard

# See the history as a graph

Let us check our work. The next command shows the history as a small picture made of text. The option --graph draws lines for branches. The option --oneline keeps each commit on one line.

You should see your new commit at the top, with labels such as HEAD -> main and feature-greeting. HEAD is Git's name for the place where you are now. When a project grows, this graph becomes very helpful for understanding what happened.

Show the graph
Code Copied to Clipboard

# Clean up the old branch

The feature is now part of main, so we do not need the side road anymore. Keeping old branches makes the list long and confusing. Let us delete it. The option -d is the safe way. Git will refuse to delete a branch that has work not merged yet. This protects you from losing something by mistake.

Delete the merged branch
Code Copied to Clipboard

# What is a merge conflict

Sometimes two branches change the same line of the same file. Then Git cannot decide which one is right. It stops and asks you. This is called a merge conflict. Please do not panic! It is a normal part of teamwork.

Git marks the problem inside the file. The next block shows how it looks. The part between <<<<<<< and ======= is your current branch. The part between ======= and >>>>>>> comes from the other branch.

A file with a conflict
Code Copied to Clipboard

Conflicts are not errors

A conflict does not mean you did something wrong. It only means two people touched the same place. To fix it, open the file and read both versions. Then decide what the final text should be. You can keep one side, or you can mix both. Most important, delete the marker lines <<<<<<<, ======= and >>>>>>>. They must not stay in your final file.

# How to resolve a conflict

Here is a calm way to fix a conflict.

Steps to resolve

  1. Run git status to see which files have conflicts
  2. Open each file and choose the final text
  3. Remove the marker lines
  4. Stage the file with git add
  5. Finish with git commit

If you feel lost in the middle of a merge, you can stop and go back to the state before the merge. The next command does exactly that. Use it whenever you want a fresh start.

Cancel a merge in progress
Code Copied to Clipboard

# Summary and next steps

Excellent work! You learned how to work with branches. Let us review.

What we learned

  • A branch is a safe side road for new work
  • git switch -c name creates and enters a branch
  • git merge name joins a branch into your current branch
  • git log --oneline --graph shows the history picture
  • git branch -d name deletes a merged branch
  • Conflicts happen when the same line changes twice, and you can fix them

Next steps

  • Practise with a small test repository
  • Learn about remote repositories on GitHub
  • Read the Git branching chapter

You are doing great. Keep going!