The explanations above may seem confusing. For example, reverting to the second commit ID will undo changes and leave the state of the file as the state of the commit that comes before the second commit – the first commit. Git revert will undo changes up to the state before the specified commit ID. For example, reverting to the second commit ID will undo changes and leave the state of the file as the state of the second commit. Git reset will undo changes up to the state of the specified commit ID. So as you can see, git reset and git revert are not the same. The commit where we reverted from will not be deleted. Unlike the git reset command, the git revert command creates a new commit for the reverted changes. When you check the commit logs, you'll have something like this: git log -oneline The command above will undo the current commit and revert the file to the state of the previous commit. In our case, we'll be using the ID of the third commit: git revert 882ad02 To revert to the to the previous commit, run the git revert command along with the commit ID of the current commit. Here's what the commit log looks like: git log -oneline I have already initialized the project and made three commits like we did in the last section. How to Revert to a Previous Commit Using the git revert Command Luckily for us, there is way to recover the state of a deleted commit. This will also change the commit history of your project. If you undo a commit and delete every file change that came after it, you might lose important changes made to your code by you and other teammates. ![]() Especially when you're working with a team. 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. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |