Branches
A branch is a movable label, not a copy
A branch is a label on a commit. HEAD names the branch you are on.
In older tools a branch was a copy of the whole project. In Git it is almost nothing. Here is a history of three commits. The label main sits on the newest one, and HEAD says that main is the branch you are on. Create a branch called feature, and Git adds a second label on the same commit. Nothing is copied. It takes no time and no space.
Branches · Going deeper
What is inside the .git folder
Blobs, trees and commits, plus labels that name them.
The graph
Committing moves your branch; switching moves HEAD
Each command moves one label or adds one commit.
Watch five commands change the picture, one thing at a time. Switch dash c creates the label feature and moves HEAD to it. Commit adds C, and the feature label moves forward with it. Main does not move. Another commit, and feature moves again. Switch main moves HEAD back. No commit is created; your files change to match main. Commit now adds E on main. The history has forked.
Fast-forward
If main has not moved, a merge just slides the label
Nothing to combine, so no new commit: main simply moves forward.
The last page ended with two branches to bring together. Start with the easy case. Feature is two commits ahead, and main has not moved since the branch was made. Merge feature into main, and Git has nothing to combine. It slides the label main forward to the same commit. That is called a fast-forward.
Merge commits
A merge commit joins two histories and has two parents
Git compares both tips with their common ancestor and creates one commit.
Usually main has moved on while you worked. Now each branch has commits the other lacks. They share one ancestor, commit B. Merging creates a new commit, M, with two parents: one on each side. Git builds its snapshot by comparing both tips against the ancestor and taking every change that only one side made.
Conflicts
A conflict is Git asking a question it cannot answer
The same line changed both ways. Git marks it and waits for you.
Sometimes the automatic merge is not possible. In the common ancestor the port was eighty eighty. Main changed it to eighty. Feature changed the same line to four four three. Git cannot know which is right, so it stops and writes both versions into the file, between marker lines. You edit the file to what it should be, remove the markers, add it and commit. Or you abort, and everything is as it was.
Merge or rebase
Merge records what happened; rebase straightens it
Merge keeps every commit. Rebase replays yours with new ids.
There are two ways to bring the same fork together. Here it is again: C and D on feature, E on main. Merge adds one commit that ties the two lines together. The history shows exactly what happened, fork and all. Rebase takes C and D off, and replays them after E. The result is a straight line, as though you had started your work today. But the replayed commits are new commits, with new ids.
Pushing a rebase · Going deeper
After a rebase, push with --force-with-lease, never plain --force
--force-with-lease refuses to overwrite commits you have not seen.
Undoing
Rewind private work; add a commit to undo shared work
restore for edits, reset for private commits, revert for shared ones.
How you undo depends on how far the mistake has travelled. If it is not committed, restore the file. Be careful: uncommitted edits that you throw away are gone. If it is committed but only on your machine, reset moves the branch label back, and the commit drops out of history. If others already have it, do not rewind. Revert adds a new commit that does the opposite, and history stays intact for everyone.
Undoing · Going deeper · 1 of 2
The reflog: your safety net
The reflog remembers where HEAD was. Dropped commits can come back.
reset modes · Going deeper · 2 of 2
reset always moves the branch; the mode decides what else is rewound
--soft keeps everything staged, the default unstages, --hard destroys the edits.
Lesson complete
You finished “Branching and merging”
Nicely done.