11/1/2023 1 Comment Git revert local commit![]() ![]() 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). You committed a change you didn’t want to (wrong code, just needs to be deleted).You committed a change too early (feature isn’t complete yet).You committed a change with the wrong files (something missing, too many files, etc.).You committed a change with the wrong message (typo, wrong tense, etc.).I’m quite sure all of these happened to you at least once: git reset -soft HEAD^Īfter adding files or making changes to files, you need to run the command below to add the changes to your branch: git add. The HEAD^ parameter tells git to reset the branch to the last commit. The -soft parameter means that any changes to tracked files in the working tree since the last commit are staged and are changes that are ready to be committed. The following command resets the Git repository’s branch one commit backward. git reset -hard HEAD^ Option 2: Stages the Recent Commit’s Changes The -hard parameter means that any changes to tracked files in the working tree since the last commit are discarded. Option 1: Discard All of the Recent Commit’s Changes There are two options to do this both options reset the Git repository’s branch but one discards all of the changes while the other leaves the changes staged. If you’ve ever wanted to undo the most recent local commits in Git before pushing your changes to production, you’ve come to the right place.
1 Comment
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |