Git Revert and Reset

Understand how to revert (undo) changes made in commits using 'git revert'. Learn the different types of reset (soft, mixed, hard) and their effects using 'git reset'.


Git Reset - Soft Mode

What is Git Reset?

git reset is a powerful command in Git that allows you to move the current branch's HEAD to a specified commit. It's essentially a way to undo changes or rewrite history, within your local repository.

Git Reset - Soft Mode Explained

The git reset --soft command is a variant of git reset that moves the HEAD of the current branch to the specified commit, but preserves the changes in both the staging area and the working directory. Think of it as "undoing" the commit(s) specified while keeping all your work intact, ready to be committed again (perhaps with modifications).

Detailed Explanation of git reset --soft

Here's a breakdown of what happens when you use git reset --soft:

  1. HEAD Movement: The HEAD of your current branch is moved to the specified commit. This effectively makes it as if that commit (and any commits that followed it) never happened.

  2. Staging Area: Crucially, the staging area (also known as the index) remains unchanged. This means all the files that were staged in the commits you "undid" are still staged.

  3. Working Directory: The working directory also remains unchanged. All the changes you made in those commits are still present in your files.

In simpler terms:git reset --soft essentially tells Git, "Forget that I committed these changes, but keep them ready for me to re-commit."

Effect on the Staging Area and Working Directory

Staging Area: Files that were staged in the undone commits remain staged. This is a key characteristic of the soft reset. It allows you to quickly re-commit the changes, or modify them before re-committing.

Working Directory: Files in the working directory are also preserved. This means that all your changes are still present in your files. This can be useful if you want to make further changes before re-committing.

Example

Imagine you have three commits: A, B, and C. The HEAD of your branch is currently pointing to commit C.

 git reset --soft HEAD~2  # or git reset --soft A 

This command will move the HEAD to commit A. Commits B and C are effectively "undone." However, the changes that were made in commits B and C are still staged and present in your working directory.

Common Use Cases for git reset --soft

  • Undoing a recent commit to modify the commit message: If you made a typo in your commit message, you can use git reset --soft HEAD^ to undo the last commit, correct the message, and re-commit.
  • Combining multiple commits into a single commit: You can use git reset --soft to undo several commits, then stage all the changes and create a single, more comprehensive commit.
  • Breaking up a large commit into smaller, more logical commits: If you realize a commit is too large, you can use git reset --soft to undo the commit, then selectively stage and commit smaller chunks of the changes.

Important Considerations

While git reset --soft is generally safe (as it doesn't lose any data), it's still important to understand what it does before using it. Always double-check the commit you're resetting to. Avoid using git reset on commits that have already been pushed to a remote repository that others are using, as it can cause conflicts and make collaboration difficult.