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).
What happened? Something is wrong with my last commit
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.
What happened? Something is wrong with my last commit
Have you pushed that commit? Yes, it's on GitHub
What happened? I want to undo a commit
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.
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.
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.
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.
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.
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.
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
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.
Have you pushed it to GitHub? Yes, it's been pushed
Revoke or change the secret right now (new password, delete the token). Treat it as leaked from the moment it was pushed.
Stop tracking the file and ignore it: git rm --cached .env, add it to .gitignore, commit and push.
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.
GitHub's secret scanning may already have sent an alert.
"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.
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
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.