![]() On the command-line it will look like this - SHA-1 hash instead of the branch name since the HEAD is not pointing to the the tip of the current branch:Ī few options on how to recover from a detached HEAD: If you are not on the latest commit - meaning that HEAD is pointing to a prior commit in history it's called detached HEAD. git/HEAD, and it contains the 40-bytes SHA-1 hash of the current commit. There can only be a single HEAD at any given time (excluding git worktree). HEAD is simply a reference to the current commit (latest) on the current branch. The following link may help those encountering that: Why does git revert complain about a missing -m option? (thanks for pointing out)īefore answering let's add some background, explaining what this HEAD is. If one of the commits between 0766c053.HEAD (inclusive) is a merge then there will be an error popping up (to do with no -m specified). No history is destroyed, so it can be used for commits that have already been made public. This is a safe and easy way to rollback to a previous state. (The -no-commit flag lets git revert all the commits at once- otherwise you'll be prompted for a message for each commit in the range, littering your history with unnecessary new commits.) You can then commit the current tree, and it will create a brand new commit essentially equivalent to the commit you "reverted" to. This will revert everything from the HEAD back to the commit hash (excluded), meaning it will recreate that commit state in the working tree as if every commit after 0d1d7fc3 had been walked back. Lots of complicated and dangerous answers here, but it's actually easy: git revert -no-commit 0d1d7fc3.HEAD How can I move HEAD back to a previous location? (Detached head) & Undo commits You may also find this answer helpful in this case: If you decide you didn't want to revert after all, you can revert the revert (as described here) or reset back to before the revert (see the previous section). Another useful link is this section discussing git-revert. The git-revert manpage actually covers a lot of this in its description. Be sure and write a good message describing what you just did # get your index and work tree into the desired state, without changing HEAD: # Or, you could do it manually (be sure to do this at top level of the repo) # To get just one, you could use `rebase -i` to squash them afterwards #Similarly, you can revert a range of commits using commit hashes (non inclusive of first hash): # This will create three separate revert commits: With Git, revert has a very specific meaning: create a commit with the reverse patch to cancel it out. In that case, you could indeed revert the commits. On the other hand, if you've published the work, you probably don't want to reset the branch, since that's effectively rewriting history. If you mess up, you've already thrown away your local changes, but you can at least get back to where you were before by resetting again. # You could get merge conflicts, if you've modified things which were # This saves the modifications, then reapplies that patch after resetting. # Alternatively, if there's work to keep: # Don't do it if you have uncommitted work you want to keep. One, if you haven't published any of these commits, simply reset: # This will destroy any local modifications. If, on the other hand, you want to really get rid of everything you've done since then, there are two possibilities. You could reset to throw them away you could stash, checkout, stash pop to take them with you you could commit them to a branch there if you want a branch there.) Hard delete unpublished commits (If you've made changes, as always when switching branches, you'll have to deal with them as appropriate. To go back to where you were, just check out the branch you were on again. ![]() Or if you want to make commits while you're there, go ahead and make a new branch while you're at it: git checkout -b old-state 0d1d7fc32 If you want to temporarily go back to it, fool around, then come back to where you are, all you have to do is check out the desired commit: # This will detach your HEAD, that is, leave you with no branch checked out: ![]() This depends a lot on what you mean by "revert".
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |