Edits in files, not yet committed
| Command | What it does |
|---|---|
git restore app.yml |
restore the file as in the last commit; the edit is lost |
git restore -p app.yml |
restore piece by piece, with a prompt for each |
git restore --staged app.yml |
take it out of the index; the edit in the file stays |
git restore --source=HEAD~3 app.yml |
take the file from three commits back |
git show HEAD~3:app.yml |
show the old version without touching the file |
git stash -u |
put all edits and new files aside |
git stash pop |
bring back what was put aside |
git clean -n |
which untracked files would be deleted — without deleting |
git clean -fd |
delete untracked files and directories; -x ignored ones too |
git clean -fdx in a server’s /opt deletes .env, databases and acme.json — everything in .gitignore (Server configs in git).
The last commit, not yet pushed
| Command | What it does |
|---|---|
git commit --amend -m "the right message" |
fix the message |
git add forgotten.yml && git commit --amend --no-edit |
add a forgotten file to the same commit |
git reset --soft HEAD~1 |
undo the commit; the edits stay in the index |
git reset HEAD~1 |
undo the commit; the edits stay in the files |
git reset --hard HEAD~1 |
undo the commit and throw away the edits |
--amend and reset rewrite history. While the commit is only yours, that’s fine. After a push, see the next group.
reset mode |
Branch | Index | Files |
|---|---|---|---|
--soft |
moves | unchanged | unchanged |
--mixed (the default) |
moves | as in the commit | unchanged |
--hard |
moves | as in the commit | as in the commit |
The commit is already on GitHub
| Command | What it does |
|---|---|
git revert 9f8e7d |
a new commit that undoes the old one; history isn’t rewritten |
git revert --no-edit HEAD |
undo the last one with a ready Revert "…" message |
git revert -m 1 3e2d1c |
undo a merge: -m 1 keeps the main side |
git push --force-with-lease |
if you did rewrite your own — overwrite, but only if nobody pushed on top (push --force-with-lease instead of --force) |
revert is the only way that doesn’t break history for people who’ve already pulled.
Lost after a reset, a rebase, a deleted branch
| Command | What it does |
|---|---|
git reflog -10 |
where HEAD has been: every commit, reset, rebase, switch |
git reset --hard HEAD@{1} |
put the branch back where it was one step ago |
git switch -c rescue 6c17cca |
save a found commit as a new branch |
git branch feature 7c6b5a |
restore a deleted branch by a hash from reflog |
git rebase --abort |
abandon a rebase halfway — back to the start |
git merge --abort |
the same for a merge |
git fsck --lost-found |
find commits not even in reflog — a dropped stash, for example |
What it looks like:
git reset --hard HEAD~2
# HEAD is now at 0df0548 other2
git reflog -3
# 0df0548 HEAD@{0}: reset: moving to HEAD~2
# 6c17cca HEAD@{1}: pull --rebase (finish): returning to refs/heads/master
# 6c17cca HEAD@{2}: pull --rebase (pick): site2
git reset --hard 'HEAD@{1}'
# HEAD is now at 6c17cca site2
Git keeps commits nothing points to for 30 days (gc.reflogExpireUnreachable), then git gc deletes them. In PowerShell,
quote HEAD@{1} — otherwise the shell eats the braces.
Committed in the wrong place
| Command | What it does |
|---|---|
git switch -c traefik-timeouts |
the commits went into main but belonged in a branch: now the branch has them |
git switch main && git reset --hard origin/main |
put main back to GitHub’s state — the commits stay in the new branch |
git cherry-pick 9f8e7d |
copy one commit into the current branch |
git reset --hard origin/main |
throw away everything of yours on the branch and take GitHub’s |
A secret in a commit
A password, key or token got into a commit — first change the secret itself, then clean the history: A secret in a commit.