简体   繁体   English

如何“git ls”文件被git标记为“由我们删除”

[英]Howto "git ls" files marked by git as "deleted by us"

I started to use git cherrypick;我开始使用 git cherrypick; and as a result of that I am now (very often) facing situations that give me a lot of files marked as "deleted by us".因此,我现在(经常)面临这样的情况,这些情况给了我很多标记为“被我们删除”的文件。

I saw this other question , that mentioned how can use git ls-files --deleted to get a flat file list;我看到了另一个问题,其中提到了如何使用git ls-files --deleted来获取平面文件列表; which can then be piped to xargs rm.然后可以通过管道传输到 xargs rm。

Problem is: --deleted doesn't list those "deleted by us" files.问题是:--deleted 没有列出那些“被我们删除”的文件。

So, long story short: what is the easiest/straight forward way of removing the "deleted by us" files?所以,长话短说:删除“被我们删除”文件的最简单/直接的方法是什么?

( I really liked the git ls-files approach; as that doesn't require sed/awk magic; so there is also no worrying about getting quotes right ...) (我真的很喜欢 git ls-files 方法;因为它不需要 sed/awk 魔法;所以也不必担心正确使用引号......)

Update;更新; just to explain why this not some "XY problem" situation:只是为了解释为什么这不是一些“XY 问题”的情况:

  • actually I am using git svn;实际上我正在使用 git svn; to connect to some backend SVN server连接到一些后端 SVN 服务器
  • SVN trunk contains directories A, B, C SVN 主干包含目录 A、B、C
  • I am making changes on trunk that affect all three directories我正在对影响所有三个目录的主干进行更改
  • But: the git where I run the cherry-pick ... actually resembles "product branches" created on the SVN server.但是:我运行cherry-pick的git ...实际上类似于在SVN服务器上创建的“产品分支”。 These "product" branches do only contain subsets (so one has A and B; the other has B and C).这些“产品”分支只包含子集(所以一个有 A 和 B;另一个有 B 和 C)。
  • I tried hard;我努力了; but I really couldn't get to create a git svn clone that contains SVN trunk AND SVN product branches at the same time但我真的无法同时创建一个包含 SVN 主干和 SVN 产品分支的 git svn clone

I don't know of a "simple" command like that for this but you can use git status itself for this.我不知道像这样的“简单”命令,但你可以使用git status本身。

while read -d '' status file; do
    case "$status" in
        (D?)
            echo "Removing $file that was deleted by us."
            #rm "./$file"
            ;;
    esac
    #printf 'status: %q\nfilename: %q\n' "$status" "$file"
done < <(git status -sz)

To match only DU and not D?只匹配DU而不匹配D? (D single-character) change the case pattern appropriately. (D 单字符) 适当更改大小写模式。

To run rm only once use files+=("$file") in the if block and then rm "${files[@]}" at the end.要仅运行rm一次,请在if块中使用files+=("$file") ,然后在最后使用rm "${files[@]}"

To list unmerged files marked as "deleted by us" the following command can be used:要列出标记为“由我们删除”的未合并文件,可以使用以下命令:

git status --porcelain --untracked-files=no | sed --silent 's/^DU //p'

or a slightly shorter variant:或稍短的变体:

git status --porcelain -uno | sed -n 's/^DU //p'

In the output of the command git status --porcelain the unmerged "deleted by us" files are marked with DU .在命令git status --porcelain的输出中, git status --porcelain合并的“被我们删除”文件用DU标记。

In sed , the option -n suppresses printing unless explicit requested, the command s/X/Y/ substitute X (a regexp) by Y, and due to the flag p the modified line is printed.sed ,除非明确要求,否则选项-n禁止打印,命令s/X/Y/s/X/Y/替换 X(正则表达式),并且由于标志p会打印修改后的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM