![]() ![]() I hope you found this article useful and learned something new. Undoing a delete operation actually means restoring the deleted commit. What are we doing here? We’re telling git to go back to the commit 1b2c3d4 and to get rid of all the changes we did (in this case, the change was deleting the commit). You can now use this commit ID to restore the changes. This means that you were at commit 1b2c3d4 and you did a reset to the previous commit. You want to look at a log like this one: 1b2c3d4 reset: moving to HEAD~1 You can see this log by running git reflog. Luckily, git keeps a log of all the commits that have been removed. In this case we entirely removed a commit and there’s no trace in the git history, so we cannot say HEAD~number anymore. If you run git reset -hard HEAD~1 and you’re not happy with the result, you can still restore the changes.Īs we’ve just seen, git reset is our time travel machine, but we need to tell it where to go. Or is it? Bonus: Restore Hard Deleted Changes The full command we need this time is git reset -hard HEAD~1 and it will delete the last commit and the changes. Warning: this will also delete any uncommitted changes you have. If you want to delete the changes, you need to add the -hard flag while running git reset. This is the case where you want to undo a commit and you don’t want to keep the changes. You can now keep working and whenever you’re ready you can do a new commit. A shortcut for HEAD is so would be the same as HEAD~1.Īfter running this command, you’ll see that the last commit is gone but the files still have your changes applied.HEAD~1 means the last commit, but you can also use HEAD~2 to undo the last 2 commits. ![]() HEAD means the current commit you’re on.-soft means that the changes will be kept.The full command we need is git reset -soft HEAD~1. The most common use case is probably to undo a commit but keep in mind that you can do much more. Git reset is kind of a time travel, really powerful but also dangerous. This is the case where you want to undo a commit, but you want to keep the changes so that you can make a new commit at a later time. is not required if all you wanted to do is to change the commit message. The commit will be updated with the new message.Īctually, git add. Once you’re done, save and close the editor. This will open your default editor and you can change the commit message. Similar situation to the previous one, but you also want to change the commit message. No extra actions required, you’re done! 2. ![]() As a result, the last commit will be updated with the new files. Instead of creating an extra commit on top, you can run git commit -amend -no-edit. Let’s start with the easiest situation, you already did a commit but you forgot to add some files. If you’re just here for the commands, you can skip the video and go straight to the article. What does that mean? I’ll get into that later.Īs usual, there’s a live demo waiting for you on my YouTube channel where I show you all the content of this article plus some extra words and scenarios. They’re similar but not exactly the same, so you can apply the best one for your situation.īonus content: I’ll also show you how to restore hard deleted changes. If you want to keep your history clean and make your mistake disappear, let me show you 4 different ways to undo a commit. Sure, you can just add a new commit, but in the long run this will mess up your git history (unless you’re closing PRs with squash).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |