While this seems like something cool to do, you should be careful when using this command. When we check the commit log, we'll have just the first commit. All changes that were made to the file after that commit were deleted. We're back to the initial state of the file at the point of the specified commit. This is what the text file looks like now: 1. Let's test this out by reverting back to the first commit: git reset 89f6c3d -hard If you want to undo a commit and the all the changes made after that commit, you attach the -hard flag to your git reset command. We've successfully gone back to a previous commit. If you've followed up to this point, you'll not notice any difference in the file (you'll see how to undo both the commit and any changes made to the file later).īut when we run the git log -oneline command, the third commit wont't be in the log of commits: git log -oneline To go back to the second commit, you run the git reset command followed by the commit ID. In my terminal, I have this: git log -onelineĪs you can see above, this command lists all your commits along with their IDs. To do that, run the command below: git log -oneline To revert to a previous commit, you must first get the commit ID. We'll repeat the process above two more times but we'll add an extra line of text to the file before each commit. Next, we're going to initialize, add, and commit this file: git init To get started, I've created a file called tasks.txt. You'll then see how you can revert to either the last commit or any other commit using the commit ID. In this section, we'll go through the process of creating a new file and making three commits. How to Revert to a Previous Commit Using the git reset Command They are not exactly the same, though, so we'll make this article a bit more practical by demonstrating how each command works in a project.Īnyone can follow along with this tutorial because it's not going to be language specific - we'll make use of a text (txt) file. These commands can help you undo your commits and go back to a previous commit. The two commands we'll discuss in this article are git reset and git revert. In this article, you'll learn how to revert to previous commits when tracking your project with Git. It also makes collaborating with others more efficient.
0 Comments
Leave a Reply. |