Re-attaching a git HEAD

Print Friendly, PDF & Email

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)

  1. 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)

  2. 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)

  3. Delete the temp branch
    git branch -d temp
    
  4. 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