Vonsild Vonsild - 1 year ago 77
Git Question

Git; code disappeared after merge

My colleague did a change a while back - introduced a new function - and that was (sucessfully) committed to git. Now, though, that function has gone missing.


git log --reverse
I've managed to find the last commit where that function was still in the code (48d60a03). The next (e6f28bfd) commit (where the function in question disappeared) is a merge (of 14158e1), but
git show
'ing any of these does not reveal a delete of the missing code.

In other words, code has disappeared during a merge, without being deleted in either of the branches being merged.

Searching StackOverflow for a couple of hours leads me to conclude, that it must be a manual error during a conflict resolution (did I get that right?). So be it, that happens. Question is - how do I get that code back - is there any other way than making a new commit with the missing code?

Related question; can I somehow find out, if there are other instances of stuff going bye-bye like this? I'm slightly worried ;)

Answer Source

Because your history has already been pushed, the best way is to make a new commit. Otherwise, you run the risk of losing other code and messing up everyone's repos.

Since you know where the commit that last has the function, you can git checkout 48d60a03 -- <name of file with function>. Then you can commit the old/new file with the function.

As there are likely to be other changes in the file, you will probably want to git reset to unstage the file and use git add -p to only add the changes for the function that you are looking for.

For preventing this from happening, my recommendation is to get a comprehensive test suite that you can run after completing a merge. That can help minimize the chances that code will be lost as tests will fail.