Git Emergency

I forgot to pull before pushing

Severity Minor

What just happened

Colleagues pushed while you were working. Your branch and the remote now each have commits the other doesn't. Git refuses to push so it doesn't erase their work: you need to integrate their commits first. Nothing is lost.

Potential risks

  • The tempting git push --force would delete their commits from the remote.
  • If you both changed the same lines, you'll get conflicts to resolve.

Before you run anything

  • Never force push to get out of this.
  • Commit (or stash) your current work before pulling.
  • Golden rule: before any destructive command, create a safety branch with git branch backup/before-fix — it costs nothing.

Recovery steps

  1. 1

    See what you missed

    fetch downloads without changing anything. The log lists their commits that you don't have yet.

    Safe
    $ git fetch origin
    $ git log --oneline HEAD..origin/main
  2. 2

    Put your commits on top of theirs, then push

    --rebase replays your commits after theirs: a clean, linear history, no merge commit.

    Caution
    $ git pull --rebase
    $ git push
  3. 3

    Conflicts during the rebase?

    Fix the files, add them, continue. Lost? --abort puts everything back as before the pull.

    Safe
    $ git status
    $ git add path/to/file
    $ git rebase --continue
    # or
    $ git rebase --abort
  4. 4

    Already pulled and got a “Merge branch 'main' of…” commit?

    Not pushed yet and you prefer a clean history: undo that merge, then pull with rebase. (Keeping the merge commit is fine too: just push.)

    Destructive
    $ git reset --hard ORIG_HEAD
    $ git pull --rebase
  5. 5

    So it never happens again

    Make pull rebase by default, and pull before starting a task and before pushing.

    Safe
    $ git config --global pull.rebase true
    $ git config --global rebase.autoStash true

These procedures are provided for information only. Read each command and adapt it to your situation before running it. Disclaimer

Other disasters

🪓 I deleted my branch🚨 I pushed directly to main💥 I ran git reset --hard🧨 I overwrote a colleague's commits🔑 I pushed my .env file to GitHub🔀 I made a bad merge👻 I lost uncommitted changes🎯 I committed on the wrong branch🎈 I'm in “detached HEAD” state⚔️ I'm stuck in a merge conflict🚫 My push is rejected (non-fast-forward)🌀 My rebase went wrong🐘 GitHub rejects a file over 100 MB🥸 I committed with the wrong name or email🗑️ I deleted a file and committed it⏪ I want to undo my last commit✏️ I amended a commit that was already pushed🧹 Too many messy commits to clean up📮 I pushed to the wrong branch🧭 I rebased onto the wrong branch🔁 I need back the changes I reverted📦 git stash pop gave me a conflict🙈 I committed a file that should be ignored🔠 Git doesn't see my file rename (upper/lower case)🧨 I ran git clean -fd by mistake🩻 My repository is corrupted👯 Duplicated lines of code after a merge➖ I added a file I didn't want to (git add)✏️ I made a typo in a commit message✂️ My commit does too many things🧺 I lost a stash (drop, clear or pop gone wrong)🫥 I pulled and my code disappeared🍒 I cherry-picked the wrong commit☁️ I deleted a branch on GitHub / GitLab👥 I pushed with the wrong GitHub account🚚 The repository was renamed or moved🏷️ I tagged the wrong commit🌊 Git says every file is modified, but I changed nothing📦 I committed node_modules (or a build folder)📂 I ran git init in the wrong folder🧨 My pull request has conflicts⏳ My pull request is behind main📝 I have to apply review changes to my PR🧳 My PR shows commits that aren't mine🎯 I opened my PR against the wrong branch🚨 A merged PR broke main🚪 I closed my PR (or deleted its branch) by mistake🔴 The checks of my PR are failing🐘 My PR is too big to review🍴 My fork is behind the original repository