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

Fake CVR Generator Denmark

What Is Danish CVR The Central Business Register (CVR) is the central register of the state with information on all Danish companies. Since 1999, the Central Business Register has been the authoritative register for current and historical basic data on all registered companies in Denmark. Data comes from the companies' own registrations on Virk Report. There is also information on associations and public authorities in the CVR. As of 2018, CVR also contains information on Greenlandic companies, associations and authorities. In CVR at Virk you can do single lookups, filtered searches, create extracts and subscriptions, and retrieve a wide range of company documents and transcripts. Generate Danish CVR For Test (Fake) Click the button below to generate the valid CVR number for Denmark. You can click multiple times to generate several numbers. These numbers can be used to Test your sofware application that uses CVR, or Testing CVR APIs that Danish Govt provide. Generate

How To Iterate Dictionary Object

Dictionary is a object that can store values in Key-Value pair. its just like a list, the only difference is: List can be iterate using index(0-n) but not the Dictionary . Generally when we try to iterate the dictionary we get below error: " Collection was modified; enumeration operation may not execute. " So How to parse a dictionary and modify its values?? To iterate dictionary we must loop through it's keys or key - value pair. Using keys

How To Append Data to HTML5 localStorage or sessionStorage?

The localStorage property allows you to access a local Storage object. localStorage is similar to sessionStorage. The only difference is that, while data stored in localStorage has no expiration time untill unless user deletes his cache, data stored in sessionStorage gets cleared when the originating window or tab get closed. These are new HTML5 objects and provide these methods to deal with it: The following snippet accesses the current domain's local Storage object and adds a data item to it using Storage.setItem() . localStorage.setItem('myFav', 'Taylor Swift'); or you can use the keyname directly as : localStorage.myFav = 'Taylor Swift'; To grab the value set in localStorage or sessionStorage, we can use localStorage.getItem("myFav"); or localStorage.myFav There's no append function for localStorage or sessionStorage objects. It's not hard to write one though.The simplest solution goes here: But we can kee