keysspot.blogg.se

Git rollback last commit
Git rollback last commit






The easiest way to undo a pushed commit is by reverting it using the git revert command. But now the most recent commit (which is the revert commit) takes away the changes made in the unwanted commit. Here’s what the git log looks like after the revert:Īs you can see, the unwanted commit is still there. This creates a revert commit that takes out the changes made in the previous commit. To undo the last commit in the above screenshot, let’s run: git revert 6b832da82d8b93335c4ac4283d2175806fd2948a To undo a pushed commit, use git revert instead. The problem is the last commit that adds text to the info.txt file is not good and you want to get rid of it.īecause the commit is already pushed, you shouldn’t use git reset like when you’re undoing a local change. Here’s the git log of a project where all the commits are already pushed in the remote origin. Let’s see a concrete example of reverting a commit using the git revert command. In this view, you can change the default revert commit message. When you run git revert, a text editor opens up. In other words, the original commit remains in the version history and the new commit reverts it. This creates a new commit that undoes the unwanted commit.

git rollback last commit

The easiest way to undo a commit after the push is by reverting it. If this is the case, you need to keep in mind others may already have pulled your changes with the unwanted commit. Undo a Commit That Has Been Pushedĭo you want to undo a commit you’ve already pushed? It’s actually equivalent to saying HEAD~1 or HEAD^ you might see in other tutorials. In Git, HEAD~ refers to the latest commit. This undoes the most recent commit and destroys the changes at the same. To get rid of the changes when undoing a commit, you can run git reset –hard. Also, logging the commits with git log, you can see that the last commit was indeed removed.

git rollback last commit

The git reset HEAD~ undoes the latest commit and unstaged the changes.Īs you can see in the above screenshot, running git status shows that the changes are still there. Here’s what the status and log end up looking like after running the command: This removes the latest commit but keeps the changes in place. To get rid of the latest commit that changes the info.txt file, let’s run: git reset HEAD~ In this project, the latest commit adds a small piece of text to the info.txt file. Although, there is a way to revert even this change as you’ll learn later in this guide. This action is destructive in the sense that it deletes all the changes associated with the commit. To undo a commit and destroy the associated changes, you can use git reset –hard. Sometimes, you might not only want to undo a commit but also remove the changes. The soft reset undoes a number of commits and stages them for a single commit.įor example, the following undoes the last 3 commits and stages them: git reset -soft HEAD~3 1.3. A great example of using git reset –soft is when multiple consecutive commits could be just a single commit. This command undoes the previous commit and re-adds the changes (stages for a commit). To undo the commit while keeping the changes staged, run: git reset -soft HEAD~








Git rollback last commit