Git Without Fear: Make a Mess, Then Clean It Up
Photo: developer terminal git commands code screen, via ideasatdom.wustl.edu
Git fear usually comes from one thing: the feeling that a wrong command will erase your work forever. That feeling is understandable, but it is also a bad teacher. The way to get comfortable is not to memorize more commands. It is to make a disposable repository, break it on purpose, and practice getting out of trouble.
This is a sandbox lesson. Nothing here touches your real projects unless you copy the commands into one, which you should not do until you have done the drills at least twice.
Set Up the Disposable Repo
Open your terminal and make a folder you can delete without crying.
mkdir git-sandbox
cd git-sandbox
git init
git config user.name 'Sandbox User'
git config user.email '[email protected]'
printf 'line one\nline two\nline three\n' > notes.txt
git add notes.txt
git commit -m 'first commit'
You now have a tiny repo with one file and one commit. Run git log --oneline and copy the hash of that first commit. You will need it later.
Drill 1: Create a Merge Conflict on Purpose
Conflicts are not errors. They are Git asking you to make a decision. Most people panic because they see angle brackets and assume they broke something.
git switch -c left
printf 'line one\nleft version\nline three\n' > notes.txt
git commit -am 'left changes line two'
git switch main
printf 'line one\nmain version\nline three\n' > notes.txt
git commit -am 'main changes line two'
git merge left
Git stops and tells you there is a conflict. Open notes.txt. You will see <<<<<<<, =======, and >>>>>>> markers. That is not damage. It is a multiple-choice question. Pick one version, delete the markers, save the file.
git add notes.txt
git commit -m 'resolve conflict'
Now run git log --oneline --graph --all. You just survived a conflict. Do it again with a different file and a different resolution. The second time is easier. The fifth time is boring, which is the goal.
Drill 2: Get Detached and Rescue Yourself
A detached HEAD sounds like a horror movie. It usually means you checked out a commit instead of a branch. You can look around, but new commits will not belong to any branch unless you make one.
git switch --detach HEAD~1
printf 'detached experiment\n' >> notes.txt
git add notes.txt
git commit -m 'experiment in detached state'
You are now floating. Do not panic. Give the commit a branch name.
git switch -c rescue-branch
git switch main
git merge rescue-branch
You just turned a detached experiment into a normal branch. If you do not want the experiment, switch back to main and delete the branch. Nothing is lost. Detached HEAD is a mode, not a mistake.
Drill 3: Rebase Badly, Then Abort
Rebase is powerful and easy to misuse. The safest way to learn is to start a rebase, hate it, and abort.
git switch -c feature
printf 'feature line\n' >> notes.txt
git commit -am 'feature work'
git switch main
printf 'main line\n' >> notes.txt
git commit -am 'main work'
git switch feature
git rebase main
You will probably hit a conflict because both branches changed the same file. That is fine. Run git rebase --abort. The rebase disappears. Your branch is exactly where it was. This is the most important lesson in the drill: Git often gives you an undo button, but you have to know it exists.
Try the rebase again. This time resolve the conflict, git add notes.txt, and run git rebase --continue. Then look at the log. You have rewritten history on your own branch. That is acceptable in a sandbox and sometimes acceptable on a feature branch. It is not acceptable on a shared branch that other people have pulled. Learn the difference now.
Drill 4: Delete Commits and Use Reflog
This is the one that scares people most. You can move a branch backward and feel like you lost work. Reflog is the safety net.
git switch main
git reset --hard HEAD~1
git log --oneline
Your last commit is gone from the branch. Now run:
git reflog
You will see a list of where HEAD has been. Find the hash before the reset. Then:
git reset --hard <hash>
Your commit is back. Reflog is local history. It is not a backup for a repo you have pushed to a shared server, and it does not last forever. But for everyday panic, it is the first place to look.
Drill 5: Break the Working Tree
Make a mess without committing.
printf 'uncommitted chaos\n' >> notes.txt
rm notes.txt
git status
Git tells you the file is deleted. You can restore it from the last commit:
git restore notes.txt
If you have unstaged changes you want to throw away, git restore is the command. If you have staged changes, use git restore --staged first. The point is to practice the difference between the working tree, the staging area, and the commit history. They are three different places, and most Git fear comes from mixing them up.
Make It a Habit
Do these drills once a month until the commands feel boring. Create a new sandbox each time. Break it. Fix it. Delete the folder. The goal is not to become a Git wizard. The goal is to stop treating every error message like a career-ending event.
Git is a tool for recording decisions. When you make a mess in a disposable repo, you are learning how those decisions are stored. That is how confidence gets built: not by avoiding mistakes, but by making them where they are cheap.