Undoing Git Commits: Revert and Reset Changes
Undo commits and updates in Git
Website Visitors:You’ve just committed something. Now what? When you’re working on a project in Git, you may run into situations where you have to revert back to an earlier version of a file or even the entire branch. Learn how to revert commits with Git. In this tutorial, we’ll cover how to revert changes using Git. To revert a single change, you can simply type ‘revert’ followed by the commit number. Let us explain in detail.
Undo Local Unstaged changes in Git
Let’s say you have a file in your local git repository, already added and committed in Git. Now, you’ve made changes locally to that file by adding or removing content. When you use git status command, it says modified: filename.txt
.
If you’d like to discard that modified content and revert it back to its original state, you should use the below command
|
|
This will discard that content you’ve modified and restore it to earlier version. Open the file and verify if the new content is removed. You can verify the file’s state by running git status
command.
Undo Local Staged Changes in Git
Referring to the same example discussed above, (a file is created and added to git), Let’s say you’ve used git add filename.txt
command. This file is now in staging area. You should use --staged
parameter in git restore filename.txt
command as shown below:
|
|
Again, you can run git status
command to verify the file state. This command will only remove the file from staging area, but the content you’ve added or removed will still be there ie., the modification you’ve done is not reverted back.
As the file is now set back to working directory from staging area, you should run git restore filename.txt
to restore the content as well. This time the modification you’ve done will also be reverted back.
Revert Local committed changes in Git
In this scenario, let’s say you’ve created a file in Git, added it and committed to Git. You have not yet pushed it to remote repository. Follow the below commands to revert the committed file to its previous state.
Use git log
command to view all the commits and the commit ids (long alpha numeric characters, aka commit hash).
The latest commit has HEAD -> Main
value. This is what you should revert to its previous state. Last but one commit will have (origin/main, origin/HEAD)
values. This is the commit that you should revert to. Now run the below command to undo the latest commit.
|
|
This command will undo your latest commit but it will still keep the changes you’ve made. If you’d like to remove your previous git commit and the changes you’ve made to the files, then you should use --hard
parameter as shown below.
|
|
Revert committed changes to remote repository
Let’s say you have a file in git, created and committed it and pushed it to a remote git repository using git push
. Now you’d like to revert that committed file in remote repo.
Run either git log
command to view full commit details or run git log --oneline
command to view the git log output in single liner.
From above git log
command output and the commit descriptions, identify which commit you want to revert to. Let’s say you’d like to revert to the latest commit. And it has a commit id of b90006f . As it is latest commit, it has the values, (HEAD -> main, origin/main). Next run the below command to undo that latest change in remote repo:
|
|
This command will undo the changes by creating a new commit and revert that file to its previous commit. Next run git push
to push that change to your remote repository/branch.
For all commits you should supply a commit message. If you use --no-edit
parameter, git will automatically add Revert Your-Previous-Commit-Message
as the commit message automatically. If you do not use --no-edit
parameter, you will be prompted to enter a git commit message. Ex: git revert b90006f
There are different ways in usinggit revert
command. Let’s look at all possible ways in using it.
-
git revert commitID1 commitID2 commitID3
This will create three separate revert commits. -
git revert HEAD~2..HEAD
Specifying range of commits. In this example, you’re reverting the last two commits. -
git revert commitID1 commitID5
Reverting to range of commits using commit hashes. -
git revert -m 1 MergeCommitID
Reverting to a merge commit.
Different files in remote repo vs local repo
Let’s say you have modified a file locally and same file is modified by someone else in remote repository like github or bit bucket. You added and commited the modified local file. When you use git push
command it says you have some updates on the remote repo and errors out. Your push will not be successful. If you think to pull the file from remote repo and use git pull
command, it writes both differences into same file in your local repo. A simple solution is, you can:
- modify this file containing both the information.
- readd it to git, commit it locally.
- push it to remote repository.
You have added a locally modified file, committed it and tried to push it. It failed as there are updates on the same file on remote repo. So, you’ve pulled it, which will append the data from remote repository at the end of the file. Now you’ve modified it by adding or removing content, and added it, committed it and pushed it again.
Alternate ways
If you’ve used git pull
command, run git merge --abort
command. This will remove all the updates that are pulled from your remote repository and leaves the file in the state before you run the initial git pull
command.
You’re now back to step 1 ie., same file modified locally and remote repo by someone else. Here, git pull --force
command also wont work. In this case, there are updates in both local file and the file in your repository. So it errors out and informs you to fix the errors first.
Git pull and push your updates
Copy your updated content to a different file on your machine.
If you run git log --oneline
command in your local git repo, you will see a commit id, which is referring to the changes you made to the file locally. Copy the commit id for the one you would like to restore to. Basically, the second commit in the git log --oneline
command. First line commit id is the one you modified locally. Second one is the one which was saved both your local repo and remote repo.
Run the command git reset HEAD~1
. This will revert the commit id in the local repo file to its previous commit id. You can run git log --oneline
to see the updated commit id.
Now run, git restore filename
command to restore the file contents to its previous state. Your file will have the contents before you added it and committed it to git locally. Next run git pull
command to pull the contents of the remote repository file to your local repo.
Next add your code/content to it and use git push
again to push your updates this time to remote repository.
Git push force
If you do not want your remote repo updates and want to push your updates to the remote repository file:
- update/modify the local repo file content
- using
git add filename
andgit commit -m CommitText
commands, commit the file locally. - use
git push --force
to push your updates to remote repository.
git push --force
will replace contents on remote repository file with the content from your local repository.Merge updates and push
You can also merge both changes to that same file (local changes and changes made to the file in remote repository). Easiest way is to install visual studio code and open the same file that has conflict. It will show “accept current change”, “accept incoming change”, “accept both changes”, " compare changes" options at the top. Use “accept both changes” option. This will add the content from git remote repository to your local repo file. There is no data loss.
Suggested article
If you’d like to go through Git basics refer to our Git Basics article here. All other DevOps categories are listed here: DevOps Tools. Have a look at the posts as per your topic of interest.
Conclusion
In this tutorial, we’ve explained different ways to restore your current state to previous commits. We’ve also demonstrated examples for all the topics that are discussed. We hope you have learned something new in this article.
Please feel free to share your thoughts about this article in the comments section below.
Your inbox needs more DevOps articles.
Subscribe to get our latest content by email.