Digital Garden
Computer Science
Git
Common Problems

Common Problems

There are some common problems that you may encounter when using Git that my basic guide does not cover. Here are some solutions to those problems.

Undo Unstaged Local Changes

To overwrite local changes:

git checkout -- <file>

To discard local changes to all files, permanently:

git reset --hard

Undo Staged Local Changes

To unstage the file but keep your changes:

git restore --staged <file>

To unstage everything but keep your changes:

git reset

To discard local changes to all files, permanently:

git reset --hard

Undo Committed Local Changes

This is a always a bit harder... To undo last two commits, but keep your changes:

git reset HEAD~2

To undo last two commits, discard changes:

git reset --hard HEAD~2

Change Commit

To change the message of the last commit:

git commit --amend -m "New message"

Did you forget to add a file? Just add it and amend the previous commit :)

git add forgotten_file
git commit --amend