π Commits & Staging
- I committed directly to main/master
- I wrote a bad commit message
- I accidentally committed a secret/password!
- I accidentally committed a massive folder / ignored .gitignore
- I used
git add .and staged things I shouldn't have - I forgot to set my username/email
πΏ Branches & Merging
- I deleted a branch and need it back!
- I need to undo a hard reset (
git reset --hard) - I messed up a merge conflict
- I committed to the wrong branch
- I have a massive feature branch that is out of sync
βοΈ Remote, Pushing & Pulling
- I force-pushed and overwrote my teammate's code
- I rebased a public branch by accident
- My push was rejected
- I pushed to the wrong remote (origin vs upstream)
π§° Stashing & Tools
- I stashed my code and forgot where it went
- I need to clean up untracked files without deleting important stuff
- I don't know how to recover my code (Using Git Reflog)
π¨ 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, andgit 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-repoor 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.gitignorefile. - 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 .gitattributesand 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 rebaseon local branches that you haven't pushed yet. - Step 3: Use
git mergewhen integrating changes into public, shared branches.
π¨ Risk: Rejected pushes and messy merge conflicts.
π οΈ Fix:
- Step 1: Run
git pull --rebaseto 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 --continueto 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>andgit committo 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 pullfrequently. - Step 2: Switch back to your feature branch.
- Step 3: Run
git rebase main(orgit 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
picktosquash(ors) 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 statusfirst to see exactly what changed. - Step 2: Use
git add -pto review your changes chunk-by-chunk in the terminal. - Step 3: Type
yto stage a chunk ornto 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
.gitignorefile. - 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 maininstead ofgit 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 reflogto 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 listoccasionally 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>orgit 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-verifyas 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 --recursiveto 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(orgit push --tags).
π¨ Risk: Makes it impossible to revert a bug without reverting the new feature.
π οΈ Fix:
- Step 1: Run
git reset HEAD~1 --mixedto unstage the massive commit. - Step 2: Use
git addto 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 pullblindly. - Step 2: Run
git fetch originto 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
.gitattributesfile in the root of your repository. - Step 2: Add
* text=auto eol=lfto 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 --softto undo the commit but keep files staged. - Step 2: Run
git stashto save the changes safely. - Step 3: Switch to the correct branch using
git checkout correct-branch. - Step 4: Run
git stash pop, thengit 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-leaseinstead 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
--softto 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
--hardONLY 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 statusbefore you rungit add. - Step 2: Type
git statusagain beforegit commit. - Step 3: Type
git statusone last time beforegit push.
π¨ Risk: Clutter, confusion, and a massive list of branches.
π οΈ Fix:
- Step 1: Run
git fetch -p && git branch -vv | grep goneto 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-branchto 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 -ito 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 -dfirst. 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 -dto 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-netand 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.
