Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git; code disappeared after merge

Tags:

git

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

Using 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 Stack Overflow 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. The question is - how do I get that code back? Is there another 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 ;)

like image 980
Vonsild Avatar asked Jul 08 '13 12:07

Vonsild


People also ask

Does branch disappear after merging?

In a good workflow, the feature branch is deleted once its merged back into master. New branches should be created for each new feature(s) that you work on.

What happens to git branch after merge?

When you perform a merge, you effectively merge one branch into another—typically a feature branch or bug fix branch into a main branch such as master or develop. Not only will the code changes get merged in, but also all the commits that went into the feature branch.

What is -- no FF in git merge?

The Git merge --no-ff command merges the specified branch into the command in the current branch and ensures performing a merge commit even when it is a fast-forward merge. It helps in record-keeping of all performed merge commands in the concerning git repo.

What happens when merging two branches?

Merging Branches. Once you've completed work on your branch, it is time to merge it into the main branch. Merging takes your branch changes and implements them into the main branch. Depending on the commit history, Git performs merges two ways: fast-forward and three-way merge.

What does the merge command do in Git?

Now our computer knows about the latest master version and the latest feature_merge version, which means that we can finally run the merge command This command tells git to merge whichever branch we are currently in ( feature_merge) with master.

How do I recover a merged branch in Git?

You’ll check out a new branch named recover-branch, based off the SHA where the pull request was merged. Push this to the remote with git push -u origin recover-branch, and your branch is back, both locally and remotely.

How to recover lost commits in Git?

commands to recover your lost commits in Git. Note: Using the reflog will only work for a certain amount of time after the commits are lost. Git cleans the reflog periodically, so don’t wait too long! The first step to recovering your lost commits is to recover the list of all your previous commits and actions done on the repository.

Why is Git not letting me checkout the master branch?

Before you run this command, make sure that you have pushed the latest code in your current branch ( feature_merge in this example) otherwise Git won’t let you checkout the master branch. All this line of code does is to get the code from the master branch into your computer.


5 Answers

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 had 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.

like image 191
Schleis Avatar answered Oct 22 '22 19:10

Schleis


Changes which were committed at some point are hard to lose completely. Try running 'git reflog' and see if you can spot the commit you've lost. Then you can merge it into the current branch by running "git merge [SHA-1 hash value of your lost commit]".

like image 26
alexakarpov Avatar answered Oct 22 '22 21:10

alexakarpov


If you know an identifiable section of the code which has been lost, you may be able to find the exact merge which lost your code by using git log -G regexToSearchFor -m --patch

  • -G <regex> tells Git to "Look for differences whose patch text contains added/removed lines that match <regex>"
  • -m tells Git to include merge commits
  • --patch tells Git to show a diff of whatever it finds
like image 10
alxndr Avatar answered Oct 22 '22 21:10

alxndr


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

Try git whatchanged. For instance,

git whatchanged --since="2 weeks ago" -- gitk

Show the changes during the last two weeks to the file gitk.

like image 6
Prince John Wesley Avatar answered Oct 22 '22 20:10

Prince John Wesley


Apparently (see this question), git show is not the right tool to use to have a clear view of the changes introduced by a merge commit.

Use git diff. The merge commit has two parents:

*   8ac6131 (M) Merge branch 'B'
|\
| * 5a53959 (B) two
|
* 7cb5a06 (A) one

Use git diff A M and git diff B M to view the differences between the merge commit and either of its parent. You should see your missing function there.

like image 5
LeGEC Avatar answered Oct 22 '22 19:10

LeGEC