Scott Whittaker

Frontend Developer

Day job React | side projects Svelte

Git Reset

Some notes on how to reset stuff with git. There is a whole world of resetting with git, see the git-reset Documentation if you want to go deeper. The following are a few of the common day to day reset commands that I use for various scenarios.

Reset all local changes

When you have been messing around and want to reset all your local changes that have not yet been committed.

git reset --hard

Resetting the last local commit

Want to undo your last local commit? Forgot to remove some logging? Realised your commit is horrendous and you cannot bear to push it? Reset it. There is a great explanation in the Tower documentation on undoing the last commit.

Keep changes

Reset the last commit and keep the changes that you made.

git reset --soft HEAD~1

Alternatively you can drop the 1 here as ~ alone is a shortcut for ~1

git reset --soft HEAD~

Another alternative is to use HEAD^. The ^ alone is a shortcut for ^1.

git reset HEAD^

There is a good explanation here on the meaning and difference between the usage of the caret and tilde symbols.

Discard the last commit

Just get rid of the last commit entirely. Make sure you really don’t want to lose any changes you made.

git reset --hard HEAD~1

–hard Resets the index and working tree. Any changes to tracked files in the working tree since are discarded.

git-reset Documentation

Discard merge

Got into local merge trouble? Then bail out.

git reset --merge

Reset branch from origin

This one always catches me out for some reason. When someone elses PR you already have checked out has changed, if you pull that branch you may get conflicts. I do not want to have mess with conflicts here as it is none of my business, fetch everything then do a hard reset on the origin branch to get the latest changes.

git fetch --all
git reset --hard origin/<branch-name>