git - How to move HEAD back to a previous location? (Detached head) -
in git, trying squash commit merging in branch , resetting head
previous place via:
git reset origin/master
but need step out of this. how can move head previous location?
i have sha1 frag (23b6772
) of commit need move to.
how can commit?
before answering lets add background, explaining head
.
first of head?
head
reference current commit (latest) on current branch.
there can single head
@ given time. (excluding git worktree
)
the content of head
stored inside .git/head
, contains 40 bytes sha-1 of current commit.
detached head
if not on latest commit - meaning head
pointing prior commit in history called detached head
.
on command line this- sha-1 instead of branch name since head
not pointing the tip of current branch
a few options on how recover detached head:
git checkout
git checkout <commit_id> git checkout -b <new branch> <commit_id> git checkout head~x // x number of commits t go
this checkout new branch pointing desired commit.
command checkout given commit.
@ point can create branch , start work point on.
# checkout given commit. # doing result in `detached head` mean `head` # not pointing latest need checkout branch # in order able update code. git checkout <commit-id> # create new branch forked given commit git checkout -b <branch name>
git reflog
you can use reflog
well.
git reflog
display change updated head
, checking out desired reflog entry set head
commit.
every time head modified there new entry in reflog
git reflog git checkout head@{...}
this desired commit
git reset head --hard <commit_id>
"move" head desired commit.
# destroy local modifications. # don't if have uncommitted work want keep. git reset --hard 0d1d7fc32 # alternatively, if there's work keep: git stash git reset --hard 0d1d7fc32 git stash pop # saves modifications, reapplies patch after resetting. # merge conflicts, if you've modified things # changed since commit reset to.
- note: (since git 2.7)
can usegit rebase --no-autostash
well.
git revert <sha-1>
"undo" given commit or commit range.
reset command "undo" changes made in given commit.
new commit undo patch commited while original commit remain in history well.
# add new commit undo of original one. # <sha-1> can commit(s) or commit range git revert <sha-1>
this schema illustrate command what.
can see there reset && checkout
modify head
.
Comments
Post a Comment