![]() Then you use the revert command and push the “deletion” of that commit. To understand why this might be a problem, imagine that the commit c1a373a had already been pushed to the shared repository your team uses to collaborate, and one of your coworkers based their work upon it. Now when I use the git log command, this is what I get:ĭ6a261b Create fourth file 354afd1 Add second file f728594 Add first file To understand that, let’s create yet another commit: What does changing history mean, and why would it be a problem? The second caveat has to do with the fact that the reset command changes the history of your repository, and in some scenarios, doing so is problematic. If that’s your case, you can “reset hard” to your heart’s content. On branch controller nothing to commit, working tree clean That’s to say, running git status returns a result like this: So before resetting with the –hard option, makes sure your repository is clean. If you have uncommitted changes when running the command, these changes will be lost for good. Using git reset with the –hard option makes this a dangerous command. This is actually a good thing since it means you can recover commits that you “delete” by accident. Git didn’t delete it it only made it inaccessible. Take a look a the image below:Īs you can see, the third commit is still there. Does that mean the last commit has been deleted? No. If you run the git log command now, you’ll only see the first and second commits, as I said earlier. So, with the command above, we’re saying to Git to reset our index and working directory to the commit specified. HEAD~1 refers to the commit before that-that is, the second-to-last commit. HEAD basically means “the current point where we are now in the repository.” In our scenario, HEAD means the most recent commit. The last command might seem hard to parse, but it’s simple once you understand its parts. Git reset -hard HEAD~1 Explaining What We Did ![]() In other words, you want to be able to run the git log –oneline command again and see this:ģ54afd1 Add second file f728594 Add first file You want the history to look like that commit never existed in the first place. Now let’s say the last commit (the one with the “Add third file” message) is wrong for some reason and you want to get rid of it. The result should look like this (the hashes for the commits will be different, though):Ĭ1a373a Add third file 354afd1 Add second file f728594 Add first file Now you can use the git log –oneline command to see the commits created. ![]() git commit -m "Add second file" touch file3.txt git add. git commit -m "Add first file" touch file2.txt git add. Mkdir git-revert-demo cd git-revert-demo git init touch file1.txt git add. A Hands-On Exampleįirst, let’s create a repository and add some commits to it: ![]() Let’s see how to do that in practice with an example. Let’s say you want to get rid of your latest commit and move the state of your repository to the way it was before that commit. We’ll now cover several ways to undo commits in Git, detailing the pros and cons of each command and explaining what the use cases for each one of them are. Undoing a commit is often tricky for Git beginners, especially because it can mean a number of different things. The problem is that, despite having the same names, the results these commands achieve are totally different! So if you’re a former Subversion user, keep that in mind and keep reading to understand how these commands differ and to learn the Git equivalent of Subversion’s revert. And Subversion, like Git, has a “revert” command. Many newcomers to Git were previously users of Subversion (SVN). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |