简体   繁体   中英

Howto "git ls" files marked by git as "deleted by us"

I started to use 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; which can then be piped to xargs rm.

Problem is: --deleted doesn't list those "deleted by us" files.

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

Update; just to explain why this not some "XY problem" situation:

  • actually I am using git svn; to connect to some backend SVN server
  • SVN trunk contains directories 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. These "product" branches do only contain subsets (so one has A and B; the other has B and 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

I don't know of a "simple" command like that for this but you can use git status itself for this.

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? (D single-character) change the case pattern appropriately.

To run rm only once use files+=("$file") in the if block and then rm "${files[@]}" at the end.

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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