Git Remove Last Commit: How to undo a commit in Git (2023)

/ #git
Git Remove Last Commit: How to undo a commit in Git (1)
Dionisio 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. This is how developers and technical teams collaborate and work together on projects.

But what happens when you accidentally upload a file and realize you shouldn't have because the file contains an error?

There's no need to worry because Git allows you to undo your mistakes and go back to a previous version of your project.

One of the most useful features of Git is the ability to undo changes made to a project over time.

In this article, you'll learn how to undo changes in Git based on the state of your Git repository.

This is what we are going to cover:

  1. How to undo untested local changes
  2. How to undo local staged changes
  3. How to undo committed changes locally
  4. How to undo publicly committed changes

How to undo uncommitted local changes in Git

Let's say you are working on your local machine. You've made and saved some changes to a file locally, but you'd like to discard them.

When you haven't staged these changes yet, you haven't used theAdddomain.

In this case, you should use thegit restoredomain.

Specifically, thegit restorecommand will look something like this:

git restore filename

so say you have oneLÉAME.mdand you accidentally typed and saved some text that you want to discard.

You can first use thegit statuscommand to view the status of your Git repository.

(Video) Undo last commits in git

This command will confirm that the file is untested (meaning you did not useAddyet) and will allow you to preview the files you want to undo:

In main branch Your branch is updated with 'source/main'. Changes not ready for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes to working directory) modified: changes to README.mdsem added to commit (use "git add" and/or "git commit -a")

This is how you would undo the changes inLÉAME.mdOffice hour:

git restore README.md

You can then usegit statusagain to check the status of the repository:

On the main branch Your branch is updated with 'source/main'. nothing to confirm, clean working tree

You have now successfully discarded your most recent changes and reverted to the latest committed version of your project.

How to undo local changes in stages in Git

A file is prepared when using theAdddomain.

Suppose you made some changes toLÉAME.mdfile locally, used theAddcommand that made the changes, and then noticed that the text contains some errors.

first racegit statusto make sure you prepared the file (meaning you usedAdd):

On the main branch Your branch is updated with 'source/main'. Changes to commit: (use "git restore --staged <file>..." to remove the stage) modified: README.md

As you can see in the output ofgit status, you can use the following command to undo the changes:

git restore --filename in stages

This command will remove the scenario from the prepared file but will keep the changes.

let's rungit statusagain:

In main branch Your branch is updated with 'source/main'. Changes not ready for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes to working directory) modified: changes to README.mdsem 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 content, use:

git restore README.md

and we are going to rungit statusone last time:

On the main branch Your branch is updated with 'source/main'. nothing to confirm, clean working tree

Now, the changes you made have been removed and the file has been reverted to the current committed version.

How to undo local committed changes in Git

Suppose you made changes to a file, prepared the file with theAddand committed the file with thegit cometerdomain.

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

(Video) How to UNDO / REVERT a PUSHED COMMIT in GIT - 2 Ways - Which One Suits Your Needs?

first usegit statusTo verify that you have committed the file:

On the main branch Your branch is ahead of 'source/main' by 1 commit. (use "git push" to post your local commits) nothing to commit, clean up working tree

Then if you want to undo your last local commit, use thegit logdomain:

The most recent commit will have a commit hash (a long string of numbers and characters) and a(HEAD -> main)at the end: This is the commit you want to undo.

The penultimate commit has a commit hash and a(source/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 usegit logagain.

You should see the commit hash and a(HEAD -> principal, fuente/principal)sin final

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

The above command restored everything to the file version before the accidental or erroneous commit and reverted a commit.

let's checkgit statusagain

On the main branch Your branch is updated with 'source/main'. Changes to commit: (use "git restore --staged <file>..." to remove the stage) modified: README.md

Note that although thegit reset --soft head~command undid your last commit, it kept the changes you made.

How to undo publicly committed changes in Git

And if you made changes to a file, you prepared the fileAdd, committed togit commandand pushed it to a remote repository withgit push– but then you realized you shouldn't have committed that file in the first place?

what do you do then?

first usegit statusTo check the status of the git repository:

On the main branch Your branch is updated with 'source/main'. nothing to confirm, clean working tree

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

(Video) I want to delete or remove last commit in Git | Git Questions

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

git log --oneline

Asgit logcommand, you can also check which commit you want to undo.

Let's say your most recent commit has a commit hash ofcc3bbf7, which follows(HEAD -> principal, fuente/principal), and a confirmation message such as "commit README.md file".

To undo that specific commit, use the following command:

git revert cc3bbf7 --unedited

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

Lastly, usegit pushto push the change to the remote branch.

Once this is done, you will see that the confirmation message will be the same as the previous one, but with the wordbackprecedent, as
'Revert "commit file README.md"'.

Remember that the commit history will show the two commits separately:

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

conclusion

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

To learn more about Git, see the following free resources:

Thanks for reading and happy coding :)

PROPAGANDA

PROPAGANDA

(Video) Deleting commits | How, why and why not | GitHub Tutorial

PROPAGANDA

PROPAGANDA

PROPAGANDA

PROPAGANDA

PROPAGANDA

Git Remove Last Commit: How to undo a commit in Git (3)
Dionisio Lemonaki

Learn something new every day and write about it.

If this article was helpful, .

Learn to code for free. The freeCodeCamp open source curriculum has helped over 40,000 people get jobs as developers.Start

(Video) In Git how do you revert a commit that has already been pushed and made public ? || git revert

PROPAGANDA

Videos

1. 5. Git Tutorial - Revert commits (undoing things)
(yoursTRULY)
2. How to UNDO / REVERT a LOCAL COMMIT in GIT
(Californian Software Engineer Guides)
3. How to revert a commit in git after push in terminal | 1 min. GitHub tutorial
(Carmelle Codes)
4. 11 | Delete recent commits from any git branch locally and remotely | By Hardik patel
(Hardik Patel)
5. how to undo the commit pushed using git revert | git revert | git tutorials
(Dilan_JT)
6. How to Revert a Git Commit
(Cameron McKenzie)

References

Top Articles
Latest Posts
Article information

Author: Melvina Ondricka

Last Updated: 02/06/2023

Views: 5732

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Melvina Ondricka

Birthday: 2000-12-23

Address: Suite 382 139 Shaniqua Locks, Paulaborough, UT 90498

Phone: +636383657021

Job: Dynamic Government Specialist

Hobby: Kite flying, Watching movies, Knitting, Model building, Reading, Wood carving, Paintball

Introduction: My name is Melvina Ondricka, I am a helpful, fancy, friendly, innocent, outstanding, courageous, thoughtful person who loves writing and wants to share my knowledge and understanding with you.