Git 2 cheatsheet

Git 2: Undoing changes

8 common Git 2 tasks.

Discard changes to a file

Restore a file to its last committed state.

git restore file.txt

Caution: Uncommitted changes to the file are lost and cannot be recovered.

Unstage a file

Remove a file from the staging area but keep your edits.

git restore --staged file.txt

Undo the last commit, keep the changes

Move the branch back one commit and leave the changes staged.

git reset --soft HEAD~1

Undo the last commit and discard the changes

Move the branch back one commit and throw away its changes.

git reset --hard HEAD~1

Caution: Discards uncommitted work too. If you regret it, git reflog can usually find the lost commit.

Revert a commit safely

Create a new commit that undoes an earlier one. Safe on shared branches.

git revert abc1234

Find lost commits

List where HEAD has been, including commits no branch points to.

git reflog

Preview untracked files to delete

Show which untracked files and folders git clean would remove.

git clean -nd

Delete untracked files

Remove untracked files and folders.

git clean -fd

Caution: Deleted untracked files are not recoverable. Preview with git clean -nd first.

Was this page helpful?