Git Remove Last Commit – How to Undo a Commit in Git (2024)

/ #Git
Git Remove Last Commit – How to Undo a Commit in Git (1)
Dionysia Lemonaki
Git Remove Last Commit – How to Undo a Commit in Git (2)

Git is a powerful tool and the most popular version control system. It is how developers and technical teams collaborate and work together on projects.

But what happens when you accidentally commit a file and realize that you shouldn't have done that because that file contains an error?

There is no need to fret because Git allows you to undo your mistakes and go back to an earlier version of your project.

One of the most helpful features of Git is the ability to undo the changes you make to a project over time.

In this article, you will learn how to undo changes in Git depending on the state of your Git repository.

Here is what we will cover:

  1. How to undo local unstaged changes
  2. How to undo local staged changes
  3. How to undo local committed changes
  4. How to undo public committed changes

How to Undo Local Unstaged Changes in Git

Say you are working on your local machine. You made and saved some changes to a file locally, but you would like to discard them.

When you have yet to stage those changes, you haven't used the git add command.

In this case, you need to use the git restore command.

Specifically, the git restore command will look something like this:

git restore filename

So, say you have a README.md file and you accidentally wrote and saved some text you want to discard.

You can first use the git status command to view the state of your Git repository.

This command will confirm that the file is unstaged (meaning you haven't used git add yet), and will let you view the files you may want to undo:

On branch mainYour branch is up to date with 'origin/main'.Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: README.mdno changes added to commit (use "git add" and/or "git commit -a")

Here is how you would undo the changes in the README.md file:

git restore README.md

You can then use git status again to check the state of the repository:

On branch mainYour branch is up to date with 'origin/main'.nothing to commit, working tree clean

Now, you have successfully discarded your most recent changes and reverted to the last committed version of your project.

How to Undo Local Staged Changes in Git

A file is staged when you have used the git add command.

So, say you made some changes to the README.md file locally, you used the git add command which staged the changes, and then you realized that the text contains some mistakes.

First, run git status to make sure you have staged the file (meaning you used git add) :

On branch mainYour branch is up to date with 'origin/main'.Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: README.md

As you can tell by the output of git status, you can use the following command to undo your changes:

git restore --staged filename

This command will unstage the staged file, but will keep your changes.

Let's run git status again:

On branch mainYour branch is up to date with 'origin/main'.Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: README.mdno changes added to commit (use "git add" and/or "git commit -a")

Now, to discard the changes you made and restore the file to its original contents, use:

git restore README.md

And let's run git status one last time:

On branch mainYour branch is up to date with 'origin/main'.nothing to commit, working tree clean

Now, the changes you made have gone and the file is reverted to the current committed version.

How to Undo Local Committed Changes in Git

Say you made changes to a file, you staged the file with the git add command, and you committed the file with the git commit command.

This means that the commit exists only locally and has not been pushed to a remote repository yet.

First, use git status to check that you committed the file:

On branch mainYour branch is ahead of 'origin/main' by 1 commit. (use "git push" to publish your local commits)nothing to commit, working tree clean

Next, if you want to undo your last local commit, use the git log command:

The latest commit will have a commit hash (a long series of numbers and characters) and a (HEAD -> main) at the end – this is the commit you are looking to undo.

The second to last commit has a commit hash and a (origin/main) at the end – this is the commit you want to keep and the commit you pushed to the remote repository.

After that, use the following command to undo the commit:

git reset --soft HEAD~

Now, let's use git log again.

You should see the commit hash, and a (HEAD -> main, origin/main) at the end.

The last commit you made is no longer part of the repository's history and has been removed.

The command above restored everything to the version of the file prior to that accidental or mistaken commit and has gone back one commit.

Let's check git status again

On branch mainYour branch is up to date with 'origin/main'.Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: README.md

Keep in mind that although the git reset --soft HEAD~ command undid your latest commit, it kept the changes you made.

How to Undo Public Committed Changes in Git

What if you made changes to a file, you staged the file git add, committed it with git command, and pushed it to a remote repository with git push – but then realized you shouldn't have committed that file in the first place?

What do you do then?

First, use git status to check the state of the git repository:

On branch mainYour branch is up to date with 'origin/main'.nothing to commit, working tree clean

In the section above, you saw that each commit has a commit hash, which is a long series of numbers and characters.

To see the short version of the commit hash, use the following command:

git log --oneline

With the git log command, you can also check which commit you want to undo.

Say that your latest commit has a commit hash of cc3bbf7, which is followed by (HEAD -> main, origin/main), and a commit message such as "commit README.md file" .

To undo that specific commit, use the following command:

git revert cc3bbf7 --no-edit

The command above will undo the changes by creating a new commit and reverting that file to its previous state, as if it never changed.

Lastly, use git push to push the change to the remote branch.

Once you do that, you will see that the commit message will be the same as the previous one but with the word revert preceding it, such as 'Revert "commit README.md file"'.

Keep in mind that the commit history will show both commits separately:

Revert "commit README.md file"@john-doejohn-doe committed 9 minutes agocommit README.md file@john-doejohn-doe committed 16 minutes ago

Conclusion

And there you have it – you now know how to undo changes in Git.

To learn more about Git, check out the following free resources:

Thank you for reading and happy coding :)

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

Git Remove Last Commit – How to Undo a Commit in Git (3)
Dionysia Lemonaki

Read more posts.

If this article was helpful, .

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

ADVERTIsem*nT

Git Remove Last Commit – How to Undo a Commit in Git (2024)
Top Articles
Brake Servicing & Inspection Guide - Les Schwab
Complete Guide to Brake Pads: Lifespan and Replacement | Les Schwab
Menards Thermal Fuse
Cars & Trucks - By Owner near Kissimmee, FL - craigslist
Myexperience Login Northwell
Lycoming County Docket Sheets
Housing Intranet Unt
What Does Dwb Mean In Instagram
Weekly Math Review Q4 3
World History Kazwire
Blue Beetle Showtimes Near Regal Swamp Fox
David Turner Evangelist Net Worth
Funny Marco Birth Chart
Kris Carolla Obituary
Lesson 8 Skills Practice Solve Two-Step Inequalities Answer Key
Conan Exiles Colored Crystal
Dutch Bros San Angelo Tx
Plan Z - Nazi Shipbuilding Plans
Booknet.com Contract Marriage 2
MLB power rankings: Red-hot Chicago Cubs power into September, NL wild-card race
25 Best Things to Do in Palermo, Sicily (Italy)
Renfield Showtimes Near Paragon Theaters - Coral Square
Dtm Urban Dictionary
Marilyn Seipt Obituary
Ascensionpress Com Login
Ordensfrau: Der Tod ist die Geburt in ein Leben bei Gott
Winterset Rants And Raves
R/Mp5
Hannah Jewell
Craigslist Sf Garage Sales
Devargasfuneral
Dumb Money, la recensione: Paul Dano e quel film biografico sul caso GameStop
Wow Quest Encroaching Heat
Pickle Juiced 1234
Pitco Foods San Leandro
Car Crash On 5 Freeway Today
Sinfuldeeds Vietnamese Rmt
Top-ranked Wisconsin beats Marquette in front of record volleyball crowd at Fiserv Forum. What we learned.
Bitchinbubba Face
Robeson County Mugshots 2022
M Life Insider
Gopher Hockey Forum
Torrid Rn Number Lookup
How to Connect Jabra Earbuds to an iPhone | Decortweaks
877-552-2666
Learn4Good Job Posting
40X100 Barndominium Floor Plans With Shop
El Patron Menu Bardstown Ky
Runescape Death Guard
Metra Union Pacific West Schedule
Att Corporate Store Location
Island Vibes Cafe Exeter Nh
Latest Posts
Article information

Author: Ms. Lucile Johns

Last Updated:

Views: 5718

Rating: 4 / 5 (61 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Ms. Lucile Johns

Birthday: 1999-11-16

Address: Suite 237 56046 Walsh Coves, West Enid, VT 46557

Phone: +59115435987187

Job: Education Supervisor

Hobby: Genealogy, Stone skipping, Skydiving, Nordic skating, Couponing, Coloring, Gardening

Introduction: My name is Ms. Lucile Johns, I am a successful, friendly, friendly, homely, adventurous, handsome, delightful person who loves writing and wants to share my knowledge and understanding with you.