Commits don't actually get destroyed in Git for some 90 days, so you can usually go back and rescue one you didn't mean to get rid of. Note that you should only do this if you haven't pushed yet. You know what git needs? Then just do git commit to commit as a regular commit. It might not be as direct as some other answers here, but it just feels right. I create the steps in the image below in more details for you, including all steps that may happens in real situations and committing the code: How to undo the last Git commit? But I still don't quite understand the difference between the two options. But unless you use --hard you leave your files as they were.
You already pushed the commit You can apply the same solution of the other scenario and then doing git push with the -f option, but it is not recommended since it overwrites the remote history with a divergent change it can mess your repository. Then the reputation git has for handling mistakes made by us mere mortals disappears. This should undo your changes. Nope, there's still a way to get it back. So now git status shows the changes you had checked into C.
If you haven't yet pulled other changes onto your branch, you can simply do. The stored contents are only removed when garbage collection is executed. But it's actually amazingly easy if you do understand. This will undo the effects of the amend, but leave changes staged. Beware however that if you have added any new changes to the index, using commit --amend will add them to your previous commit.
Because you used --hard, your files are reset to their state at commit B. For a git beginner, it isn't obvious what the difference is between the last two options --soft and the one above it. If you do not need to edit the message, you could use the -C option. How to undo the previous local commit Just reset your branch to the previous location for example, using gitk or git rebase. It is therefore possible to recover the last staged version of a file that was not currently staged when git reset --hard was executed see the posts linked above for more information. When you do git status, you'll see that the same files are in the index as before. .
It should be noted that if you're attempting to remove secret information before pushing to a shared repository, doing a revert won't help you, because the information will still be in the history in the previous commit. Here --soft means reset into staging. Mentioning the index doesn't help, we don't really know what that means yet. You want to undo the commit but keep your changes for a bit of editing before you do a better commit. Once you found the point to which you wish to undo to use the sequence of commands as explained above. You can also modify the commit message.
In my case, I was on the wrong branch when I edited a file. Word of warning: Careless use of git reset is a good way to get your working copy into a confusing state. How to undo a public commit Perform a to undo the changes. How to fix the previous local commit Use git-gui or similar to perform a git commit --amend. Type git reflog and you'll see a list of partial commit that is, hashes that you've moved around in. I committed it then realized I was in the wrong branch. It would affect performance, so it would be best to add a config flag as to whether to enable it.
Undoing a commit is a little scary if you don't know how it works. Then I typed in the following. Then navigate to your repository's local directory and run this command: git -c diff. I recommend that Git novices avoid this if they can. Advanced: Correction of the private branch in public repository This can be dangerous -- be sure you have a local copy of the branch to repush. Ah, but suppose commit C wasn't a disaster, but just a bit off.
Just push a followup commit see 'How to undo a public commit' above , and later, do a to hide the history. You haven't lost a thing! In fact, right after this command, you could do git commit and you'd be redoing the same commit you just had. Find the commit you destroyed, and do this: git checkout -b someNewBranchName shaYouDestroyed You've now resurrected that commit. After garbage collection in your local repository, it will be like the unwanted commit never happened. As a sidenote: Everytime a file is staged, git stores its contents in its object database.