Git Go Back to the to Last Commit on Remote
How To Undo The Last Local Commit in Git
Understanding how to undo the most recent local commit in Git
Introduction
Git is among the most powerful tools that help Developers, Data Scientists and Engineers version their code. A very common scenario when working with Git is when you commit files by accident and you'd need to undo the latest commit in order to avoid pushing the changes made in these files to the remote host.
In today's article, we are going to discuss a few possible options you have when it comes to reversing the last commit assuming that you did not push it to the remote host. Specifically, we will explore how to
- undo the most recent commit and erase all the changes made to the files committed
- undo the last commit and keep the changes made in the modified files
- undo the most recent commit and keep the changes in the files as well as the index
Undo last commit and discard all changes made to the files
Someti m es, apart from undoing the most recent commit you may also want to discard all the changes that were made to the files included in that particular commit. In this case, you need to do a hard reset.
git reset --hard HEAD~1
- The
--hard
flag indicates thatgit reset
command will reset the HEAD, the index and the working tree. This means that the commit will be reversed and additionally, the changes made to the files will also be discarded. -
HEAD~1
translates to "go back 1 commit fromHEAD
using the first parent". Usually a commit has only a single parent so this should do the trick in most of the cases. Note thatHEAD~1
is equivalent toHEAD~
.
Undo last commit but keep the changes made to the files
Alternatively, you may still want to unto the most recent commit but at the same time keep the changes that were made to the files locally. In this case all you need to do is to specify HEAD~1
when running git reset
command:
git reset HEAD~1
This command will instruct Git to move the pointer of HEAD
back by one commit. However, the changes made to the files will not be affected. Now if you do run git status
you should still be able to see the changes made to the files locally.
Undo last commit and keep both the index and local file changes
Finally, you may want to undo the last commit, keep the changes to the files and at the same time keep the index, too.
The index (also known as the staging area) is where the new commit gets prepared and it contains everything that will be included in the new commit. For example, every time you run git add <filename>
a file is being added (or updated) from the working three into the index. In practise, the index is a binary file stored under <repo>/.git/index
and contains.
git reset --soft HEAD~1
- The
--soft
flag indicates thatgit reset
command will only reset the HEAD while the index and the working tree will remain untouched.
Now if you run git status
you should be able to see that the same files are still in the index.
Final Thoughts
In today's short guide we explored a few different ways for undoing the most recent local commit in Git. Sometimes you may want to undo the most recent commit and also discard all the changes made to the modified files while in other occasions you may just want to undo the last commit but at the same keep the changes.
Note however that the options we discussed in this article are applicable only if you have not already pushed the commit to the remote host. For more details with respect to the git reset
command, you can run git reset -h
in the command line that will display usage instructions.
Become a member and read every story on Medium. Your membership fee directly supports me and other writers you read. You'll also get full access to every story on Medium.
Git Go Back to the to Last Commit on Remote
Source: https://towardsdatascience.com/undo-last-local-commit-git-5410a18f527
0 Response to "Git Go Back to the to Last Commit on Remote"
Post a Comment