Skip to content

frdinkoi/MessyGit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 

Repository files navigation

The ultimate cheat sheet for fixing every Git mistake you will ever make.


πŸ” Quick Find (What did you mess up?)

πŸ“ Commits & Staging

🌿 Branches & Merging

☁️ Remote, Pushing & Pulling

🧰 Stashing & Tools



πŸ› οΈ The 43 Mistakes (And How to Fix Them)

🚨 Risk: Missing foundational knowledge and making avoidable mistakes.

πŸ› οΈ Fix:

  • Step 1: Read the official Git documentation and the Pro Git book.
  • Step 2: Learn core commands like git status, git add, git commit, git branch, git merge, git rebase, and git reflog.
  • Step 3: Practice on a small local repository before applying changes to shared production branches.

🚨 Risk: Immediate bugs in the production branch; hard to code review.

πŸ› οΈ Fix:

  • Step 1: Undo the commit while keeping your changes using git reset HEAD~1 --soft.
  • Step 2: Create and switch to a new feature branch using git checkout -b feature-branch.
  • Step 3: Commit your changes safely to the new branch using git commit -m "message".

🚨 Risk: Makes it impossible to understand the history of the project later.

πŸ› οΈ Fix:

  • Step 1: If you haven't pushed yet, run git commit --amend.
  • Step 2: Rewrite the message using a standard convention (e.g., Subject, Body, Ticket ID).
  • Step 3: Save and close the editor to update the commit.

🚨 Risk: Massive security breach.

πŸ› οΈ Fix:

  • Step 1: Immediately revoke or rotate the leaked API key on the provider's website.
  • Step 2: Install git-filter-repo (pip install git-filter-repo or via system package).
  • Step 3: Make a fresh mirror clone to get the full history using git clone --mirror https://github.com/user/repo.git.
  • Step 4: Scrub the secret by running git filter-repo --replace-text <(echo 'PASSWORD==>REDACTED').
  • Step 5: Force push the cleaned history using git push --force --tags --all.
  • Step 6: Tell everyone on your team to re-clone the repository.

🚨 Risk: A noisy, cluttered repository and a massive history size.

πŸ› οΈ Fix:

  • Step 1: Add the file or folder pattern (e.g., node_modules/) to your .gitignore file.
  • Step 2: Remove the files from Git's memory by running git rm -r --cached <folder-name>.
  • Step 3: Commit the removal (git commit -m "Clean up tracked files").

🚨 Risk: Bloated repository; makes cloning the project incredibly slow.

πŸ› οΈ Fix:

  • Step 1: Consider moving massive assets to external storage like S3. For remaining large files, install Git LFS using git lfs install.
  • Step 2: Tell Git LFS to track specific files, e.g., git lfs track "*.mp4".
  • Step 3: Run git add .gitattributes and commit it.
  • Step 4: Commit your newly tracked large files as usual.

🚨 Risk: Overwriting and permanently deleting your teammates' work.

πŸ› οΈ Fix:

  • Step 1: Stop using git push --force.
  • Step 2: Alias a safer alternative: echo "alias pushf='git push --force-with-lease'" >> ~/.bashrc && source ~/.bashrc.
  • Step 3: Always use pushf (or --force-with-lease), which cancels the push if someone else added new commits.
  • Step 4: Always communicate with your team before rewriting shared history.

🚨 Risk: Rewrites history that other developers are already depending on.

πŸ› οΈ Fix:

  • Step 1: If you already rebased and pushed a public branch, warn your team so they can reset their local repos.
  • Step 2: For future work, only run git rebase on local branches that you haven't pushed yet.
  • Step 3: Use git merge when integrating changes into public, shared branches.

🚨 Risk: Rejected pushes and messy merge conflicts.

πŸ› οΈ Fix:

  • Step 1: Run git pull --rebase to fetch remote changes and apply your local commits on top of them.
  • Step 2: If there are conflicts, resolve them in your code editor.
  • Step 3: Run git rebase --continue to finish.
  • Step 4: Safely push your code.

🚨 Risk: Broken code, missing features, or lost changes.

πŸ› οΈ Fix:

  • Step 1: Open the conflicting file in your code editor.
  • Step 2: Look for the <<<<<<<, =======, and >>>>>>> conflict markers.
  • Step 3: Carefully delete the markers and edit the code to combine both features correctly.
  • Step 4: Run git add <file> and git commit to finalize the merge.

🚨 Risk: Painful, massive merge conflicts when you finally try to merge.

πŸ› οΈ Fix:

  • Step 1: Switch to your main branch and run git pull frequently.
  • Step 2: Switch back to your feature branch.
  • Step 3: Run git rebase main (or git merge main) to keep your branch up to date.
  • Step 4: Repeat this process daily.

🚨 Risk: A noisy history filled with "wip" and "fixing typo" commits.

πŸ› οΈ Fix:

  • Step 1: Run git rebase -i HEAD~N (where N is the number of recent commits you want to clean).
  • Step 2: In the editor, change the word pick to squash (or s) for the minor commits.
  • Step 3: Save, then write a clean, single commit message for the merged work.

🚨 Risk: Accidentally staging unwanted files, secrets, or unfinished code.

πŸ› οΈ Fix:

  • Step 1: Type git status first to see exactly what changed.
  • Step 2: Use git add -p to review your changes chunk-by-chunk in the terminal.
  • Step 3: Type y to stage a chunk or n to skip it.

🚨 Risk: Anonymous commits or wrong author info showing up on GitHub.

πŸ› οΈ Fix:

  • Step 1: Run git config --global user.name "Your Name".
  • Step 2: Run git config --global user.email "your.email@example.com".
  • Step 3: Verify with git config --list.
  • Step 4: For per-repo overrides, run git config user.email "work@email.com" inside the project folder.

🚨 Risk: Constant merge conflicts and unnecessary repository bloat.

πŸ› οΈ Fix:

  • Step 1: Identify the generated folders (e.g., dist/, build/).
  • Step 2: Add those folders to your .gitignore file.
  • Step 3: Remove them from Git using git rm -r --cached <folder>.
  • Step 4: Rely on your CI/CD pipeline to generate these files automatically.

🚨 Risk: A cluttered, hard-to-read commit graph.

πŸ› οΈ Fix:

  • Step 1: While on your feature branch, run git rebase main instead of git merge main.
  • Step 2: Resolve any conflicts as they appear.
  • Step 3: Enjoy a clean, straight, linear history before you open a Pull Request.

🚨 Risk: Permanent data loss if you haven't committed or stashed your files.

πŸ› οΈ Fix:

  • Step 1: Do not panic. Run git reflog.
  • Step 2: Find the commit hash (e.g., HEAD@{1}) from right before you did the hard reset.
  • Step 3: Run git reset --hard <hash> to travel back in time and restore your files.

🚨 Risk: Accidentally removing unmerged work.

πŸ› οΈ Fix:

  • Step 1: If already deleted, run git reflog to find the last commit hash of that branch.
  • Step 2: Run git checkout -b <deleted-branch-name> <hash> to bring it back from the dead.
  • Step 3: In the future, verify the PR is merged on GitHub before running branch delete commands.

🚨 Risk: Lost or forgotten work that gets buried over time.

πŸ› οΈ Fix:

  • Step 1: Stop using plain git stash.
  • Step 2: Use git stash save "Descriptive message".
  • Step 3: Run git stash list occasionally to review your saved work.
  • Step 4: Run git stash pop stash@{n} to apply a specific stash.

🚨 Risk: Messing up your working code on the main branch.

πŸ› οΈ Fix:

  • Step 1: Run git checkout -b experimental-feature.
  • Step 2: Code and commit without fear.
  • Step 3: If the experiment fails, switch back to main and delete the branch (git branch -D experimental-feature).

🚨 Risk: Assuming your lost commits are permanently gone.

πŸ› οΈ Fix:

  • Step 1: Open your terminal and type git reflog.
  • Step 2: Scroll through the chronological list of every single action you took recently.
  • Step 3: Copy the hash of the state you want to return to.
  • Step 4: Run git checkout <hash> or git reset --hard <hash> to recover it.

🚨 Risk: Duplicate commits or out-of-order changes that break the build.

πŸ› οΈ Fix:

  • Step 1: If a cherry-pick causes massive conflicts, run git cherry-pick --abort.
  • Step 2: To cherry-pick safely, find the exact commit hash you need.
  • Step 3: Switch to your target branch.
  • Step 4: Run git cherry-pick <hash>.

🚨 Risk: Breaking the main branch for everyone else.

πŸ› οΈ Fix:

  • Step 1: Set up automated pre-commit checks on your server (GitHub Actions).
  • Step 2: Install a local pre-commit tool (Husky or Python pre-commit).
  • Step 3: Configure the tool to automatically run your linters and test suite locally.
  • Step 4: Only use git commit --no-verify as an absolute emergency escape hatch.

🚨 Risk: Submodules get stuck at the wrong commits.

πŸ› οΈ Fix:

  • Step 1: After pulling new code, check if your app fails to build.
  • Step 2: Run git submodule update --init --recursive to fetch the correct submodule data.
  • Step 3: Create an alias for this command if you use submodules frequently.

🚨 Risk: Hard to identify which commit matches which production release.

πŸ› οΈ Fix:

  • Step 1: Checkout the exact commit deployed to production.
  • Step 2: Run git tag -a v1.0.0 -m "Release version 1.0.0".
  • Step 3: Run git push origin v1.0.0 (or git push --tags).

🚨 Risk: Makes it impossible to revert a bug without reverting the new feature.

πŸ› οΈ Fix:

  • Step 1: Run git reset HEAD~1 --mixed to unstage the massive commit.
  • Step 2: Use git add to selectively stage only the files related to the bug fix.
  • Step 3: Commit the fix.
  • Step 4: Stage the remaining files and commit the feature separately.

🚨 Risk: Accidental merges that mess up your working directory.

πŸ› οΈ Fix:

  • Step 1: Stop running git pull blindly.
  • Step 2: Run git fetch origin to download remote data safely.
  • Step 3: Check the incoming changes using git log main..origin/main.
  • Step 4: If everything looks good, manually run git merge origin/main.

🚨 Risk: The secret is still accessible in older commits.

πŸ› οΈ Fix:

  • Step 1: Assume the secret is compromised and immediately change it on the platform.
  • Step 2: Install git-filter-repo.
  • Step 3: Make a fresh mirror clone: git clone --mirror https://github.com/user/repo.git.
  • Step 4: Scrub the secret: git filter-repo --replace-text <(echo 'PASSWORD==>REDACTED').
  • Step 5: Force push: git push --force --tags --all.

🚨 Risk: Noisy diffs where Git thinks you rewrote the entire file.

πŸ› οΈ Fix:

  • Step 1: Create a .gitattributes file in the root of your repository.
  • Step 2: Add * text=auto eol=lf to enforce LF line endings.
  • Step 3: Run git add --renormalize . to fix any existing files.
  • Step 4: Commit the updated files and .gitattributes.

🚨 Risk: Commits end up in unintended places.

πŸ› οΈ Fix:

  • Step 1: Run git reset HEAD~1 --soft to undo the commit but keep files staged.
  • Step 2: Run git stash to save the changes safely.
  • Step 3: Switch to the correct branch using git checkout correct-branch.
  • Step 4: Run git stash pop, then git commit.

🚨 Risk: Losing the original commit authorship and message history.

πŸ› οΈ Fix:

  • Step 1: Ensure the Pull Request description contains necessary context.
  • Step 2: Run git merge --squash <branch>.
  • Step 3: When Git prompts you for the commit message, manually list the co-authors.

🚨 Risk: Collaborators' incoming commits get erased from GitHub.

πŸ› οΈ Fix:

  • Step 1: If you erased code, ask your teammate to find their lost commit in git reflog.
  • Step 2: Have them push that specific commit back up.
  • Step 3: Alias your terminal to use git push --force-with-lease instead of --force.

🚨 Risk: Hard to bisect bugs or revert changes safely.

πŸ› οΈ Fix:

  • Step 1: Break your task down into logical steps before coding.
  • Step 2: Write code for one small piece.
  • Step 3: Commit that piece immediately.
  • Step 4: Move on to the next piece and repeat.

🚨 Risk: Unintended staging of files or complete data loss.

πŸ› οΈ Fix:

  • Step 1: Use --soft to undo a commit but keep files actively staged.
  • Step 2: Use --mixed (default) to undo a commit and unstage the files, keeping code in editor.
  • Step 3: Use --hard ONLY to completely destroy the local code and return to a previous state.

🚨 Risk: Accidental pushes to a company's main repo instead of a personal fork.

πŸ› οΈ Fix:

  • Step 1: Verify your remotes by running git remote -v.
  • Step 2: Never type just git push.
  • Step 3: Always specify the remote and branch: git push origin feature-branch.

🚨 Risk: Total uncertainty about your repository state.

πŸ› οΈ Fix:

  • Step 1: Make it a reflex to type git status before you run git add.
  • Step 2: Type git status again before git commit.
  • Step 3: Type git status one last time before git push.

🚨 Risk: Clutter, confusion, and a massive list of branches.

πŸ› οΈ Fix:

  • Step 1: Run git fetch -p && git branch -vv | grep gone to track down pruned branches.
  • Step 2: Review the list of dead branches.
  • Step 3: Delete them locally using git branch -d <branch-name>.

🚨 Risk: A revert might not apply cleanly to future history.

πŸ› οΈ Fix:

  • Step 1: Run git revert <hash>.
  • Step 2: Do not push immediately. Run your local server and test suite.
  • Step 3: Ensure the reversion didn't break surrounding code before pushing.

🚨 Risk: Accidental dropping or deleting of important commits.

πŸ› οΈ Fix:

  • Step 1: Create a backup branch by running git branch backup-branch.
  • Step 2: Perform your git rebase -i.
  • Step 3: If you mess up, simply run git reset --hard backup-branch to safely start over.

🚨 Risk: Incorrect credit, and CI checks might fail.

πŸ› οΈ Fix:

  • Step 1: If it's just the last commit, run git commit --amend --author="Name <email@example.com>".
  • Step 2: If multiple commits, use git rebase -i to edit the specific commits.
  • Step 3: Check your config (git config user.email) to prevent it happening again.

🚨 Risk: Deleting untracked files accidentally that you actually needed.

πŸ› οΈ Fix:

  • Step 1: Always run git clean -n -d first. This is a "dry run".
  • Step 2: Review the list of untracked files Git prints.
  • Step 3: Once you are 100% sure, run git clean -f -d to forcefully delete them.

🚨 Risk: Pushing sloppy code and wasting your team's time.

πŸ› οΈ Fix:

  • Step 1: Read your project's setup documentation.
  • Step 2: Run the required command (e.g., npm install) to enable hooks.
  • Step 3: Fix the formatting/linting errors locally instead of bypassing with --no-verify.

🚨 Risk: Irreversible mistakes when learning complex Git commands.

πŸ› οΈ Fix:

  • Step 1: Identify the dangerous command you are about to run.
  • Step 2: Create a safety net by running git branch safety-net.
  • Step 3: Run the command. If it destroys your code, git checkout safety-net and try again.

🚨 Risk: Unnecessary panic, crying, or giving up.

πŸ› οΈ Fix:

  • Step 1: Stop panicking when code disappears.
  • Step 2: Remember that Git almost never deletes commits instantly; it just hides them.
  • Step 3: Run git reflog, follow the breadcrumbs, and rescue your code.



Created for the open-source community. Star the repo if this saved your code! ⭐

About

complete cheat sheet of the 43 most common Git mistakes and the step-by-step terminal commands to fix them (git rebase, reset, reflog).

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors