

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:
- How to undo untested local changes
- How to undo local staged changes
- How to undo committed changes locally
- 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 theAdd
domain.
In this case, you should use thegit restore
domain.
Specifically, thegit restore
command will look something like this:
git restore filename
so say you have oneLÉAME.md
and you accidentally typed and saved some text that you want to discard.
You can first use thegit status
command to view the status of your Git repository.
This command will confirm that the file is untested (meaning you did not useAdd
yet) 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.md
Office hour:
git restore README.md
You can then usegit status
again 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 theAdd
domain.
Suppose you made some changes toLÉAME.md
file locally, used theAdd
command that made the changes, and then noticed that the text contains some errors.
first racegit status
to 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 status
again:
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 status
one 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 theAdd
and committed the file with thegit cometer
domain.
This means that the commit only exists locally and has not yet been pushed to a remote repository.
first usegit status
To 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 log
domain:
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 log
again.
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 status
again
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 command
and 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 status
To 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.
To view the short version of the commit hash, use the following command:
git log --oneline
Asgit log
command, 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 push
to 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 wordback
precedent, 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:
- Git and GitHub for Beginners - Crash Course
- Git Tutorial for Professionals: Tools and Concepts to Master Version Control with Git
- Advanced Git Tutorial: Interactive Rebase, Cherry Picking, Reflog, Submodules, and More
Thanks for reading and happy coding :)
PROPAGANDA
PROPAGANDA
PROPAGANDA
PROPAGANDA
PROPAGANDA
PROPAGANDA
PROPAGANDA

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
PROPAGANDA