What is Git HEAD? A Practical Guide Explained with Examples.The Complete Guide to Git Alias: Shorcuts to Be Efficient.How to Rebase in Git: Explained Step-by-Step.I wrote other articles explaining how to use other git commands, and I thought you might be interested in reading some of them since you are reading this. Interested in Learning Other Git Commands? While you don’t typically revert changes every day if you are a programmer, it is important to know how to use the revert command in case you made a mistake to commit file changes that you didn’t intend to commit. In this article, you learned how to revert changes made on your latest commit using the revert command in both local and remote repositories. git log -onelineĪnd verify the origin is in the same commit of the HEAD.Ĭhecking in Github latest commit matches the latest commit from a local repository Conclusion To verify the remote is synced with the local repository, you have two options things. Now, we are ready to push the revert to commit to the remote repository. However, prior to doing so, make sure there to get the latest changes from the remote repository by using the pull command. Hence, we are going to push the latest commit, which is eca19cb, or commit that reverts changes made on commit 98cfeb4. Commit in sync with the remote repository Then, make sure to sync your remote repository using pull and push commands.įor example, if we look at the last image which is the log of the commits in a local repository, you will notice the origin commit that is in sync with the remote repository is commit 98cfeb4. To revert the last commit in a remote repository, you have to follow all the steps to revert the last commit in your local repository. New commit generated after using git revert Reverting the Last Commit in Remote The default message will start with the text “Revert” followed by the commit message of the commit being reverted. Git will also prompt you to add a commit message. Once you use the revert command, your terminal should display the following message: Use the current commit id or the HEAD keyword if you want to revert the last commit changes. In my case, the current commit id is 98cfeb4.ģ. The first commit id logged in the terminal is the current commit where you are now. Terminal result from using git log –oneline command git log -onelineįor instance, this is the list of previous commits I have in my current project. Otherwise, it will display other information such as the author and date of each commit, which is not needed to revert the last commit. I recommend using the -oneline flag to get the information of previous commits in one line. To do so, we will use the log command to display the list of previous commits we have made. Now, we need to get the id of the current commit. You will know there are no changes in your current branch if your terminal does not display the following message:Ģ. Let’s make sure we don’t have any changes in the current branch by using the status command.git revert HEAD Reverting the Last Commit Locally Hence, if the current commit is C, then HEAD will pull the commit id of C. The keyword HEAD is another way to say the current commit. git revert CĪnother option is to use the keyword HEAD instead of the commit id. In case we want to revert the changes made from B to C, what we need to do is to use commit C to revert to the state of B. Let’s use the previous diagram as a repository example where we have git commits of A, B, and C, and C is the current commit. Using the revert command in Git is as simple as providing the commit identifier.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |