The git reset
command in Git is used to reset your repository to a specific state. This can be useful for undoing changes, removing commits, or even discarding local changes entirely. Let’s explore how you can utilize this command effectively.
Example 1: Resetting to a Commit
To reset your repository to a specific commit, use:
git reset --hard
Replace
with the actual commit hash you want to reset to. This command resets the index and working tree, discarding all changes since that commit.
Verification: After executing the command, use git log
to verify that the repository is now at the specified commit.
Example 2: Soft Reset
Performing a soft reset keeps your changes in the working directory but resets the index. Use:
git reset --soft
This command is useful when you want to undo commits but keep your changes for further editing.
Verification: Check the status with git status
to see staged changes ready to be committed.
Example 3: Mixed Reset
A mixed reset resets the index to match the specified commit while keeping your working directory unchanged. Execute:
git reset --mixed
This allows you to unstage changes that you mistakenly added to the index.
Verification: Use git diff
to see changes that are not staged for commit after the reset.
Example 4: Unstaging Changes
To unstage changes from the index (but keep them in the working directory), use:
git reset
Replace
with the name of the file you want to unstage.
Verification: Check with git status
to ensure the file is no longer staged for commit.
Example 5: Resetting a Single File
If you only want to discard changes in a specific file, you can use:
git checkout --
This reverts the file to the state it was in at the last commit.
Verification: Inspect the file to see that it matches the version in the last commit.
Example 6: Resetting to HEAD
To reset back to the last commit on the current branch, use:
git reset --hard HEAD
This is useful for undoing local changes and resetting everything to the latest commit.
Verification: Check git log
to ensure the HEAD is now at the latest commit.
Example 7: Undoing a Merge
If you need to undo a merge and reset the branch to the state before the merge, use:
git reset --hard HEAD^
This command moves the branch pointer back to the commit before the merge commit.
Verification: Use git log
to verify the branch history and ensure the merge commit is removed.
Example 8: Resetting to a Tag
To reset to a specific tag, use:
git reset --hard
Replace
with the name of the tag you want to reset to.
Verification: Check with git tag
and git log
to confirm the reset to the tagged commit.
Example 9: Removing Untracked Files
To remove untracked files from your working directory, use:
git clean -f
Be cautious as this permanently deletes untracked files.
Verification: Check with git status
to ensure untracked files are removed.
Example 10: Interactive Mode Reset
For more control over what gets reset, use interactive mode:
git reset -i
This allows you to choose individual commits or changes to reset interactively.
Verification: Follow the prompts and check the repository state using git status
after making selections.
Discussion about this post