Skip to content

Git reset and commit history manipulation

Git reset is a powerful command used to undo changes, reset the current state to a specific point in history, or manipulate the commit history.^[600-developer__tools__git__git-command.md] Depending on the flags used, it can alter the staging index, the working directory, or move the branch pointer itself.

Resetting to a specific commit

To reset the current branch pointer to a specific commit, use the commit hash.

git reset 09d528c^[600-developer__tools__git__git-command.md]

This moves the branch pointer without modifying the working directory by default. To return the working directory to the clean state of that commit (discarding all local changes), use the --hard flag:

git reset --hard 09d528c^[600-developer__tools__git__git-command.md]

Undoing the last commit

There are several ways to undo or modify the most recent commit, depending on whether you want to keep the changes or delete them entirely.

Modifying the last commit

If you need to add forgotten files to the previous commit or correct the commit message:

  • git commit --amend: Modifies the commit message of the last commit^[600-developer__tools__git__git-command.md].
  • git commit --amend [files...]: Adds specified files to the last commit^[600-developer__tools__git__git-command.md].

Soft reset (Keep changes)

To undo the commit action but keep the file modifications in the staging area:

git reset HEAD^ --soft^[600-developer__tools__git__git-command.md]

This removes the commit but leaves the changes ready to be re-committed.

Hard reset (Discard changes)

To completely remove the last commit and revert the working directory to the state it was in before that commit (a "clean" state):

git reset HEAD^ --hard^[600-developer__tools__git__git-command.md]

Discarding local modifications

If the working directory has become messy and you want to revert to the exact state of the most recent commit, discarding all uncommitted changes:

git reset --hard^[600-developer__tools__git__git-command.md]

Syncing with Remote

To force the local branch to match the remote branch exactly (effectively a "hard pull"):

git reset --hard origin/master^[600-developer__tools__git__git-command.md]

Conversely, to force the remote branch to match the local history after a rewrite:

git push --force^[600-developer__tools__git__git-command.md]

Compressing history (Rebase)

To combine multiple commits into a single one (squashing), an interactive rebase is used:

git rebase -i [commit_hash]^[600-developer__tools__git__git-command.md]

This opens an editor where you can change the action of subsequent commits from pick to squash to merge them into the parent commit^[600-developer__tools__git__git-command.md].

Reverting to the latest after a reset

If you have reset to an older version and wish to return to the most recent commit state, you can reset again using the latest commit ID:

git reset --hard [commit_id]^[600-developer__tools__git__git-command.md]

Sources

^[600-developer__tools__git__git-command.md]

  • [[Git branches]]
  • [[Git remote]]