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:
cd ~/git-demo/notes
git status
git branchgit 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:
git switch -c add-weekend-tasksgit 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
git branch
git branch --show-currentBoth 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:
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:
git switch main
lsweekend.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:
git switch add-weekend-tasks
ls
git switch mainThe last command leaves you on main for the next step.
Check it worked
git log --oneline --all --graph --decorateThe 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:
git merge add-weekend-tasksGit 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
git log --oneline --graph --decorateThe 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:
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:
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
git log --oneline --graph --decorateThe 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:
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
git log --oneline --graph -4The 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:
git branch --mergedDelete them with -d, which refuses to delete a branch that has unmerged work:
git branch -d add-weekend-tasks add-shopping add-remindersGit 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:
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:
git branch -D experimentCheck it worked
git branchOnly 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:
git switch -c wip
git branch -m better-name
git branch --show-current
git switch main
git branch -d better-nameTwo 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.
git branch -vvTroubleshooting
error: pathspec 'name' did not match any file(s) known to gitaftergit switch namemeans the branch does not exist. Check the spelling withgit branch, or create it withgit switch -c name.error: Your local changes to the following files would be overwritten by checkoutmeans you have uncommitted edits to a file that differs between the two branches. Commit them, or set them aside withgit stash, then switch.fatal: refusing to merge unrelated historiesmeans the two branches have no common commit, for example because they come from two separategit initruns. 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:
rm -rf ~/git-demoNext steps
- Handle the case where both branches changed the same lines: resolve a Git merge conflict.
- Move a branch back or undo a merge: undo mistakes in Git.
- Put branches on a server and ask for a review: push to GitHub over SSH and open a pull request.
Related tutorials
- How to Install and Configure GitYou will install Git 2.53 from Ubuntu 26.04's own packages, set your name and email, make main the default branch, choose an editor and a few safe defaults, use a different email for work projects, and create an SSH key to use with GitHub.
- How to Make Your First Git Commits and Read the HistoryYou will turn a folder into a Git repository, make commits in small steps, check what changed before you commit, ignore files that do not belong in history, rename and delete files, and read the history back in several ways.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials