Branches, merging, conflicts, rebase and stash
Work on features without breaking main: create and switch branches, fast-forward and three-way merges, read and resolve a real merge conflict, rebase a branch on main, and park unfinished work with stash.
Key ideas
How it works
1. Branch and fast-forward merge
$ git switch -c feature/quiz
Switched to a new branch 'feature/quiz'
$ echo 'Q1: What is a commit?' > quiz.md && git add quiz.md && git commit -q -m "Add quiz question 1"
$ git log --oneline --all
1e3e254 Add quiz question 1
fb1523a Ignore cache files and .env
e26a370 Add Git notes and a hello script
5e9b349 Add README with the course title
$ git switch main
Switched to branch 'main'
$ git merge feature/quiz
Updating fb1523a..1e3e254
Fast-forward
quiz.md | 1 +
1 file changed, 1 insertion(+)
create mode 100644 quiz.md
$ git branch -d feature/quiz
Deleted branch feature/quiz (was 1e3e254).Because main had no new commits, Git simply moved main forward ("Fast-forward"). No merge commit was needed.
2. A real merge conflict, resolved
Here both branches changed the first line of README.md. Read the markers, write the final version, then add and commit:
$ git log --oneline --graph --all -4
* 2117d1a Change title on main
| * 1670ef3 Rename notes title
|/
* 1e3e254 Add quiz question 1
* fb1523a Ignore cache files and .env
$ git merge feature/title
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
$ git status --short
UU README.md
$ cat README.md
<<<<<<< HEAD
# My Git and GitHub notes
=======
# Asha study notes
>>>>>>> feature/title
## Git
- commit = snapshot
$ printf '# Asha: Git and GitHub notes\n\n## Git\n- commit = snapshot\n' > README.md
$ git add README.md
$ git commit -q -m "Merge feature/title: keep both ideas in the title"
$ git log --oneline --graph -5
* 14bb0e2 Merge feature/title: keep both ideas in the title
|\
| * 1670ef3 Rename notes title
* | 2117d1a Change title on main
|/
* 1e3e254 Add quiz question 1
* fb1523a Ignore cache files and .env| Marker | Meaning |
|---|---|
<<<<<<< HEAD | Start of your current branch's version |
======= | Divider |
>>>>>>> feature/title | End of the incoming branch's version |
- Edit the file so it reads exactly as it should, and delete all three marker lines.
git addthe file, thengit committo finish the merge. To give up instead, rungit merge --abort.- Editors such as VS Code show buttons: Accept Current, Accept Incoming, Accept Both.
3. Rebase for a straight history
$ git log --oneline --graph --all -3
* e0492ac Add glossary
| * 6478e23 Add quiz question 2
|/
* 14bb0e2 Merge feature/title: keep both ideas in the title
|\
$ git switch feature/glossary
Switched to branch 'feature/glossary'
$ git rebase main
Rebasing (1/1)
[KSuccessfully rebased and updated refs/heads/feature/glossary.
$ git log --oneline --graph -3
* 209b3bc Add glossary
* 6478e23 Add quiz question 2
* 14bb0e2 Merge feature/title: keep both ideas in the title
|\
$ git switch main && git merge --ff-only feature/glossary
Updating 6478e23..209b3bc
Fast-forward
glossary.md | 1 +
1 file changed, 1 insertion(+)
create mode 100644 glossary.md
Switched to branch 'main'4. Merge or rebase?
| Merge | Rebase | |
|---|---|---|
| History | Keeps exactly what happened, with merge commits | Straight line, easier to read |
| Safety | Never rewrites commits | Rewrites your branch's commits |
| Use when | Joining shared branches, finishing a pull request | Updating your own feature branch with the latest main |
On GitHub you can choose Create a merge commit, Squash and merge (one commit per pull request) or Rebase and merge for each pull request.
5. Stash unfinished work
$ echo 'half-written idea' >> glossary.md
$ git stash push -m "glossary draft"
Saved working directory and index state On main: glossary draft
$ git status --short
$ git stash list
stash@{0}: On main: glossary draft
$ git stash pop
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: glossary.md
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (a9804dbe10f8ba3363df232790ddcdfe5accab07)
$ git status --short
M glossary.md
$ git restore glossary.md6. Branch naming that teams like
feature/signup-otp,fix/cart-total-rounding,docs/readme-setup.- Lowercase, hyphens, short. Include a ticket number if your team uses one:
fix/PAY-142-refund-status. - Delete branches after merging:
git branch -d namelocally; GitHub can delete them automatically.
Common mistakes and fixes
| Mistake | Fix |
|---|---|
| Working directly on main | Create a branch for every change and merge through a pull request. |
| Leaving conflict markers in the file | Search for <<<<<<< before committing; tests should fail if markers remain. |
| Rebasing a branch others use | Merge instead, or agree with the team first. |
Practice
- Create two branches that change the same line, merge them, and resolve the conflict.
- Rebase a feature branch on main and compare git log --graph before and after.
- Stash a half-finished change, fix something on main, then pop the stash.
Quick check
When does Git do a fast-forward merge?
When the target branch has no new commits since the feature branch started, so Git just moves the pointer.
How do you finish a merge after fixing a conflict?
git add the fixed files, then git commit (or git merge --abort to cancel).
What is the golden rule of rebasing?
Never rebase commits that other people have already pulled, such as main or shared branches.
What does git stash do?
It shelves uncommitted changes so the working tree is clean; git stash pop brings them back.