Git Branching Guide

Create a branch, commit your work, merge it back, or copy only one commit with cherry-pick.

Quick summary

Use this when you need the short version before reading the full explanation.

1. The basic idea

A branch is a separate line of work. You create a branch when you want to change code without touching the original branch immediately.

The original branch is often called main, master, or develop. In this guide, the original branch is called main. Replace main with your real branch name if needed.

2. Check where you are

Before creating or merging branches, check your current branch and your working state.

git status
git branch --show-current
git branch

git status tells you whether you have uncommitted changes. It is one of the most important Git commands.

Important: Before switching branches or merging, try to keep your working tree clean. That means your changes are either committed or intentionally stashed.

3. Create a new branch

Start from the original branch, usually main.

git switch main
git pull origin main

Create and move to a new branch:

git switch -c feature/my-change

Example branch names:

Older Git tutorials use git checkout -b feature/my-change. That still works, but git switch -c is clearer for branch switching.

4. Work on the branch

Now you are inside your new branch. Edit your files normally in your code editor.

After editing, check what changed:

git status
git diff

git diff shows the actual line-by-line changes before you commit.

5. Add and commit changes in the branch

After you make a change, stage the files you want to commit.

git add path/to/file

Or stage all changed files:

git add .

Then commit:

git commit -m "Describe what you changed"

Example:

git add src/App.js
git commit -m "Fix navbar mobile layout"
Good commit messages are short but specific. Example: Fix navbar mobile layout is better than changes.

6. Push your branch to GitHub or remote repository

If you are using GitHub, GitLab, Bitbucket, or another remote repository, push the branch:

git push -u origin feature/my-change

The -u connects your local branch to the remote branch. After this first push, you can usually just use:

git push

7. Merge the branch back into the original branch

Use merge when you want to bring the whole branch into the original branch.

First, switch back to the original branch:

git switch main

Make sure the original branch is up to date:

git pull origin main

Merge your feature branch into main:

git merge feature/my-change

Push the updated original branch:

git push origin main
Read the merge command as: “I am currently on main, and I want to bring feature/my-change into main.”

8. What if there is a merge conflict?

A conflict happens when Git cannot automatically decide which version of a file to keep.

Git will show conflicted files. Check them with:

git status

Open the conflicted file. You may see something like this:

<<<<<<< HEAD
Code from main
=======
Code from feature/my-change
>>>>>>> feature/my-change

Edit the file manually and keep the correct version. Then continue:

git add path/to/conflicted-file
git commit

If you want to cancel the merge instead:

git merge --abort

9. Cherry-pick: take only one commit from another branch

Use cherry-pick when you do not want the whole branch. You only want one specific commit, or a few specific commits.

First, find the commit hash from the branch:

git log --oneline feature/my-change

You might see something like:

a1b2c3d Fix navbar mobile layout
9f8e7d6 Add temporary experiment
4a5b6c7 Update dashboard copy

Switch to the branch that should receive the commit:

git switch main

Cherry-pick the commit you want:

git cherry-pick a1b2c3d

Push the result:

git push origin main
Cherry-pick creates a new commit with a new commit hash. It copies the change; it does not move the original commit.

10. Cherry-pick multiple commits

Cherry-pick several separate commits:

git cherry-pick a1b2c3d 4a5b6c7

Cherry-pick a consecutive range of commits:

git cherry-pick abc123^..def456

This means: include abc123 through def456.

11. What if cherry-pick has a conflict?

Check the conflict:

git status

Fix the files manually, then continue:

git add path/to/file
git cherry-pick --continue

Cancel the cherry-pick:

git cherry-pick --abort

12. Merge vs cherry-pick: which one is easier?

Situation Use this Why
You want the whole branch Merge It brings all commits from the branch into the original branch.
You only want one specific commit Cherry-pick It copies only the selected commit.
You are a beginner and the branch contains only the changes you want Merge Usually simpler and easier to understand.
The branch has experimental commits you do not want Cherry-pick You can choose only the safe commits.
You want to keep clear project history Merge or Pull Request Better for normal team workflows.
Simple rule: merge is easier when you want everything. Cherry-pick is better when you want only selected commits.

13. Safe practice before merging

If you are not sure, create a backup branch before merging:

git switch main
git branch backup/main-before-merge

Or test the merge on a temporary branch first:

git switch main
git switch -c test-merge
git merge feature/my-change

If the test merge looks good, go back to main and do the real merge.

14. Full example workflow

This is a complete example from start to finish.

# Start from the original branch
git switch main
git pull origin main

# Create a new branch
git switch -c feature/navbar-fix

# Edit files in your editor, then check changes
git status
git diff

# Commit the change
git add src/Navbar.js
git commit -m "Fix navbar layout on mobile"

# Push the feature branch
git push -u origin feature/navbar-fix

# Merge back into original branch
git switch main
git pull origin main
git merge feature/navbar-fix
git push origin main

15. Full cherry-pick example

Use this when you only want one commit from the branch.

# Find the commit you want
git log --oneline feature/navbar-fix

# Move to the branch that should receive the commit
git switch main
git pull origin main

# Copy only one commit into main
git cherry-pick a1b2c3d

# Push the result
git push origin main

16. Useful commands cheat sheet

Command Meaning
git status Show current branch and changed files.
git branch List local branches.
git switch branch-name Move to another branch.
git switch -c new-branch Create a new branch and move to it.
git add . Stage all changed files.
git commit -m "message" Save staged changes as a commit.
git merge branch-name Bring another branch into your current branch.
git cherry-pick commit-hash Copy one commit into your current branch.
git log --oneline Show commits in a short format.
git branch -d my-branch Delete a local branch
git branch -D my-branch Force delete a local branch

Delete a branch

After you merge a branch and you no longer need it, you can delete it.

Delete a local branch

git branch -d my-branch

Use -d when the branch has already been merged. Git will protect you if the branch contains work that was not merged.

Force delete a local branch

git branch -D my-branch

Use -D only if you are sure you do not need the work in that branch. This deletes the branch even if it was not merged.

Delete a remote branch

git push origin --delete my-branch

This deletes the branch from the remote repository, for example GitHub.

Important rule

Do not delete a branch before checking that the work was merged into the correct original branch.

git checkout main
git pull
git log --oneline
git branch --merged