Skip to main content

Dev tools and workflow · Beginner · 15 min

How to Create, Switch and Merge Branches in Git

By Padding · Updated 15 Nov 2026
Ubuntu 26.04.1 LTS · Git 2.53 (Ubuntu package)

You will create branches, switch between them, commit on each, merge one into another in the two ways Git can, read the branch graph, and delete branches you no longer need.

On this page

A branch is a movable label that points at a commit. Making one costs nothing, which is why Git users make one for every piece of work: a fix, an experiment, a feature. Your main line stays stable while you work, and when the work is good you merge it back.

This tutorial uses the notes repository from the previous tutorial. You will make branches, commit on them, and merge them. Then you will look at what Git does in each case, because the difference between a fast-forward merge and a merge commit is the part that confuses people most.

Step 1 · See where you are

Go to the repository and ask Git which branches exist:

Bash
cd ~/git-demo/notes
git status
git branch

git branch lists local branches and puts a * in front of the one you are on. Right now that is main, the only one. HEAD is Git's name for "where you are": normally it points at a branch, and the branch points at a commit.

Make sure the working tree is clean before you move around. The status should say nothing to commit, working tree clean. If it does not, commit the changes first. Git carries uncommitted edits across a switch when it can, which surprises people.

Step 2 · Create a branch and switch to it

Create a branch for a new piece of work and move to it in one command:

Bash
git switch -c add-weekend-tasks

git switch changes branch, and -c creates the branch first. Git prints Switched to a new branch 'add-weekend-tasks'. The new branch points at the same commit as main, so nothing in your folder changed. Older tutorials use git checkout -b for this. It still works, but switch does only branch changes, so it is harder to misuse.

Check it worked

Bash
git branch
git branch --show-current

Both branches are listed and add-weekend-tasks has the *. --show-current prints just its name, which is handy in scripts.

Step 3 · Commit on the branch

Add a weekend list and commit it:

Bash
cat > weekend.txt <<'TXT'
Clean the bike
Plan the trip
TXT
git add weekend.txt
git commit -m "Add a weekend task list"

Then go back to main and look at the folder:

Bash
git switch main
ls

weekend.txt is gone from the listing. It is not lost: it belongs to the commit on the other branch, and Git rewrote your working folder to match main. Switch back and the file returns:

Bash
git switch add-weekend-tasks
ls
git switch main

The last command leaves you on main for the next step.

Check it worked

Bash
git log --oneline --all --graph --decorate

The graph shows two commits at the top. The branch label add-weekend-tasks sits on the newest one, and main is on the commit below it. --all includes every branch, not only the one you are on.

Step 4 · Merge with a fast-forward

main has not moved while you worked, so merging is simple. Git only has to slide the main label up to the branch's commit. This is called a fast-forward. Run the merge from the branch that should receive the work:

Bash
git merge add-weekend-tasks

Git prints Fast-forward and lists weekend.txt as a new file. No new commit exists. main and add-weekend-tasks now point at the same commit.

Check it worked

Bash
git log --oneline --graph --decorate

The history is a straight line, and both labels are on the top commit.

Step 5 · Merge when both branches have moved

A fast-forward is only possible when the receiving branch has no new commits of its own. Create that situation. Make a branch and a commit on it, then return to main and commit there as well:

Bash
git switch -c add-shopping
printf 'Milk\nBread\n' > shopping.txt
git add shopping.txt
git commit -m "Add a shopping list"
git switch main
echo "Pay the rent" >> tasks.txt
git commit -am "Add rent to the task list"

The two branches have diverged: each has a commit the other lacks. Merge add-shopping into main. Git cannot slide a label this time, so it creates a merge commit with two parents. The -m option gives the message, so Git does not open your editor:

Bash
git merge add-shopping -m "Merge the shopping list"

The two sets of changes touch different files, so Git combines them without asking you anything. The output says Merge made by the 'ort' strategy and lists the files. When both branches change the same lines, Git stops and asks you to decide. That is the subject of the next tutorial.

Check it worked

Bash
git log --oneline --graph --decorate

The graph now shows the two lines of work coming back together at the merge commit, and the merge commit is at the top, labelled main. git log --merges --oneline lists only the merge commits. git log --first-parent --oneline follows only the main line and hides the commits that came in from other branches, which gives a summary of what was merged and when.

Step 6 · Force a merge commit when you want one

Some teams want a merge commit even when a fast-forward is possible, so that the history records that a group of commits was one piece of work. --no-ff does that:

Bash
git switch -c add-reminders
echo "Water the plants" > reminders.txt
git add reminders.txt
git commit -m "Add reminders"
git switch main
git merge --no-ff add-reminders -m "Merge the reminders"

Without --no-ff this merge would have been a fast-forward, because main had not moved since you made the branch. Which style you use is a team decision. This tutorial uses fast-forwards for small changes, and merge commits when a branch holds work worth keeping together.

Check it worked

Bash
git log --oneline --graph -4

The newest commit is the merge commit, and it has the reminders commit as a second parent, drawn as a short side line.

Step 7 · Delete merged branches

A branch label is cheap, but a long list of stale ones is clutter. Once the work is merged, the label has done its job. The commits stay in the history, because main contains them. git branch --merged lists the branches whose work is already in the current branch:

Bash
git branch --merged

Delete them with -d, which refuses to delete a branch that has unmerged work:

Bash
git branch -d add-weekend-tasks add-shopping add-reminders

Git prints Deleted branch for each, with the commit it pointed at. To see the safety check, make a branch with a commit that you do not merge, and try to delete it:

Bash
git switch -c experiment
echo "Idea" > idea.txt
git add idea.txt
git commit -m "Try an idea"
git switch main
git branch -d experiment || echo "refused: the branch has unmerged commits"

Git refuses with error: the branch 'experiment' is not fully merged and a hint to use -D. git branch -D experiment deletes it anyway, and the commit becomes hard to find, so use -D only when you mean to throw the work away. Here you do:

Bash
git branch -D experiment

Check it worked

Bash
git branch

Only main is left.

Step 8 · Rename a branch and look at all of them

Rename the current branch with git branch -m. This is useful when you named a branch after a task and it changed. Try it on a throwaway branch:

Bash
git switch -c wip
git branch -m better-name
git branch --show-current
git switch main
git branch -d better-name

Two commands help when you have many branches. git branch -v adds the latest commit of each branch, and git branch -vv also shows the remote branch each one tracks, which you will use when you push in the last tutorial of the series.

Bash
git branch -vv

Troubleshooting

  • error: pathspec 'name' did not match any file(s) known to git after git switch name means the branch does not exist. Check the spelling with git branch, or create it with git switch -c name.
  • error: Your local changes to the following files would be overwritten by checkout means you have uncommitted edits to a file that differs between the two branches. Commit them, or set them aside with git stash, then switch.
  • fatal: refusing to merge unrelated histories means the two branches have no common commit, for example because they come from two separate git init runs. Check that you are in the right repository before you add --allow-unrelated-histories.
  • You merged into the wrong branch. Do not panic; the undo tutorial shows how to step back.

Step 9 · Clean up

Keep the notes repository for the next tutorial. To remove everything from the series so far, run:

Bash
rm -rf ~/git-demo

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.