Git Essentials

Managing History

T'Chaka Dev· 5 pages· 4 min read

Merge vs rebase

Merge preserves the true history, including the fact that two lines of work happened at once. Rebase rewrites it into a straight line that reads more cleanly but never actually happened.

DIAGRAM the same two branches merged on the left and rebased on the right, side by side

Never rebase a branch other people have pulled. You are rewriting a history they already have, and their next pull will be a mess.

Undoing things

bash
git restore file.txt git reset --soft HEAD~1 git revert <commit>

revert is the only one of the three that is safe on a shared branch — it adds a new commit rather than removing an old one.

Reflog

bash
git reflog

Almost nothing in git is truly lost for about thirty days. reflog is where you find it.