Back to Explore
GuideVibeFix editorialUpdated Nov 23, 2026

Git in Practice: Branching, Rebasing, Conflicts, Stash and Cherry-Pick

AI changes code fast, but Git is what brings it back when a change goes wrong. Using a fictional bookshop ledger as the demo, this guide runs through branching, rebasing, merge conflicts, stash and cherry-pick one by one — every command and terminal output verified on Git 2.43.0.

Real photo: a developer seen from behind at a desk, the large monitor showing a terminal with git status, git pull --rebase and git log, a laptop beside it running a code editor

Since AI started writing code, changing code has become remarkably easy: say one sentence and dozens of lines are replaced. But the faster you change things, the more a bad change costs. The code was fine before the change, and now it does not run — and you cannot remember what those lines looked like half an hour ago. What saves you then is not AI, but Git. Git is a safety net for your code: every commit is a snapshot, and you can return to any snapshot at any time.

This guide covers the five Git skills you will use most: branching, rebasing, resolving merge conflicts, stashing, and cherry-picking. Each one comes with a demo that was actually run, built around a fictional small bookshop ledger: a few book titles with prices and a two-line discount function. The example is deliberately tiny, because the point is not the bookshop — it is what Git does at each step. Every terminal output quoted here comes from a real run on 23 November 2026, with Git version 2.43.0. The short hashes in front of the commits, such as 4471d00, will be different when you run the same commands yourself: a hash is computed from the content and the time, so different times mean different hashes. Follow the structure, not the exact letters and digits.

One sentence up front that will save you time: branching, rebasing, and merging feel confusing at first because they describe different views of the same thing — which line your changes travel along. This guide answers each concept with a real commit-history graph, so you can see how the lines split and join back together. Seeing the lines beats memorising definitions.

How this guide differs from the other Git guide

VibeFix already has a guide called “Solo AI Git Workflow”, which is about working habits: how one person and one AI divide the work, how to name branches, how to write commit messages, three ways to roll back, and how to keep a daily CHANGELOG. That is a set of working practices. This guide is about Git itself: what a branch actually is, what rebasing really changes, why conflicts appear, and when to reach for stash and cherry-pick. If you already work the way that guide describes, this one explains the commands in your hands. If you have not read it, no preparation is needed — this guide starts from zero, and every step can be run on your own machine.

Preparation: make sure Git runs

Before you start, confirm Git is installed and check its version. Your version does not have to match mine exactly, but every command and output in this guide was verified on Git 2.43.0; much older versions may word a few messages slightly differently:

git --version
git version 2.43.0

Then create a practice repository. Keep the practice repo separate from your real projects — never practise on a project you care about. Learn the moves here first, then use them on real work. These few lines are the whole setup:

git init -b main
git config user.name "VibeFix Demo"
git config user.email "demo@example.invalid"

Two details are worth noting. First, git init -b main creates the repository with its main branch already called main; older tutorials default to master, but new projects generally use main now. Second, the git config lines have no --global flag, so this name and email apply only to this practice repository and do not touch the configuration of anything else on your machine. The name and email used in the demo are made up; when you are done, just delete the whole directory.

Next, put two files in the repository. README.md holds two lines of introduction, and books.txt lists two books with prices. The file contents are invented, but every operation is real:

git add .
git commit -m "First version: ledger skeleton"
[main (root-commit) 4471d00] First version: ledger skeleton
 2 files changed, 5 insertions(+)
 create mode 100644 README.md
 create mode 100644 books.txt

The commit succeeded. 4471d00 is this commit's short hash — effectively the serial number of this snapshot. From now on, any commit can be referred to by its number. Build one habit early: before every commit, run git status --short and look at which files are about to be committed. It costs nothing, and it catches many accidents where a temporary file gets committed by mistake.

Move one: branch — let changes walk down a side road first

The idea of a branch seems complicated to beginners, but it is actually very simple: a branch is a movable label stuck onto a particular commit. Right now you are on main, and every time you commit, the main label moves one step forward. Creating a new branch is just sticking on a second label — both labels start on the same commit, and after that, whichever branch you commit on is the label that moves.

Suppose you want to add a discount feature to the ledger. You could edit main directly, but if the idea turns out wrong halfway through, main is dirty. The right approach is to open a branch, work on it slowly, and merge it back into main when it is done:

git checkout -b feature/discount
git add .
git commit -m "Add a book and write a discount function"

The commit on the new branch looks like this: one more book, West with the Night, in books.txt, plus a new file calc.py holding a two-line discount function:

def discount(price, rate):
    return round(price * (1 - rate), 2)

Switch back to main and look: main is unchanged — no West with the Night, no calc.py. That is the most practical benefit of branches: when you switch branches, Git automatically restores the working directory to how that branch looked at its last commit. The official Pro Git book says the same thing: when you switch branches, Git adds, removes, and modifies files automatically to make sure your working copy is what the branch looked like at its last commit.

To give this branch something to work against, add one more commit on main: a line in the readme saying the shop restocked after a weekend stock check. Now the two lines have forked, and a history graph shows it most clearly:

git log --graph --oneline --all
* ec217b3 Main adds an inventory note
| * 960279f Add a book and write a discount function
|/
* 4471d00 First version: ledger skeleton

Learn to read this graph: 4471d00 at the bottom is the shared starting point, and above it the history splits in two — main's ec217b3 on the left, the branch's 960279f on the right. --graph draws the lines, --oneline puts each commit on one line, and --all draws every branch. Whenever history looks confusing, run this command first; the picture makes it clear. It is the most-used command in this guide, so it is worth memorising.

Move two: rebase — pull a forked history straight

The feature is done and you want it back in main. But main moved one step while you were working, so history has forked. There are two ways to join it back: merging and rebasing. Merging ties the two lines together with a knot and leaves a merge commit; rebasing lifts your commits off one by one and replays them on top of main's newest commit, so history looks like a single straight line.

Try rebasing first. Switch to the feature branch and rebase it onto main:

git checkout feature/discount
git rebase main
Successfully rebased and updated refs/heads/feature/discount.

Look at the history graph again — the fork is gone:

git log --graph --oneline --all
* 96a7b01 Add a book and write a discount function
* ec217b3 Main adds an inventory note
* 4471d00 First version: ledger skeleton

Notice one crucial detail: the commit's hash changed. Before the rebase it was 960279f; after the rebase it is 96a7b01. The commit message is identical and the change is identical, but the number is different. That is because a Git commit's number is computed from its content plus its parent — rebasing swapped its parent from 4471d00 to ec217b3, so the number had to change. In plain language: rebasing does not move the old commit over; it makes a fresh commit with the same change on a new parent, and the old one is discarded. The diagram below shows this exact rebase, before and after, with the real hashes:

Before-and-after rebase diagram: before rebasing, main and the branch fork apart and the branch commit hash is 960279f; after rebasing, history is one straight line and the branch commit hash is 96a7b01

After rebasing, merging back into main is smooth, because the branch now sits directly ahead of main and Git only has to move the main label one step forward. That is called a fast-forward merge:

git checkout main
git merge feature/discount
Updating ec217b3..96a7b01
Fast-forward
 books.txt | 1 +
 calc.py   | 2 ++
 2 files changed, 3 insertions(+)
 create mode 100644 calc.py

The Fast-forward in the output means exactly that: no new merge commit was created; main simply moved to 96a7b01. The official docs explain fast-forward the same way: when the commit you are merging in sits directly ahead of the commit you are on, Git simply moves the pointer forward, because there is no divergent work to merge together.

So when should you rebase and when should you merge? My rule of thumb is to look at who owns the branch and whether it has been pushed. For your own small local branch that has not been pushed to anyone yet, rebase to tidy history before merging — the history stays readable and easier to investigate later. For a shared branch that has already been pushed and that other people may be using, never rebase, because rebasing rewrites hashes and the old commits still sitting in other people's repositories will no longer match. The official rebase documentation makes the same point: a rebase merge works by replaying each commit from the working branch on top of the upstream branch. Replaying means the old commits are discarded and new ones take their place — which is why rebase only fits history that has not been shared yet.

If you are unsure whether a branch has been used by someone else, the safest choice is not to rebase — just merge. Merging never rewrites any existing commit; it only leaves one extra knot in history. Slightly uglier, but safe. Ugly history is fine; tangled history is the real problem.

Move three: merge conflicts — two people changed the same line

The thing people fear most about merging is not the fork, but the conflict. A conflict means two sides changed the same part of the same file in different ways, and Git cannot tell which one to trust, so it stops and asks you to decide. Note the “same part” condition: if one person changes line one and the other changes line ten, Git merges them itself and never bothers you. Only changes to the same line cause a conflict.

Let us create a real conflict. First open a price-change branch, change Three-Body from 59.9 to a sale price of 49.9, and commit; then switch back to main and change the same line to 69.9 and commit. Both sides touched the same book, the same line, with different numbers:

git checkout -b feature/price
git commit -m "Three-Body on sale at 49.9"
git checkout main
git commit -m "Main raises price: Three-Body to 69.9"
git log --graph --oneline --all
* 7265f50 Main raises price: Three-Body to 69.9
| * 454a54e Three-Body on sale at 49.9
|/
* 96a7b01 Add a book and write a discount function
* ec217b3 Main adds an inventory note
* 4471d00 First version: ledger skeleton

Now merge the branch into main, and the conflict arrives:

git merge feature/price
Auto-merging books.txt
CONFLICT (content): Merge conflict in books.txt
Automatic merge failed; fix conflicts and then commit the result.

These three lines are worth reading word by word. The first says Git is auto-merging books.txt; the second says there is a content conflict; the third says the automatic merge failed and asks you to fix the conflicts and commit the result. Git did not decide for you, and it did not damage your files — it simply stopped and waited. Run git status to see the current state:

On branch main
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:   books.txt

both modified means exactly what it says: both sides changed the file. Open books.txt and Git has marked the conflicted spot:

<<<<<<< HEAD
Three-Body 69.9
=======
Three-Body 49.9
>>>>>>> feature/price
To Live 39.0
West with the Night 45.0

Learn to read these markers: everything above the =======, under <<<<<<< HEAD, is your current branch's version (main), 69.9; below the ======= is the other branch's version, 49.9; and >>>>>>> feature/price closes the block. There is no standard answer for resolving a conflict — you decide from the real situation whether one price is right, or neither is and a middle value is needed. This time we take a compromise price of 54.9, delete the whole marked block plus both price lines, keep a single line Three-Body 54.9, and tell Git the conflict is resolved:

git add books.txt
git commit -m "Merge conflict: Three-Body compromise price 54.9"

After the merge, the history graph shows a shape you have not seen before:

*   93774a2 Merge conflict: Three-Body compromise price 54.9
|\
| * 454a54e Three-Body on sale at 49.9
* | 7265f50 Main raises price: Three-Body to 69.9
|/
* 96a7b01 Add a book and write a discount function

The top commit, 93774a2, is a merge commit, and it differs from an ordinary commit in one way: it has two parents — main's 7265f50 and the branch's 454a54e. The |\ and |/ in the graph are drawing exactly that. Whenever you see a graph that forks and then joins back together, you know a real merge happened there, not a fast-forward.

A quick honest note about a mistake I made while preparing this demo. On my first attempt at creating the conflict, I rewrote books.txt on the main side without actually changing anything: the price was still 59.9, not a character different. Git replied nothing to commit, working tree clean, no commit was created at all, and the merge fast-forwarded — so of course no conflict appeared. The lesson is very practical: a conflict requires both sides to really change the same line, and to change it differently. If the file content is identical, nothing changed, and Git will not play along. Once I spotted it, I changed main's price to a real 69.9 and ran it again, and the conflict appeared as it should. The outputs above are from that second, corrected run.

One more life-saving command: if a conflict looks wrong and you want to back out, use git merge --abort. I really ran it during the demo: after aborting, the working directory returned to its pre-merge state, books.txt went back to main's 69.9, and git status --short showed nothing at all. Think it through, merge again, and the same conflict reappears — nothing is lost. But note: if there is no merge in progress, the command errors out with fatal: There is no merge to abort (MERGE_HEAD missing). That only means “there is no merge to abort right now”, not that your repository is broken.

Move four: stash — what to do when work is interrupted halfway

The most common interruption in coding looks like this: you are halfway through something, the files are in a messy half-done state that cannot be committed yet, and suddenly you have to switch to another branch to fix an urgent problem. You cannot commit, because a half-finished commit looks terrible in history; and you cannot switch branches directly, because Git will stop you for fear of losing your uncommitted changes. The Pro Git chapter on stashing opens with exactly this situation: things are in a messy state, you want to switch branches for a bit, and you do not want to commit half-done work just to get back to this point later.

Stashing piles your uncommitted changes aside for a moment, leaving the working directory instantly clean, and you take them back out when you return. Try it: add a line for Dream of the Red Chamber in books.txt, do not commit, and stash it:

git status --short
 M books.txt
git stash push -m "WIP: add Dream of the Red Chamber first"
git status --short
git stash list
 M books.txt
Saved working directory and index state On main: WIP: add Dream of the Red Chamber first
stash@{0}: On main: WIP: add Dream of the Red Chamber first

After stashing, git status --short prints nothing — the working directory is as clean as if you had just committed. The Dream of the Red Chamber line is not lost; it sits on the stash stack under the name stash@{0}. Now you can switch to any branch you like. When you come back, take it out again with git stash pop:

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:   books.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (afffb29ba7a6a48596239e0c631f9e5565007424)

The Dream of the Red Chamber line is back in books.txt, and git diff shows it is exactly the line you added before. The last line, Dropped refs/stash@{0}, says the stash has been removed from the stack, so the stack does not keep growing. That is the difference between pop and apply: pop takes the stash out and deletes it from the stack, while apply takes it out but leaves a copy on the stack. If you are unsure the restore worked, use apply first, check the result, and then drop the stash — one extra layer of safety.

Two details about stashing trip up beginners. First, by default only changes to files Git already tracks are stashed; a brand-new file you have never run git add on is left behind, and you need -u to include it. Second, the stash stack can hold many entries, and stash@{0} is always the newest one — to take out an older one you must name its number. My advice: use the stash as a same-day holding spot, not as long-term storage. Once something sits in the stash for months, seeing stash@{0} will not remind you what is inside. So always stash with -m and a plain-language note, as in the example above.

Move five: cherry-pick — move just one commit across

Sometimes you do not want a whole branch, just one commit from it. For example, someone fixed a typo on another branch, and main wants that fix but not the branch's other commits. Cherry-pick takes exactly that one commit, by name.

Try it: open a typo-fix branch, change one character, stage it, commit, and note its hash 699c5cf; switch back to main and ask for it by its full hash:

git checkout -b fix/typo
git add .
git commit -m "Fix a typo in README"
git checkout main
git cherry-pick 699c5cfa9af32c4744ce9b81ad1a9bf26ce21800
[main ea74785] Fix a typo in README
 Date: Mon Nov 23 04:53:08 2026 +0800
 1 file changed, 1 insertion(+), 1 deletion(-)

The cherry-pick worked — but notice the hash changed again: it was 699c5cf, and on main it became ea74785. The reasoning is the same as with rebasing: the commit got a new parent, so its number had to be recalculated. What is interesting is that the output kept the original author date, 23 November 2026 at 04:53, so the record still shows when the commit was first written. The history graph now shows two commits with the same content on two branches, each with its own number:

* ea74785 Fix a typo in README
| * 699c5cf Fix a typo in README
|/
*   93774a2 Merge conflict: Three-Body compromise price 54.9

One easily missed point about cherry-pick: it can conflict too. If the same line on main has already been changed by someone else, the picked change will not apply cleanly, and Git stops and asks you to resolve it exactly like a merge conflict. Cherry-pick is not magic — it just automates “do this commit's change over again on another line”.

When is cherry-pick the right tool? The classic case is a hotfix: something broke in production, you fixed it on main, and you want only that one fix on the release branch without dragging main's new features along. Another case is a rescue: a colleague's branch has one small fix you need urgently, but the branch as a whole is not ready to merge, so you pick that one commit to tide you over. Be careful, though: too many cherry-picks leave history full of duplicate-content commits, which gets confusing when you investigate a problem later. If one merge can solve it, do not use five cherry-picks.

The safety net: deleting the wrong branch is fine, thanks to the reflog

By now you may be worrying: what if I delete the wrong branch? What if a rebase goes wrong? Here is some reassurance: almost every Git operation has an undo, called the reflog — a logbook of every move HEAD has made. Every branch switch, commit, rebase, merge, and cherry-pick is written down. Even if a branch is deleted, as long as the commit still exists, the reflog can help you find it again.

First, look at a real error from deleting a branch. Create a temporary branch, commit something on it, switch back to main, and delete it:

git branch -d temp/wip
error: the branch 'temp/wip' is not fully merged.
If you are sure you want to delete it, run 'git branch -D temp/wip'

Git refused, because the commits on that branch have not been merged into the current branch — deleting it would lose them. This is not a bug; Git is protecting you. Only when you are sure you do not want it, delete it forcefully with a capital -D:

git branch -D temp/wip
Deleted branch temp/wip (was 2cd11bb).

Lowercase -d is the safe delete: it refuses anything that has not been merged in. Capital -D is the forced delete — think before you use it. My habit is to always try lowercase d first; when it refuses, stop and think whether the branch still holds something unmerged. Nine times out of ten, the refusal itself reminds you that something in there is still needed.

Now look at the reflog itself:

git reflog
ea74785 HEAD@{0}: checkout: moving from temp/wip to main
2cd11bb HEAD@{1}: commit: WIP: not finished yet
ea74785 HEAD@{2}: checkout: moving from main to temp/wip
ea74785 HEAD@{3}: cherry-pick: Fix a typo in README
93774a2 HEAD@{4}: checkout: moving from fix/typo to main
699c5cf HEAD@{5}: commit: Fix a typo in README
93774a2 HEAD@{6}: checkout: moving from main to fix/typo
93774a2 HEAD@{7}: reset: moving to HEAD

Each line records one move of HEAD, with the newest first. If a rebase goes wrong one day, or a branch is deleted by accident, run git reflog, find the hash from just before the accident, and point a branch back at it with git reset --hard or by creating a new branch. That is Git's last safety net: commits themselves are very hard to lose for real — what usually gets lost is just the label pointing at them.

Seven common pitfalls, with fixes

  • Editing on main directly and only remembering to branch halfway through. Do not panic — the changes are still in the working directory, and nothing is committed yet: run git checkout -b new-branch-name and the changes come along with you to the new branch, then commit there, as long as the working directory is clean enough to switch without conflict.
  • Thinking that editing a file is the same as changing a commit. Editing a file without git add and git commit leaves nothing in history. Rebasing and merging only recognise commits, not uncommitted changes in your working directory. Commit first, then talk about branches.
  • Rebasing a public branch that has already been pushed. Rebasing rewrites hashes, and the old commits in other people's repositories will no longer match. Only rebase local branches that have not been pushed; always merge public branches.
  • Committing with conflict markers still in the file. <<<<<<< and >>>>>>> are markers for humans, not code. Before committing, check with git diff that no marker lines remain in the file.
  • Stashing something and forgetting to take it back out. The stash stack keeps growing, and months later no one remembers what each stash contains. Always stash with a -m note, and take it back out the same day — do not leave it overnight.
  • Cherry-picking so much that history is full of duplicate commits. One or two for an emergency is fine, but frequent cherry-picking usually means the branches are divided wrongly — consider merging the whole branch instead of moving commits one at a time.
  • Deleting branches only with capital -D. The lowercase -d refusal is protecting you. When it refuses, stop and ask whether the commits on that branch are really all unwanted.

Wrap-up: a checklist you can use right away

You now have all five moves. Before your next round of code changes, walk through this order: check git status and confirm the working directory is clean; open a branch for a new feature and work on it; when main has moved ahead, rebase the branch onto it or just merge; if a merge will not go through, read the conflict markers, fix them, git add, and commit; if you are interrupted halfway, stash and take it back out when you return; if you only want one commit from someone else's branch, cherry-pick it; when deleting a branch, try lowercase -d first, and do not rush to the capital letter when it refuses.

One last thing: Git is learned by typing, not by reading ten guides. The demo repository in this guide grows from zero to only eight commits; typing the commands yourself takes at most fifteen minutes. Copy the commands, not the outputs — read the outputs yourself and compare them with the ones quoted here. Different hashes are normal; matching structure is what matters. Once you have watched a fork appear with your own hands and then pulled it straight yourself, branching and rebasing will never feel mysterious again.

A good next step is the branching and merging chapter of the official documentation, or the first feature branch on your own real project — start with the smallest change, and do not try to do everything at once. Practise rebase, stash, and cherry-pick once each in the practice repository before using them on a real project.

Views 1Comments 0

Comments (0)

ME
0/1000
Loading comments...

Sources

Browse projectsPublish your project

Related articles