← Back to Git Course | Chapter 8: Best Practices | Lesson 4 of 6

Git Common Mistakes

Common Git mistakes are like tripping over the same rug: saving on the wrong branch, or pushing too soon. Knowing them helps you fix them without losing work.

Committing to the Wrong Branch

Committing straight to main when you meant to be on a feature branch is fixable without losing the work: create a new branch at your current commit, then reset main back to where it was before (git reset --hard origin/main) so the commit only lives on the new branch.

Example: Committing to the Wrong Branch

bash
git branch feature-x
git reset --hard HEAD~1
git checkout feature-x

Forgetting to Add a File

Realizing you forgot to git add a file after already committing doesn't require a whole new commit — stage the missing file and run git commit --amend --no-edit to fold it into the commit you just made, as long as you haven't pushed yet.

Example: Forgetting to Add a File

bash
git add forgotten-file.txt
git commit --amend --no-edit

Accidental Hard Reset

A git reset --hard that goes further back than intended feels catastrophic, but the commits usually aren't actually deleted — git reflog still records where your branch pointed a moment ago, so you can find the old commit hash and reset back onto it.

Example: Accidental Hard Reset

bash
git reflog
git reset --hard a1b2c3d

Fixing Messy States

When a merge or rebase leaves your branch in a state you don't understand and don't trust, the fastest way out is often not to keep fighting it — git reset --hard origin/<branch> throws away local confusion and realigns you with the last known-good state on the remote (at the cost of any uncommitted local work).

Example: Fixing Messy States

bash
git reset --hard origin/main

Untracking an Already Tracked File

Adding a file to .gitignore does nothing for a file Git is already tracking — ignore rules only apply to *untracked* files. You have to git rm --cached <file> first to stop tracking it, and only then does .gitignore actually keep it out of future commits.

Example: Untracking an Already Tracked File

bash
echo "config.local.json" >> .gitignore
git rm --cached config.local.json
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Committing on main by mistake and using git reset --hard before saving the work on a branch, losing the commits.
  2. Making a new commit for a forgotten file instead of using git commit --amend --no-edit, cluttering the history.
  3. Panicking after a wrong git reset --hard and assuming commits are gone, when git reflog usually shows how to recover them.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.