5 Git Mistakes Every Developer Makes (and the One-Line Fixes)

2026-10-20来源 dev.to WebDevEnglish

Git is easy until it isn't. Here are five mistakes everyone makes, and how to fix each in one line.

1. "I committed to the wrong branch"

git switch correct-branch git cherry-pick <commit-hash> git switch wrong-branch git reset --hard HEAD~1 

Copy the commit over, then remove it from the wrong branch.

2. "I need to undo my last commit"

Pick based on what you want to keep:

Command Commit Staged Files
git reset --soft HEAD~1 removed kept kept
git reset --mixed HEAD~1 removed removed kept
git reset --hard HEAD~1 removed removed deleted

Source: DataCamp (credit the original)

Already pushed? Use git revert <hash>. It adds a new commit instead of rewriting history.

3. "I lost my work after a bad reset"

You probably didn't. Git remembers where HEAD has been:

git reflog git reset --hard <hash-from-reflog> 

4. Merge or rebase?

Source: ByteByteGo (credit the original)

  • Merge: keeps full history, safe for shared branches.
  • Rebase: gives a clean linear history, but never rebase a branch others are using.

5. "I committed a secret or a huge file"

git rm --cached .env echo ".env" >> .gitignore git commit -m "Stop tracking .env" 

If it was pushed, rotate the key and scrub history with git filter-repo.

Cheat sheet

git stash # save work temporarily git stash pop # bring it back git commit --amend # fix the last commit message git restore <file> # discard changes to a file git log --oneline --graph # see history visually 

TL;DR

  • Wrong branch? cherry-pick + reset
  • Undo locally? reset. Undo pushed? revert
  • Lost something? git reflog
  • Shared branch? Merge, don't rebase

What's your worst Git horror story? Tell me in the comments. 👇

原文:https://www.a8q9.cn/en/article/5-git-mistakes-every-developer-makes-and-the-one-line-fixes-bcb508.html

相关文章