You have a note on your desktop. It holds the eleven Git commands you trust, copied from old chat messages and a colleague's screen. Anything outside those eleven makes you nervous. When a rebase goes strangely, you delete the folder, clone the repository again and redo your changes by hand. It works, mostly. It also costs you an hour every time, and it keeps you quietly afraid of a tool you use fifty times a day.
The way out is not memorising more commands. It is learning the handful of ideas the commands are built on. Git is far less mysterious once you see them.
A commit is a snapshot of your whole project at one moment, along with a record of which commit came before it. Commits link backwards to their parents, and together they form the history.
A branch is just a name pointing at one commit. When you commit on a branch, the name moves forward to the new commit. That is why creating a branch is instant. You are not copying anything.
HEAD is Git's way of saying where you are right now. Usually it points at a branch.
With those three ideas, most commands become readable. A merge usually creates a new commit with two parents. A rebase replays your commits on top of a different one, creating new commits with new IDs, which is why rebasing work other people already have causes so much confusion. A reset moves a branch name to a different commit.
Then learn about the reflog, because it removes most of the fear. Git keeps a local record of where HEAD and your branches have pointed recently. If a reset or a rebase seems to have eaten your work, git reflog will usually show the commit you lost, and you can create a branch on it. By default those entries are kept for weeks, so committed work is very rarely gone.
Practise somewhere safe. Make a throwaway repository with three small files and spend an afternoon breaking it. Create branches, merge them, rebase them, reset them, and run git log --oneline --graph --all after every step so you can see what moved.
The next time something goes wrong in a real repository, stop before you delete the folder. Look at the graph and the reflog. You will be surprised how often the answer is already there.
– Asael Shinder
Top comments (0)