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 ;)
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.
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.
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.
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.
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.
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.
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.
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.
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.
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]".
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 findsRelated 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With