git revert will reverse the changes made by the commit hash given, and then create a new commit.If we open the Working Directory, we'll see that it has indeed rewound back to commit A. In the git log we see that commits B-D are still in the commit history even though their changes have been reversed. Now we just need to commit those changes. You could do that like this:īy running git status, we can see that all files added in the commits B-D have been deleted. In the same example we used above, lets say we want to rewind to commit A and maintain the commit history. This way you can do one commit manually to maintain a neat commit history. And the -n option tells git to not create a commit, but to instead stage the changes in the Staging Index and the Working Directory. That caret symbol tells git to include that first commit hash given in the range. To revert a range of commits, simply enter the following git command in the terminal: Now if we look at the working directory you'll see that the "commit-A" has been removed, but the rest remain. So, the fourth most recent commit is HEAD~3. Remember that most recent commit is equal to HEAD, which can be written as HEAD~0. Also, I used HEAD~3 to reference commit A. You can see in the git log that the git revert command automatically created a commit. We would enter the following git command: Now let's say there was some bug in commit A that we wanted to undo but keep commits B-D. I created a local repository with some files whose names match the commit in which they were created, which looks like this:Īll these files just have the text "added content" in them and each was added with their own commit. So, if you entered git revert HEAD~2, the changes from the third most recent commit would be reversed. You can also pass git revert a reference to the commit that is relative the HEAD ref. To revert a single commit, enter the following git command in the terminal: This makes git revert great for removing a bug that was introduced by a single commit without having to redo all the work in the following commits. A git revert only reverses the commit that was given and keeps all the following commits and the changes that were made in them. This is different from a git reset, because a reset will move the HEAD ref to the commit hash given, and drop all the commits after that one. This is the preferred method for undoing the changes of a specific commit if that commit has already been push to a shared remote repository because it preserves the commit history. The git revert command will reverse the changes made by the commit hash given, and then create a new commit.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |