Git:stage只添加了文件的行

Git:stage只添加了文件的行,git,Git,我正在运行一个脚本,该脚本从文本文件(表示树)生成代码(字符串常量) 运行脚本将删除代码库中其他地方使用的常量 我不想提交已删除的行(因为它会破坏其他团队的代码),而是将这些常量标记为已弃用 有没有办法在我生成的文件中只放置“添加”的行 我可以用git add-p来做,但是有点乏味,因为我有几个文件,有很多更改 输入差异: const string Namespace1::kProperty1 = "/namespace_1/property_1"; + const string Namespa

我正在运行一个脚本,该脚本从文本文件(表示树)生成代码(字符串常量)

运行脚本将删除代码库中其他地方使用的常量

我不想提交已删除的行(因为它会破坏其他团队的代码),而是将这些常量标记为已弃用

有没有办法在我生成的文件中只放置“添加”的行

我可以用git add-p来做,但是有点乏味,因为我有几个文件,有很多更改

输入差异:

const string Namespace1::kProperty1 = "/namespace_1/property_1";
+ const string Namespace1::kProperty2 = "/namespace_1/property_2";

const string Namespace2::kProperty3 = "/namespace_2/property_3";
- const string Namespace2::kProperty2 = "/namespace_2/property_2";
预期提交差异:

const string Namespace1::kProperty1 = "/namespace_1/property_1";
+ const string Namespace1::kProperty2 = "/namespace_1/property_2";

const string Namespace2::kProperty3 = "/namespace_2/property_3";
const string Namespace2::kProperty2 = "/namespace_2/property_2";

没有一种工具可以做到这一点,但有几种工具可以协同工作

  • 包含在标准Python库中,并生成比
    diff
    命令更易于使用的输出
  • git cat file blob…
    从git数据库检索内容,而不修改工作树

  • 如果您没有使用类似shell的Bash来执行
    ,那么您将需要一个图形客户端。除了
    git add-p
    之外,没有好的内置选项,除了手动编辑删除的行。如果你有一个很好的diff程序,比如Beyond Compare,它支持编辑,并设置了一个新的未修改副本,你可以使用diff程序将新副本与你的工作文件夹进行比较,并带回删除的每一行。此外,在我看来,这是一个更好的选择。如果你不应该删除这些文件中的内容,请修复你的生成脚本,这样它就不会这样做。谢谢,你为我指明了正确的方向。但是您需要打印diff中的所有行,因为我还需要删除的行。另外,如果您想同时生成“不推荐的”注释,那么python的使用非常方便。
    git update-index --cacheinfo 100644,$(python -c '
        import difflib, sys;
        sys.stdout.writelines(
            line[2:] for line in difflib.Differ().compare(
                open(sys.argv[1]).readlines(), open(sys.argv[2]).readlines())
            if line.startswith("  ") or line.startswith("+ "))' \
        <(git cat-file blob HEAD:filename) filename \
    | git hash-object -t blob -w --stdin --path filename),filename