ReadmeBuddy LogoReadmeBuddy
Back to Blog

🛠️ How to Fix Common Git Mistakes (With Commands)

readmebuddy
🛠️ How to Fix Common Git Mistakes (With Commands)

Introduction

Let’s face it: Git is powerful, but it can be intimidating. Whether you're a beginner or a seasoned developer, you’ve probably made a mistake in Git at some point—like committing the wrong file, pushing secrets, or dealing with a messy merge.

The good news? Most Git mistakes are fixable—and fast. Here are some of the most common Git blunders and the exact commands you need to fix them.

Common Git Mistakes and Fixes

❌ 1. Oops! Wrong Commit Message

git commit --amend -m "New, correct message"

Note: ⚠️ Use this only if you haven’t pushed the commit yet.

🗑️ 2. Accidentally Committed a File You Didn’t Want

git reset HEAD filename.txt

This removes the file from the staging area but keeps it on disk.

đź”™ 3. Undo the Last Commit (Without Losing Changes)

git reset --soft HEAD~1

Keeps changes staged. To unstage too:

git reset --mixed HEAD~1

đź’Ł 4. You Committed a Secret (like .env)

Remove from history:

git rm --cached .env
echo ".env" >> .gitignore
git commit -m "Remove .env and add to .gitignore"

To scrub it from history:

git filter-branch --force --index-filter \
"git rm --cached --ignore-unmatch .env" \
--prune-empty --tag-name-filter cat -- --all

(Note: git filter-branch is deprecated; consider using git filter-repo for large projects.)

🔀 5. Merge Conflict Panic

  • If you hit a conflict:
    • Edit files to resolve conflict markers.
    • Then run:
      
      

    git add .
    git commit```

  • To cancel the merge:
    git merge --abort```
    

🔄 6. Pushed to the Wrong Branch

git push origin main --force
git checkout dev
git push origin dev```
*Warning:* Be careful with `--force` on shared branches!

### đź§Ż 7. Deleted a Branch by Mistake?
- If it was local:
    ```bash
git reflog
git checkout -b feature-branch <commit_hash>```
*Note:* Always double-check before deleting branches.

## Conclusion
Making mistakes in Git is part of the learning process. The key is knowing how to recover. With the commands in this guide, you’ll be able to fix most issues quickly and confidently—without breaking your repo or workflow.

Want to automate parts of your Git setup? Try out [ReadmeBuddy](https://readmebuddy.com/) — it helps you generate high-quality README files for your repos in seconds.

## Want More?
Stay tuned to [readmebuddy.com/posts](https://readmebuddy.com/posts) for more Git tutorials, developer tools, and productivity hacks.