Re-attaching detached git commits

Published on 18th Jun, 2018 by David Young

Today I had an interesting git problem. I had been setting up a central git repository for my dotfiles (.zshrc, .vimrc, etc.) and started off by putting the files from three separate computers into separate branches. Somehow in all this one of the branches ended up in a detached state. While I could have just deleted the branch and recommitted the files, that would have been boring and I wouldn't have been able to get better at git.

After testing a few methods using rebase and merge, I discovered that the easiest way of getting the histories back in line was via a temporary branch, but none of the websites I'd looked at had explained this at all well. Unfortunately, I didn't take a screenshot of the commit graph in its floaty state, but after running a few commands we were in line again.

git checkout -b temp
git merge origin/priestess --allow-unrelated-histories
vim .gitignore
git add .gitignore
git commit -m "Merge in changes from priestess branch"
git checkout priestess
git merge temp

Commit graph after creating a temp branch and merging in the floating commits:

Commit graph after creating a temp branch and merging in the floating commits

Priestess branch has been fast-forwarded to the tip of temp:

Priestess branch has been fast-forwarded to the tip of temp

One of the reasons this was reasonably complicated was because both master and priestess touched the .gitignore file, so in the middle of the merge I had to edit the merged version and commit it: vim.png