20 Troubleshooting
Even experienced developers forget a command or hit a weird error sometimes. You will mess up Git at some point. That’s part of the learning process!
Here’s how to recover from common problems and feel less stuck.
Common scenarios and fixes
“I made a change I don’t want anymore”
Scenario 1: you changed a file but haven’t committed it yet.
Run:
git restore filename.qmdThis resets the file to how it was in your last commit.
Scenario 2: you changed a file and already staged it (git add <filename>)
First, unstage it by running:
git restore --staged filename.qmdThen run:
git restore filename.qmd“I committed the wrong thing”
You can undo the most recent commit (without losing your changes):
git reset --soft HEAD~1Now your changes are back in the staging area. Fix what you need, and recommit.
If you want to undo the commit and all your changes:
git reset --hard HEAD~1⚠️ Warning: Using --hard will delete your work — only use this if you’re really sure!
“I forgot to add a file before committing”
If you made a commit but forgot a file you meant to include:
Step 1: Stage the missing file:
git add missed_file.qmdStep 2: Amend your last commit:
git commit --amendThis opens the Terminal’s default text editor, Vim, so you can update the message or leave it the same. Git will include the new file in the same commit.
After editing your message in Vim:
- Press
Escto make sure you’re not in insert mode. - Type
:wq(stands for write and quit). - Press
Enter/returnto save you changes and exit Vim.
“I can’t push because ‘tip of your current branch is behind’”
This means someone else pushed changes to GitHub before you.
To fix it:
git pull --rebaseThis fetches the new work and replays your changes on top of it. If there’s a conflict, Git will pause and ask you to resolve it. Once done, run:
git add . git rebase --continueThen push again:
git push