Tokenova Learn

Office-Ready Git · provided by Tokenova

"Oh no!" Git rescue guide

Something went wrong? Stay calm: almost everything in Git can be undone. Start with the question that matches your situation, then follow the links.

Open the interactive version

What happened?

Throw away changes you haven't committed

How you get here

  1. What happened? I changed files and want to throw the changes away

git restore puts files back to how they were in the last commit. If you already staged a change with git add, unstage it first: git restore --staged transform.py. New files Git has never tracked need git clean. Always preview first: -nd lists the files and folders that would be deleted (plain -n skips folders).

git restore transform.py
git restore .
git clean -nd
git clean -fd

Open this fix in the interactive guide

Take a file out of the staging area

How you get here

  1. What happened? I staged something I shouldn't have

git restore --staged unstages the file but keeps your edits. If the file should never be in Git (like .env), stop tracking it and ignore it.

git restore --staged transform.py
git rm --cached .env
git status

Open this fix in the interactive guide

Fix the last commit (not pushed yet)

How you get here

  1. What happened? Something is wrong with my last commit
  2. Have you pushed that commit? No, it's only on my laptop

Add a forgotten file, or fix the message. --amend replaces the last commit with a corrected one. To take the commit back entirely but keep its changes, use a soft reset.

git add report.py
git commit --amend --no-edit
git commit --amend -m "Add supplier filter to report"
git reset --soft HEAD~1

Open this fix in the interactive guide

Undo a commit that's already pushed

How you get here

  1. What happened? Something is wrong with my last commit
  2. Have you pushed that commit? Yes, it's on GitHub
  1. What happened? I want to undo a commit
  2. Have you pushed the commit you want to undo? Yes, it's on GitHub

git revert adds a new commit that does the opposite of the old one. History isn't rewritten, so it's safe on shared branches. To undo a merged pull request, use the Revert button on the PR, or git revert -m 1 <merge id>. Only a small mistake, such as a forgotten file? Skip the revert and fix it in a new commit.

git log --oneline
git revert a1b2c3d
git push

Open this fix in the interactive guide

Move local commits to a new branch

How you get here

  1. What happened? I committed on the wrong branch
  2. Have you pushed it? No, it's only on my laptop

You committed on main but meant to start feature/csv-export. Put a branch label where you are (it keeps the commits), move main back to match GitHub, then switch to the new branch.

git branch feature/csv-export
git reset --hard origin/main
git switch feature/csv-export

Open this fix in the interactive guide

A pushed commit landed on the wrong branch

How you get here

  1. What happened? I committed on the wrong branch
  2. Have you pushed it? Yes, it's on GitHub

Copy the commit to where it belongs, then undo it where it doesn't with a revert. Nobody's history gets rewritten. If it went to main, tell your team.

git switch feature/csv-export
git cherry-pick a1b2c3d
git push
git switch main
git revert a1b2c3d
git push

Open this fix in the interactive guide

Undo local commits

How you get here

  1. What happened? I want to undo a commit
  2. Have you pushed the commit you want to undo? No, it's only on my laptop

Move your branch back. How much you keep depends on the mode:

  • --soft: undo the commit, keep the changes staged.
  • default (--mixed): undo the commit, keep the changes unstaged.
  • --hard: undo the commit and throw the changes away.
git reset --soft HEAD~1
git reset HEAD~1
git reset --hard HEAD~1

Open this fix in the interactive guide

Someone pushed before you

How you get here

  1. What happened? My push was rejected
  2. What does the rejection say? (fetch first) or (non-fast-forward)

GitHub has commits you don't have yet. Bring them in first, then push. --rebase replays your commits on top of theirs, which keeps the history straight.

git pull --rebase
git push

Open this fix in the interactive guide

The branch doesn't exist on GitHub yet

How you get here

  1. What happened? My push was rejected
  2. What does the rejection say? has no upstream branch

Your branch lives only on your laptop. -u creates it on GitHub and remembers the link, so from then on plain git push and git pull work.

git push -u origin feature/7-supplier-filter

Open this fix in the interactive guide

GitHub doesn't accept your sign-in

How you get here

  1. What happened? My push was rejected
  2. What does the rejection say? Permission denied, 403 or Authentication failed

Check which remote you're using and which account you're signed in as. With HTTPS, Git Credential Manager opens a browser sign-in, so choose the account that has access. With SSH, the test command should greet you by username. Company repos behind SSO need your key or token authorised for the organization.

git remote -v
ssh -T git@github.com

Open this fix in the interactive guide

You can't push straight to main

How you get here

  1. What happened? My push was rejected
  2. What does the rejection say? GH006: Protected branch or GH013: Repository rule violations

main is protected, so changes must go through a pull request. Move your commits to a branch, put main back, then push the branch and open a PR.

git branch feature/my-change
git reset --hard origin/main
git switch feature/my-change
git push -u origin feature/my-change

Open this fix in the interactive guide

A file is too big for GitHub

How you get here

  1. What happened? My push was rejected
  2. What does the rejection say? GH001: Large files detected

GitHub rejects files over 100 MB. If the big file is in commits you haven't pushed yet, take it out of those commits, ignore it, and push again. The example assumes it's only in your last commit.

git reset --soft HEAD~1
git rm --cached data/export.parquet
git commit -m "Add export step (data file ignored)"
git push

Open this fix in the interactive guide

Your force-with-lease was refused

How you get here

  1. What happened? My push was rejected
  2. What does the rejection say? (stale info)

Someone pushed to your branch after your last fetch. The lease protected their work. Fetch, look at what's new, bring it into your branch, and try again. The log also lists your own old commits, the ones you rewrote: your teammate's are the ones you don't recognise.

git fetch
git log --oneline HEAD..origin/feature/export-csv
git cherry-pick b2c3d4e
git push --force-with-lease

Open this fix in the interactive guide

Get out of a merge, rebase, cherry-pick or revert

How you get here

  1. What happened? I'm stuck in a merge, rebase or cherry-pick

The first lines of git status tell you which operation is in progress. Each one has an abort that puts everything back to how it was before you started. To finish instead of going back: fix the conflicted files, git add them, then run the same command with --continue in place of --abort.

git status
git merge --abort
git rebase --abort
git cherry-pick --abort
git revert --abort

Open this fix in the interactive guide

Find lost commits with the reflog

How you get here

  1. What happened? I lost commits or deleted a branch

Git records every place HEAD has been. Commits that no branch points to any more are kept for at least 30 days by default. Find the commit in the reflog, then put a branch label back on it, using the HEAD@{n} from its line. To move the branch you're on back to where it was before your last command (for example after a wrong git reset --hard), reset to 'HEAD@{1}' instead. Deleted a branch? git branch -D printed its last id (was a1b2c3d): git branch feature/x a1b2c3d brings it back.

git reflog
git branch rescue/lost-work 'HEAD@{2}'
git reset --hard 'HEAD@{1}'

Open this fix in the interactive guide

Remove a secret before anyone sees it

How you get here

  1. What happened? I committed a password or secret
  2. Have you pushed it to GitHub? No, it's only on my laptop

If it's only in your last commit, take the commit back, untrack the file, ignore it, and commit again. Check the whole diff before you push.

git reset --soft HEAD~1
git rm --cached .env
git commit -m "Add warehouse loader"
git show --stat HEAD

Open this fix in the interactive guide

A secret was pushed: rotate first

How you get here

  1. What happened? I committed a password or secret
  2. Have you pushed it to GitHub? Yes, it's been pushed
  1. Revoke or change the secret right now (new password, delete the token). Treat it as leaked from the moment it was pushed.
  2. Stop tracking the file and ignore it: git rm --cached .env, add it to .gitignore, commit and push.
  3. Tell your lead or security team. Removing it from history needs a coordinated rewrite (for example git filter-repo). Everyone who cloned still has the old copy.
  4. GitHub's secret scanning may already have sent an alert.
git rm --cached .env
git commit -m "Stop tracking .env"
git push

Open this fix in the interactive guide

You're on a commit, not a branch

How you get here

  1. What happened? Git says I'm in 'detached HEAD'

"Detached HEAD" means you're looking at a specific commit instead of a branch. Just looking is harmless: leave with git switch main, or with git switch - to go back to the branch you were on before. If you made commits here that you want to keep, put a branch on them before you leave: git switch -c rescue/my-work. Already left without one? Git printed the id of the commit you left behind, and git reflog still lists it: git branch rescue/my-work <id> brings it back.

git switch -c rescue/my-work
git switch main

Open this fix in the interactive guide

Put unfinished work on a shelf

How you get here

  1. What happened? I need to switch branches but my work isn't finished

Stash saves your uncommitted changes and cleans your working folder, so you can switch branches. Bring them back later with pop. New files that were never added need -u.

git stash push -u -m "currency wip"
git switch main
git switch -
git stash pop

Open this fix in the interactive guide

Get out of Vim, then never see it again

How you get here

  1. What happened? Git opened a strange black editor (Vim)

Git opened Vim because no editor is set. Press Esc, type :wq and press Enter to save the message and continue. Then make VS Code your Git editor. From then on, Git opens a VS Code tab, and you close the tab when you're done.

git config --global core.editor "code --wait"

Open this fix in the interactive guide