多次应用git diff/patch [英] Apply git diff/patch multiple times

查看:93
本文介绍了多次应用git diff/patch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用git flow,我在develop分支之外创建了一个新分支newFunction. 我将newFunction添加到示例类:

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";
    }

}

让我们说我将其合并到develop,几个月后我回来了,我的课看起来像这样.

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";
    }

}

是否可以在该时间点在developnewFunction之间应用diff?可能使用git patch或某种黑魔法. 因此,我运行了一个神奇的git something命令,最终得到了这样的内容:

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.

推荐答案

答案

我认为您要查找的命令是 git apply .假设newFunction分支上的最后一次提交仅添加了newFunction的四行,则可以使用以下命令在develop分支上应用相同的更改:

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 checkout develop
git diff newFunction^ newFunction | git apply --3way
# "newFunction^" means the second last commit on newFunction branch

Git apply在这里将尝试将对newFunction分支的最后一次提交所做的更改应用于开发分支.

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.

在测试存储库中为每次提交添加一个功能的测试时,上述git apply并非完全适用.但是当使用三向模式时,它将使结果保持打开状态以进行手动解析,例如git status将显示文件的both modified,而git ls-files -u将显示类似的文件

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 show both modified for the file and git ls-files -u will show something like

100644 21ecaba537582661c82c9dd2dff920a88a4b69a1 1   file.php
100644 56728941868d309cc06a49a8c49afc72cb0285b7 2   file.php
100644 0bbcab178802667c0228e424ce84f4c13a2b4c94 3   file.php

关于处理合并冲突的其他提示

默认的git行为是为合并冲突部分插入一些文本标记,而我发现这些标记很难使用.我更喜欢使用 KDiff3 图形化合并,并使用ls文件的输出进行提取,相关版本,并创建具有相应内容的文件,然后在该文件上运行kdiff3.例如

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

$ git cat-file blob 21ecaba537582661c82c9dd2dff920a88a4b69a1 > file1.php
$ git cat-file blob 56728941868d309cc06a49a8c49afc72cb0285b7 > file2.php
$ git cat-file blob 0bbcab178802667c0228e424ce84f4c13a2b4c94 > file3.php
$ kdiff3 -o file_merged.php file1.php file2.php file3.php
$ mv file_merged.php file.php
$ git add file.php
$ git commit -m "Manually resolved git apply result"

有了两个手动的diff对齐,解决起来并不困难:

With two manual diff alignments it is not difficult to resolve:

我个人创建了一个脚本,该脚本的核心是执行上述命令来解决git冲突,尽管稍微复杂一些,而且我一直使用它,我喜欢kdiff3.

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.

这篇关于多次应用git diff/patch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆