Common Git Mistakes & Fixes
Don't panic! Git is basically a giant undo button. Here is how to fix the 5 most common mistakes.
1. "I committed to the wrong branch!"
You were working on a new feature, but you forgot to run git checkout -b new-feature and accidentally committed directly to main!
The Fix:
# Move your commit to a new branch
git branch new-feature
# Reset main back by one commit (but keep the changes on new-feature)
git reset HEAD~1 --hard
# Switch to your new branch
git checkout new-feature 2. "Oh no, there's a typo in my last commit message!"
You wrote git commit -m "fxied login button". It happens to the best of us!
The Fix: Use the --amend flag.
git commit --amend -m "fixed login button" Note: Only do this if you haven't pushed to GitHub yet. If you have, you'll need to force push git push -f.
3. "I want to undo my last commit (but keep the code!)"
You committed too early and realized you still have one more file to add to that commit.
The Fix: Do a "soft" reset. This deletes the commit, but keeps the files modified on your computer.
git reset HEAD~1 --soft Now you can make your changes, run git add ., and commit again.
4. "My code is a total mess. Just delete my changes and go back to the last commit."
You tried a new approach, it failed spectacularly, and you just want to revert every file back to exactly how it was 30 minutes ago.
The Fix: Do a "hard" reset.
Warning: A hard reset permanently deletes any uncommitted changes. You cannot get them back.
git reset --hard HEAD 5. "I have merge conflicts and I'm terrified!"
You ran git pull and saw the dreaded: CONFLICT (content): Merge conflict in file.js
The Fix: Open the file in your code editor (like VS Code). You will see weird arrows in your code:
<<<<<<< HEAD
const color = "blue";
=======
const color = "red";
>>>>>>> incoming-branch VS Code normally gives you buttons above this block saying "Accept Current Change" or "Accept Incoming Change". Click the one you want, save the file, then run:
git add file.js
git commit -m "resolved merge conflict" You are ready!
Go back to the Issues page and start your open-source journey.