This would be called 'HEAD'.Ĭommit 100: good commit # Second to last commit. git logĬommit 101: bad commit # Latest commit. Let's say I committed locally, but now I want to remove that commit. There are two ways to "undo" your last commit, depending on whether or not you have already made your commit public (pushed to your remote repository): How to undo a local commit 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. Find the commit you destroyed, and do this: git checkout -b someNewBranchName shaYouDestroyed Type this git reflogĪnd you'll see a list of (partial) commit shas (that is, hashes) that you've moved around in. Nope, there's still a way to get it back. One more thing: Suppose you destroy a commit as in the first example, but then discover you needed it after all? Tough luck, right? Option 4: you did git reset -hard and need to get that code back In fact, right after this command, you could do git commit and you'd be redoing the same commit you just had. When you do git status, you'll see that the same files are in the index as before. This not only leaves your files alone, it even leaves your index alone. You haven't lost a thing! Option 3: git reset -softįor the lightest touch, you can even undo your commit but leave your files and your index: git reset -soft HEAD~1 So now git status shows the changes you had checked into C. But (unless you use -hard) you leave your files as they were. When you do a git reset HEAD~1, you tell Git to move the HEAD pointer back one commit. In both cases, HEAD is just a pointer to the latest commit. Starting again from here, with C as your HEAD: (F)ĭo this, leaving off the -hard: git reset HEAD~1 You want to undo the commit but keep your changes for a bit of editing before you do a better commit. Maybe commit C wasn't a disaster, but just a bit off. Because you used -hard, your files are reset to their state at commit B. You want to destroy commit C and also throw away any uncommitted changes. Say you have this, where C is your HEAD and (F) is the state of your files. I'll show you the 4 different ways you can undo a commit. But it's actually amazingly easy if you do understand. Undoing a commit is a little scary if you don't know how it works. ![]() The article What is the HEAD in git? is helpful if you want to uncommit multiple commits. Once you have this value, use the sequence of commands as explained above. You can use git reflog to determine the SHA-1 for the commit to which you wish to revert. You should understand the implications of rewriting history if you has already been published. It's almost always a bad idea to use -force prefer -force-with-lease instead, and as noted in the git manual: To remove (not revert) a commit that has been pushed to the server, rewriting history with git push origin main -force is necessary. If you do not need to edit the message, you could use the -C option.Īlternatively, to edit the previous commit (or just its commit message), commit -amend will add changes within the current index to the previous commit. git/ORIG_HEAD commit with -c ORIG_HEAD will open an editor, which initially contains the log message from the old commit and allows you to edit it. Commit the changes, reusing the old commit message. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |