Using git flow I create a new branch, newFunction
, off of develop
branch.
I add newFunction to example class:
class ExampleClass
{
public function exampleFunction(){
return "example";
}
public function newFunction(){
return "new";
}
}
Lets say I merge it to develop
, I come back a few months later and my class looks like this.
class ExampleClass
{
public function exampleFunction(){
return "example";
}
public function anotherFunction(){
return "another";
}
public function yetAnotherFunction(){
return "yetAnother";
}
public function newFunction(){
return "new";
}
}
Is it possible to apply the diff
between develop
and newFunction
at that point in time? Possibly using git patch
or some sort of black magic.
So I run a magical git something
command and I end up with something like this:
class ExampleClass
{
public function exampleFunction(){
return "example";
}
public function anotherFunction(){
return "another";
}
public function yetAnotherFunction(){
return "yetAnother";
}
public function newFunction(){
return "new";
}
public function newFunction(){
return "new";
}
}
and if I did it again, I would get this:
class ExampleClass
{
public function exampleFunction(){
return "example";
}
public function anotherFunction(){
return "another";
}
public function yetAnotherFunction(){
return "yetAnother";
}
public function newFunction(){
return "new";
}
public function newFunction(){
return "new";
}
public function newFunction(){
return "new";
}
}
etc. I am aware that the code it currently generates is incorrect, as in the functions have the same name. I would use this for generation of boilerplate code in a legacy system, for example.
Answer
I think the command you are looking for is git apply. Assuming that the last commit on the newFunction branch only adds the four lines for newFunction you could apply the same change on the develop branch with the following commands:
Git apply will here attempt to apply the same change that was done in the last commit on the newFunction branch onto the develop branch.
When testing with a test repository adding one function for each commit, the above git apply did not apply cleanly. But when using three way mode it will leave the result open for manual resolve, e.g.
git status
will showboth modified
for the file andgit ls-files -u
will show something likeAdditional tip on handling merge conflicts
The default git behaviour is to insert some text markers for the merge conflicts part which I find very hard to work with. I much rather like to merge graphically using KDiff3, and armed with the output from ls-files we can fetch all the relevant versions and create files with corresponding content which we run kdiff3 on. E.g.
With two manual diff alignments it is not difficult to resolve:
Personally I have created a script that at its core does the above commands to resolve git conflicts, although a bit more sophisticated, and I use it all the time, I love kdiff3.