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.git add anything that you want to include in your new commit.Make corrections to working tree files.You'll need to add them again before you can commit them again. It will undo your last commit while leaving your working tree (the state of your files on disk) untouched. git reset is the command responsible for the undo.Undo a commit & redo $ git commit -m "Something terribly misguided" # (0: Your Accident)
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |