B T B T - 8 months ago 28
Git Question

How can I fix my git history after accidentally deleting a file and adding it back in a different location?

Here's my situation: I moved a file to a new location, but accidentally only committed the deletion. Then in a subsequent commit, I committed the new file in the new location.

git log --follow
seems to not detect that the file was moved, and so doesn't show me the full history for that file now.

I then tried to fix this by moving the file back to its original location, but it still has its truncated history now lol, repair fail.

How can I now fix this file to make git detect the history chain?


You need to merge the two commits together:

git reset --soft "HEAD^"
git commit --amend

Or you can go back to the previous commit completely, redo the stuff and amend the commit:

git reset --hard "HEAD^"
# create the file, or move, ...
git commit --amend

At the end, if you already pushed to remote, you need to use -f to overwrite the remote history:

git push origin -f