About this chapter
This is the chapter you'll come back to when something goes wrong. Every lesson is built
around a real problem you will hit and the exact phrase you'll type into Google: a
.gitignore that stubbornly won't ignore a file, a last commit you want to undo, the
scary "detached HEAD" message, commits that landed on the wrong branch, and a secret or
huge file you accidentally committed and now need out of history.
Each one states the problem first, gives you the fix, then explains what actually happened so you understand it instead of just copying commands. The final lesson is a capstone: a practical, everyday Git workflow that pulls together everything from the whole course - branching, committing, pushing, pull requests, and undoing mistakes - for both solo work and teams.
Lessons from courses
- 1 gitignore not working Your .gitignore is not working because the file is already tracked by Git. Fix it with git rm --cached, then commit. Full explanation and examples.
- 2 Undo last commit Undo your last Git commit: keep the changes with git reset --soft, discard them with git reset --hard, or safely undo a pushed commit with git revert.
- 3 Detached HEAD What 'You are in detached HEAD state' means in Git, how you ended up there, and how to get back to a branch with git switch - keeping or discarding your work.
- 4 Committed to the wrong branch Committed to main instead of a feature branch? Move the commit onto a new branch with git switch -c, then reset main back - safe, step-by-step fix.
- 5 Remove a file from Git history Committed a secret or huge file? git rm --cached fixes the future; to purge it from all history use git filter-repo or BFG. Rotate the secret and force-push.
- 6 A sensible Git workflow A practical everyday Git workflow for beginners, solo or on a team: main plus feature branches, small commits, pull before push, pull requests, merge vs rebase.