How to With Git: Undo Commit

how-to

Our experts take readers step-by-step through a variety of hosting and programming tasks in our popular series of "How-To" guides.

Sometimes you might commit changes to your Git repo, but then realize you made a mistake. Now you want to undo those commits to get your code working again.

In this guide, we’ll look at the git reset command for local and remote commits to a repository.

Difference Between a git reset and git revert

It’s important to understand the difference between resetting vs reverting commits when using Git.

The git reset command undoes commits by removing previous commits from the repository and resetting the Git HEAD to an earlier commit. Some Git history might be lost by doing this, depending on what option is used.

The git revert command undoes commits by creating a new commit that represents an earlier state of the repository. No Git history will be lost by doing this.

Already pushed changes and someone else pulled those changes? You should not use git reset to undo changes, you should use git revert as described in our Git rollback commit guide instead.

Using the git reset Command to Undo a Commit

For the following examples, assume our local Git tree looks like this:

To undo the commit named “C “due to a mistake, we can run a git reset command.

Depending on file tracking needs, you’ll want to use the git reset command with different flags.

Case 1: Use git reset and Remove Files From the Staging Area

We can use the git reflog command to see our Git history.

We can use git ls-files to see the currently staged files for our project:

1

2

3

4

5

6

7

8

9

10

$Gitreflog

f326d13HEAD@{0}:commit:AddedC

358d535HEAD@{1}:commit:AddedB

00b61d5HEAD@{2}:commit(initial):AddedA

$Git ls-files

A

B

C

X

To keep any tracked files since commit C, but remove them from the Git index, you can run:

1

Git reset HEAD~1

You can use the Git shorthand of HEAD~1 to remove the commit one commit before HEAD.

If you used HEAD~5,this would remove the commit that is five commits before HEAD.

Changes to the Git index,also called the “staging area,” will be lost, so the Git tree would now be:

1

2

3

X

|

A-B

Which we can confirm with these commands:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

$Gitreflog

358d535HEAD@{0}:reset:movingtoHEAD~1

f326d13HEAD@{1}:commit:AddedC

358d535HEAD@{2}:commit:AddedB

00b61d5HEAD@{3}:commit(initial):AddedA

$Git ls-files

A

B

$Git status

On branch master

Untracked files:

(use"Git add ..."toinclude inwhat will be committed)

C

X

When just using git reset with no flags, we don’t remove any files; they are just un-staged:

1

2

$ls

ABCX

You do end up removing the log entry for the commit you reset, however:

1

2

3

4

5

6

7

8

9

10

11

12

$Git log

commit358d535bc5e06730e61d272be34a6d0e568f42af

Author:User&lt;user@example.com&gt;

Date:Tue Feb1013:54:552015-0500

AddedB

commit00b61d53fe06ee672fa0497b175fb7bd89e26b72

Author:User&lt;user@example.com&gt;

Date:Tue Feb1013:54:552015-0500

AddedA

Case 2: Use git reset –soft and Preserve Changes to Files

If you use the –soft flag, any changes to tracked files and the Git index are preserved:

1

Git reset--soft HEAD~1

Any files that were staged remain so, and no files are physically removed:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

$Git ls-files

A

B

C

X

$ls

ABCX

$Git status

On branch master

Changes tobe committed:

(use"Git reset HEAD ..."tounstage)

newfile:C

newfile:X

Using the –softflag, you still end up removing the log entry for the commit you reset.

Case 2: Use git reset –hard and Remove All Changes to Files

If you use the –hardflag, any changes to tracked files and the Git index are lost:

1

2

$Git reset--hard HEAD~1

HEAD isnow at3bf1b55AddedB

All files after the commit you reset to are un-staged and physically removed:

1

2

3

4

5

6

7

8

9

10

$Git ls-files

A

B

$ls

AB

$Git status

On branch master

nothing tocommit,working directory clean

You again remove the Git log entry for the commit that you reset.

Try to Use git revert When Commits Are Already Pushed

As stated above, if you’ve already pushed your changes and someone else pulled in those changes, you should not use git reset to undo changes, use git revert instead.

However, if you really want to, you can apply the same steps as in the previous section to delete some previous Git commits.

After that, you can do a git push -f to use the force option. Again, this is not recommended, because it can create serious conflicts between the various repository states of other Git users.

Questions or Comments? Ask Jacob!

Ask a question and Jacob will respond to you. We strive to provide the best advice on the net and we are here to help you in any way we can.

Was this helpful? Tell Us Thanks.

Like this article on Facebook

Tweet this article on Twitter

Share this article on Google+

About the Author

Jacob Nicholson

Jacob Nicholson has worked at all levels of both small and large web hosting companies. With a background in computer networking, he enjoys all things Internet, as well as trying out the latest and greatest technology. After several years of serving in a system administration role, he transitioned to become a customer advocate, focused on streamlining common web hosting woes with detailed online guides. He brings with him tons of experience and lots of passion for the wonderful world of web hosting — and he's eager to share his knowledge with HostingAdvice fans.

Disclaimer: Great efforts are made to maintain reliable data on all offers presented. However, this data is provided without warranty. Users should always check the offer provider’s official website for current terms and details. Our site receives compensation from many of the offers listed on the site. Along with key review factors, this compensation may impact how and where products appear across the site (including, for example, the order in which they appear). Our site does not include the entire universe of available offers. Editorial opinions expressed on the site are strictly our own and are not provided, endorsed, or approved by advertisers.