How to Create a Branch in Git and Merge It
To create a branch in Git, run git switch -c <branch-name>. Commit your changes on the new branch, switch back with git switch main, and bring the changes into main with git merge <branch-name>.
What you'll build or solve
You'll create a branch, commit on it, and merge it into main. Then you'll create a merge conflict on purpose and fix it, so you know what to do when it happens in a real project.
Build your GitHub portfolio with Mimo
When this approach works best
Use a branch when you:
- Try an idea you might throw away.
- Work on a feature that takes several commits before it's ready.
- Share changes with a team for review before they reach
main.
Prerequisites
- Git 2.23 or newer, so you have
git switch(check withgit --version) - The
git-practicerepository from the previous tutorials, on branchmainwith no uncommitted changes
This tutorial continues the git-practice project from How to Use Git and How to Push Code to GitHub. Use that repository, because the steps and outputs depend on its files and history. If you skipped the GitHub tutorial, your output won't mention origin/main or the Add a line commit.
Step-by-step instructions
Step 1: List your branches
In your terminal (Git Bash on Windows), go to your project folder and run:
Bash
git branch
* main
The * marks the branch you're on.
Step 2: Create a branch and switch to it
Bash
git switch -c add-notes
Switched to a new branch 'add-notes'
-c creates the branch, and git switch moves you onto it. Many older guides use git checkout -b add-notes, which does the same thing.
List your branches again:
Bash
git branch
* add-notes
main
Step 3: Commit on the new branch
Create a file and commit it:
Bash
echo "Branches let me try ideas safely." > notes.txt
git add notes.txt
git commit -m "Add notes"
[add-notes e1e6ea9] Add notes
1 file changed, 1 insertion(+)
create mode 100644 notes.txt
This commit only exists on add-notes. Your hash will be different.
Step 4: Switch back to main
Bash
git switch main
ls
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
README.md
notes.txt disappears from the folder. It isn't deleted. It belongs to add-notes, and Git shows the files of the branch you're on.
Step 5: Merge the branch into main
From main, merge add-notes:
Bash
git merge add-notes
Updating ff55d47..e1e6ea9
Fast-forward
notes.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 notes.txt
"Fast-forward" means main had no new commits of its own, so Git moved main forward to include the branch's commit.
Check the result:
Bash
ls
git log --oneline
README.md notes.txt
e1e6ea9 (HEAD -> main, add-notes) Add notes
ff55d47 (origin/main) Add a line
1c05a7f Describe the project
c8ee487 Add README
Step 6: Delete the merged branch
Bash
git branch -d add-notes
Deleted branch add-notes (was e1e6ea9).
The commit stays in main. Only the branch name is removed.
Practice: create and fix a merge conflict
A merge conflict happens when two branches change the same line in different ways. Git can't choose for you, so it asks you to decide.
Step 1: Change a line on a new branch
Bash
git switch -c change-title
Open README.md in your code editor, change the first line to # My Git practice, and save. Then commit:
Bash
git add README.md
git commit -m "Change title on branch"
Step 2: Change the same line on main
Bash
git switch main
Open README.md again. It shows the old title, because you're on main. Change the first line to # Git practice notes, save, and commit:
Bash
git add README.md
git commit -m "Change title on main"
Step 3: Merge and read the conflict
Bash
git merge change-title
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
git status shows which files need your attention:
On branch main
Your branch is ahead of 'origin/main' by 2 commits.
(use "git push" to publish your local commits)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
Open README.md. Git has written both versions into the file:
<<<<<<< HEAD
# Git practice notes
=======
# My Git practice
>>>>>>> change-title
Learning Git step by step.
Pushed from my computer.
- Between
<<<<<<< HEADand=======is the version onmain, the branch you're on. - Between
=======and>>>>>>> change-titleis the version fromchange-title. - Lines outside the markers had no conflict.
Step 4: Resolve the conflict
Edit the file so it contains exactly what you want, and delete all three marker lines. You can keep one version, or combine them:
# My Git practice notes
Learning Git step by step.
Pushed from my computer.
Save the file, then mark the conflict as resolved and finish the merge:
Bash
git add README.md
git commit
Git opens your editor with the message Merge branch 'change-title'. Save and close it to finish. To skip the editor and keep that message, run git commit --no-edit instead.
[main 9332634] Merge branch 'change-title'
Step 5: Check the history
Bash
git log --oneline --graph
* 9332634 (HEAD -> main) Merge branch 'change-title'
|\
| * 5ab8e7d (change-title) Change title on branch
* | 5f74e70 Change title on main
|/
* e1e6ea9 Add notes
* ff55d47 (origin/main) Add a line
* 1c05a7f Describe the project
* c8ee487 Add README
The graph shows the two branches splitting and joining again in a merge commit. Delete the branch with git branch -d change-title. If your project is on GitHub, run git push to upload the merged main.
The escape hatch: git merge --abort
If a merge gets confusing before you commit it, run:
Bash
git merge --abort
Your files go back to how they were before git merge, and you can try again.
Push a branch to GitHub
If your project is on GitHub, you can push a branch too. Create it, commit on it, and push it:
Bash
git switch -c add-contact
echo "Contact: you@example.com" >> README.md
git add README.md
git commit -m "Add contact line"
git push -u origin add-contact
GitHub's reply includes a link to create a pull request for the branch. A pull request shows your changes on GitHub so teammates can review them before they're merged into main.
After you merge a pull request on GitHub, update your local main:
Bash
git switch main
git pull
Common mistakes and how to fix them
Mistake 1: Switching branches with uncommitted changes
You might see:
error: Your local changes to the following files would be overwritten by checkout:
README.md
Please commit your changes or stash them before you switch branches.
Aborting
Why it breaks:
Switching would overwrite changes you haven't saved in a commit.
Fix:
Commit your changes on the current branch first, then switch.
Mistake 2: Deleting a branch that isn't merged
You might see:
error: the branch 'add-notes' is not fully merged
Why it breaks:
The branch has commits that aren't in your current branch. Deleting it would lose them.
Fix:
Merge the branch first, then delete it. Git's hint suggests git branch -D. It deletes the branch anyway, so only use it when you're sure you don't need those commits.
Mistake 3: Leaving conflict markers in the file
Why it breaks:
Git doesn't check for leftover <<<<<<<, =======, or >>>>>>> lines when you run git add. If you commit them, they end up in your code and can break it.
Fix:
Before git add, search the file for <<<<<<<. If you already committed markers, remove them and make a new commit.
Mistake 4: Committing on the wrong branch
Why it breaks:
New commits go to the branch you're on. If you forget to switch, they land on main.
Fix:
Check where you are with git branch or the first line of git status before you start working.
Quick recap
- List branches with
git branch. - Create and switch with
git switch -c <name>orgit checkout -b <name>. - Commit on the branch, then
git switch main. - Merge with
git merge <name>and delete withgit branch -d <name>. - To fix a conflict, edit the file, remove the markers,
git add, andgit commit. - Back out of a merge with
git merge --abort.
Next steps
Go back to How to Use Git or How to Push Code to GitHub for a refresher, or see all guides in the Git and GitHub tutorials.
Branching becomes second nature once you build real projects. Mimo's Full-Stack Developer and Front-End Developer paths teach you to code and have you build portfolio projects on GitHub.
Join 40M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot