16 June 2017 by Lars
Re-attaching a git HEAD
Sometimes, it is necessary to re-attach a git clone to the HEAD of another (remote) instance. Since I never know, here’s the quick solution (via Stackoverflow/Louis Maddox)
- create a branch that points to the commit currently pointed to by your detached HEAD:
git branch temp git checkout temp
(these two commands can be abbreviated as
git checkout -b temp
) - then update (e.g.) master to point to it:
git branch -f master temp git checkout master
(these two commands can be abbreviated as git checkout -B master temp)
- Delete the temp branch
git branch -d temp
- Finally, you will probably want to push the reestablished history:
git push origin master
All in one place for quick copy-and-paste:
git checkout -b temp
git checkout -B master temp
git branch -d temp
git push origin master