Skip to main content

Undo the most recent local commits in Git - Explained with commands and example

Undoing the most recent local commits in Git is a common task that can be accomplished in several ways. Here are some of the most common methods, explained in detail.


Undo the most recent local commits in Git - Explained with commands and example


Undoing the last commit while keeping changes

If you want to undo the last commit while keeping the changes you made, you can use the git reset --soft HEAD^ command. This will reset the branch to the previous commit, but keep the changes you made in the staging area. You can then make any additional changes you need and commit them again.

To illustrate this method, let's say we have a repository with a file called example.txt that we want to modify. We make some changes to the file and commit them using the git commit -m "Initial commit" command. We then realize that we made a mistake and want to undo the commit while keeping the changes we made.

To do this, we can use the git reset --soft HEAD^ command. This will reset the branch to the previous commit, but keep the changes we made in the staging area. We can then make any additional changes we need and commit them again.

Here's a step-by-step guide:

  • First, we make some changes to the example.txt file and commit them using the git commit -m "Initial commit" command:

Initial commit

  • Next, we realize that we made a mistake and want to undo the commit while keeping the changes we made. We use the git reset --soft HEAD^ command:

Reset command

  • The git reset --soft HEAD^ command resets the branch to the previous commit, but keeps the changes we made in the staging area. We can now make any additional changes we need and commit them again:

Additional changes

Undoing the last commit and discarding changes

If you want to undo the last commit and discard the changes you made, you can use the git reset --hard HEAD^ command. This will reset the branch to the previous commit and discard any changes you made.

To illustrate this method, let's say we have a repository with a file called example.txt that we want to modify. We make some changes to the file and commit them using the git commit -m "Initial commit" command. We then realize that we made a mistake and want to undo the commit and discard the changes we made.

To do this, we can use the git reset --hard HEAD^ command. This will reset the branch to the previous commit and discard any changes we made.

Here's a step-by-step guide:

  • First, we make some changes to the example.txt file and commit them using the git commit -m "Initial commit" command:

Initial commit

  • Next, we realize that we made a mistake and want to undo the commit and discard the changes we made. We use the git reset --hard HEAD^ command:

Reset command

  • The git reset --hard HEAD^ command resets the branch to the previous commit and discards any changes we made:

Discarded changes

Undoing multiple commits

If you want to undo multiple commits, you can use the git reset command with the number of commits you want to undo. For example, git reset HEAD~3 will undo the last three commits.

To illustrate this method, let's say we have a repository with a file called example.txt that we want to modify. We make some changes to the file and commit them three times using the git commit -m "Commit 1", git commit -m "Commit 2", and git commit -m "Commit 3" commands. We then realize that we made a mistake and want to undo the last three commits.

To do this, we can use the git reset HEAD~3 command. This will reset the branch to the commit before the last three commits.

Here's a step-by-step guide:

  • First, we make some changes to the example.txt file and commit them three times using the git commit -m "Commit 1", git commit -m "Commit 2", and git commit -m "Commit 3" commands:

Multiple commits

  • Next, we realize that we made a mistake and want to undo the last three commits. We use the git reset HEAD~3 command:

Reset command

  • The git reset HEAD~3 command resets the branch to the commit before the last three commits:

Reset branch

Reverting a commit

If you have already pushed the commit to a remote repository, you can use the git revert command to create a new commit that undoes the changes made in the previous commit. For example, git revert HEAD will create a new commit that undoes the changes made in the last commit.

To illustrate this method, let's say we have a repository with a file called example.txt that we want to modify. We make some changes to the file and commit them using the git commit -m "Initial commit" command. We then push the commit to a remote repository. We later realize that we made a mistake and want to undo the changes made in the last commit.

To do this, we can use the git revert HEAD command. This will create a new commit that undoes the changes made in the last commit.

Here's a step-by-step guide:

  • First, we make some changes to the example.txt file and commit them using the git commit -m "Initial commit" command:

Initial commit

  • Next, we push the commit to a remote repository:

Push commit

  • Later, we realize that we made a mistake and want to undo the changes made in the last commit. We use the git revert HEAD command:

Revert command

  • The git revert HEAD command creates a new commit that undoes the changes made in the last commit:

Reverted commit

It's important to note that these commands can be dangerous if used incorrectly, so it's always a good idea to make a backup of your repository before making any changes. Additionally, if you're unsure about which command to use, it's always a

Here are few references to understand the concept in different context:

Comments

Popular posts from this blog