Git one-liner用于交互式应用补丁

时间:2016-01-21 19:20:49

标签: git git-add git-patch git-apply

我有补丁文件,它包含一系列我想要应用于git分支的修改。但我不希望对所有这些修改做出一个唯一的提交,而是我想将其拆分为2或3次提交

我知道我可以通过首先应用补丁,然后执行交互式添加(hunk by hunk)来实现这一点,如下所示:

git apply mypatch
git add -p

我只是想知道是否可以在一个git命令中执行此操作。 我在git applygit add的联机帮助页中找不到任何内容。

修改

我不认为这个问题应该被视为Syntax for Git aliases with multiple commands的副本,因为该问题(及其答案)不需要涉及传递给别名的参数。

3 个答案:

答案 0 :(得分:2)

由于@ 8bittree的评论,我回答了我自己的问题。

至少有两种方法可以做到:

  • 使用 shell函数see 8bittree's answer
  • 使用 git别名。我更喜欢这个解决方案,因为它只涉及.gitconfig,无需修改.bashrc,而git shell 完成适用于新别名,它与其他标准git命令一起显示完成清单

所以这就是我要添加到全球.gitconfig

的结果
[alias]
    # interactive apply patch
    ipatch = "!f() { git apply $1; git add -p; }; f"

然后我必须这样做:

git ipatch mypatchfile

答案 1 :(得分:1)

正如我的评论中所提到的,你可以通过一个函数获得类似的效果。经过一番思考之后,我不确定别名是否有效,因为您需要指定补丁文件以及可能的一些选项。这是您可以添加到.bashrc的示例函数,如果您使用Bash以外的shell,则可能需要进行调整。我认为它应该适用于Zsh,也许是Ksh,但我还没有用它们进行测试。

function git() {
    # Allows you to call `git ipatch patchfile`
    # Change ipatch to whatever you want the git command to be
    if [[ "$1" = ipatch ]]; then
        shift
        # Specify the full path to git, otherwise infinite recursion
        # Alternatively, name the function something else
        /usr/bin/git apply "$@"
        /usr/bin/git add -p
    else
        /usr/bin/git "$@"
    fi
}

如果您想添加更多自定义git命令,可能需要查看case statement,而不是长链if

答案 2 :(得分:-1)

你可以使用unix"补丁"命令(与git分开)。这样Git就不会对“补丁”有任何了解。 --- Git会认为你做了常规编辑。输出" git show"和" git diff"与unix" patch"兼容命令:

# assuming "my-git-repo" is clean...
cd my-git-repo
patch -p1 < my.patch
git status

此时你可以使用&#34; git add&#34; /&#34; git commit&#34;创建你想要的提交序列。

相关问题