

Scenario: You made some commits, then realized you were checked out on master. If you want to replay exactly one of those commits into your repository use git cherry-pick.If you want to recreate one or more files in your working directory as they were at that moment in time, without altering history use git checkout.If you want to restore the project’s history as it was at that moment in time use git reset -hard.So… how do you use the reflog to “redo” a previously “undone” commit or commits? It depends on what exactly you want to accomplish: You can’t use git reflog to restore another developer’s un-pushed commits. Git will periodically clean up objects which are “unreachable.” Don’t expect to find months-old commits lying around in the reflog forever.

HEAD changes when you switch branches, make commits with git commit and un-make commits with git reset, but HEAD does not change when you git checkout - (from an earlier scenario-as mentioned before, those changes were never committed, so the reflog can’t help us recover those. git reflog is similar, but instead shows a list of times when HEAD changed. You’re probably familiar with the git log command, which shows a list of commits.

You can recover almost anything-anything you’ve committed-via the reflog. What’s happening: git reflog is an amazing resource for recovering project history. Undo with: git reflog and git reset or git checkout Scenario: You made some commits, did a git reset -hard to “undo” those changes (see above), and then realized: you want those changes back! This is the safest option, but often, you’ll want to “undo” the commits and the changes in one move-that’s what -hard does. The commits are gone, but the contents are still on disk. By default, git reset preserves the working directory. What’s happening: git reset rewinds your repository’s history all the way back to the specified SHA. Scenario: You’ve made some commits locally (not yet pushed), but everything is terrible, you want to undo the last three commits-like they never happened. Be sure you know what you’re throwing away here! (Maybe use git diff to confirm.) Reset “local” changes They were never committed, so Git can’t help us recover them later. Keep in mind: any changes you “undo” this way are really gone. You could provide a branch name or specific SHA you want to go back to or, by default, Git will assume you want to checkout HEAD, the last commit on the currently-checked-out branch. What’s happening: git checkout alters files in the working directory to a state previously known to Git. You want to undo everything in that file-just go back to the way it looked in the last commit. You haven’t committed those changes, though. Scenario: The cat walked across the keyboard and somehow saved the changes, then crashed the editor. With nothing currently staged, this just rewrites the previous commit message.
Git change branch and reset files update#
What’s happening: git commit -amend will update and replace the most recent commit with a new commit that combines any staged changes with the contents of the previous commit. Undo with: git commit -amend or git commit -amend -m "Fixes bug #42" Scenario: You just typo’d the last commit message, you did git commit -m "Fxies bug #42" but before git push you realized that really should say “Fixes bug #42”. This is Git’s safest, most basic “undo” scenario, because it doesn’t alter history-so you can now git push the new “inverse” commit to undo your mistaken commit. If the old commit is “matter”, the new commit is “anti-matter”-anything removed in the old commit will be added in the new commit and anything added in the old commit will be removed in the new commit. What’s happening: git revert will create a new commit that’s the opposite (or inverse) of the given SHA. Scenario: You just ran git push, sending your changes to GitHub, now you realize there’s a problem with one of those commits. In this post, I’m going to take a look at some common scenarios where you might want to “undo” a change you’ve made and the best way to do it using Git. When you make a new commit, Git stores a snapshot of your repository at that specific moment in time later, you can use Git to go back to an earlier version of your project. In Git, “undo” can mean many slightly different things. One of the most useful features of any version control system is the ability to “undo” your mistakes.
