Git Reset

In the intricate landscape of version control, Git provides a powerful tool called git reset for rewriting history and managing the state of your repository. This command is versatile, offering various modes to unstage changes, move branches, and reset the commit history.

1. Soft Reset: Preserving Changes in Staging Area:

The soft reset is commonly used to uncommit the last commit while retaining changes in the staging area:

bash
git reset --soft HEAD^

This command moves the HEAD pointer to the previous commit (HEAD^), leaving the changes staged. You can then make additional modifications or commit with the amended changes.

2. Mixed Reset: Unstaging Changes:

The mixed reset is the default behavior of git reset and is often used to unstage changes while preserving them in the working directory:

bash
git reset HEAD file-name

Replace file-name with the name of the file you want to unstage. This command removes the file from the staging area, allowing you to rework the changes before staging again.

3. Hard Reset: Discarding Changes:

The hard reset is a powerful command that discards changes in both the working directory and the staging area:

bash
git reset --hard HEAD^

This command not only moves the HEAD pointer to the previous commit but also resets the working directory and staging area to match that commit. Use with caution, as it irreversibly discards changes.

4. Resetting to a Specific Commit:

To reset to a specific commit:

bash
git reset --hard <commit-hash>

Replace <commit-hash> with the hash of the commit to which you want to reset. This is useful for undoing commits and resetting the repository to a specific state.

5. Undoing a Merge or Pull:

To undo a merge or pull, you can use the --hard option with the commit hash before the merge:

bash
git reset --hard HEAD^

This command resets the repository to the state before the merge, effectively undoing the merge commit.

6. Interactive Reset: Selectively Undoing Commits:

To interactively choose which commits to reset:

bash
git reset -i HEAD~3

Replace 3 with the number of commits you want to interactively reset. This opens an interactive editor, allowing you to choose the reset mode for each commit.

7. Untracking Files:

To untrack files without deleting them, you can use:

bash
git reset file-name

This command removes the specified file from the staging area but leaves it in the working directory.

8. Soft Reset and Amend: Adding Changes to the Last Commit:

To add changes to the last commit:

bash
git commit --amend

This opens the commit message in the default editor, allowing you to append changes or modify the commit message. It effectively combines the changes with the last commit.

9. Keeping Changes in Staging Area:

If you want to keep changes in the staging area after a reset:

bash
git reset --mixed HEAD^

This is similar to the default mixed reset but explicitly specifies the --mixed option.

10. Unmodifying Files:

To unmodify files in the working directory:

bash
git reset --hard

This resets the working directory and staging area to match the last commit, effectively discarding all local modifications.

11. Resetting to a Remote Branch:

To reset a branch to match a remote branch:

bash
git reset --hard origin/master

Replace origin/master with the remote branch to which you want to reset. This is useful when you want to align your local branch with the remote branch.

12. Removing Untracked Files:

To remove untracked files from the working directory:

bash
git clean -fd

This command, combined with git reset --hard, removes untracked files and resets the working directory to match the last commit.