20

以下は簡単なスニペットの例です (Linux ターミナルに貼り付けることができます)。新しいgitリポジトリを作成し、いくつかのファイルを追加します (git バージョン 1.7.9.5 を使用):

cd /tmp/
mkdir myrepo_git
cd myrepo_git/
git init
git config user.name "Your Name"
git config user.email you@example.com
echo "test" > file_tracked_unchanged.txt
echo "test" > file_tracked_changed.txt
echo "test" > file_untracked.txt
git add file_tracked_unchanged.txt 
git add file_tracked_changed.txt
git commit -m "initial commit"

ここで、最初のコミットの後、ファイルを変更し、次のコミットのためfile_tracked_changed.txtに他のファイル (ここでは のみfile_tracked_unchanged.txt) を変更しないようにします。以下はそれを示すスニペットで、git statusvsのさまざまな出力git ls-files(gitシェル出力の前に が付きます#):

echo "test more" >> file_tracked_changed.txt

git status -uno
# # On branch master
# # Changes not staged for commit:
# #   (use "git add <file>..." to update what will be committed)
# #   (use "git checkout -- <file>..." to discard changes in working directory)
# #
# # modified:   file_tracked_changed.txt
# #
# no changes added to commit (use "git add" and/or "git commit -a")
git status
# # On branch master
# # Changes not staged for commit:
# #   (use "git add <file>..." to update what will be committed)
# #   (use "git checkout -- <file>..." to discard changes in working directory)
# #
# # modified:   file_tracked_changed.txt
# #
# # Untracked files:
# #   (use "git add <file>..." to include in what will be committed)
# #
# # file_untracked.txt
# no changes added to commit (use "git add" and/or "git commit -a")
git status -uno --short
#  M file_tracked_changed.txt
git status --short
#  M file_tracked_changed.txt
# ?? file_untracked.txt
git ls-files -v
# H file_tracked_changed.txt
# H file_tracked_unchanged.txt

git add file_tracked_changed.txt

git status -uno
# # On branch master
# # Changes to be committed:
# #   (use "git reset HEAD <file>..." to unstage)
# #
# # modified:   file_tracked_changed.txt
# #
# # Untracked files not listed (use -u option to show untracked files)
git status
# # On branch master
# # Changes to be committed:
# #   (use "git reset HEAD <file>..." to unstage)
# #
# # modified:   file_tracked_changed.txt
# #
# # Untracked files:
# #   (use "git add <file>..." to include in what will be committed)
# #
# # file_untracked.txt
git status -uno --short
# M  file_tracked_changed.txt
git status --short
# M  file_tracked_changed.txt
# ?? file_untracked.txt
git ls-files -v
# H file_tracked_changed.txt
# H file_tracked_unchanged.txt

私が探しているのは、ディレクトリ内のすべての追跡ファイルを表示するコマンドです(これは表示されます)。git ls-files -v正確なリポジトリ ステータス (すべての追跡ファイルのステータスとして表示されるためgit ls-files、表示されませんH)。たとえば、擬似コードのようなものを取得したいと思います:

git status-tracked
# M file_tracked_changed.txt
# . file_tracked_unchanged.txt

...ここで、ドット.は追跡されているが変更されていないファイルを示す記号になります(正しく思い出せば、SVNはUこれらに文字を使用する場合があります)。

最終的に、擬似コードのように、ディレクトリ内のすべてのファイルのステータスも表示したいと思います。

git status-tracked-and-untracked
# M file_tracked_changed.txt
# . file_tracked_unchanged.txt
# ?? file_untracked.txt

...しかし、上記の擬似のように、追跡されているすべてのファイルのステータスを取得することがより重要ですgit status-tracked

gitで、すでにこのようなことを行っているコマンドはありますか?

4

8 に答える 8

9

Linux で動作する簡単なワンライナー:

sort -uk 2bi <(git status -s) <(git ls-files | sed 's/^/ . /')

于 2020-01-13T18:58:10.893 に答える
7

ありがとう@sdaau。実行速度が大幅に向上し、結果が と同じ形式で表示されるように、いくつかの変更を加えましたgit status

git ls-files | while read -r line;
do
    st=$(git status -s "$line");
    if [ -n "$st" ]; then
        echo "$st";
    else
        echo "   $line";
    fi;
done
于 2014-03-25T22:28:40.197 に答える
3

@RogerDueckの回答に触発されて、それぞれ1回だけ実行するスクリプトを作成しましgit ls-filesgit status。私のレポでは、約 1700 個のファイルで約 15 倍速く、2 秒弱で実行されます。

編集:いくつかの修正といくつかの単体テストを追加し、GitHub に移動しました: https://github.com/frnhr/git-fullstatus

出力例:

 M some/file
D  another/file
 D more/files/blahblah
A  this/is/an/added/file/i/think
   an/unchanged_file
   another/unchanged_file
于 2015-07-04T14:08:29.287 に答える
0

未変更/未編集の追跡ファイル/パスのみを表示:

バッシュ/zsh

$ git ls-files | grep -vf <( git status -s | grep '^[^?]' | cut -c4- )
$ git ls-tree --name-status HEAD |
    grep -vf <( git status -s | grep '^[^?]' | cut -c4- )
  1. ls-files/ls-tree を grep にパイプします

  2. grep -v -f

    • -v除外/除外
    • -fで始まり、で終わるgit status -s | grep '^[^?]' | cut -c4-[ファイル記述子][1] として の出力を読み取ります<()
    • [^?]grep の最初の文字を?
    • cut -c4-4 番目の文字を行末までカット
$ time zsh -c " git ls-files |
    grep -v -f <( git status -s | grep '^[^?]' | cut -c4- )"
fileA
fileB
dirA/file1
zsh -c "git ls-files| grep -v -f <( git status -s | grep '^[^?]' | cut -c4- )"  0.02s user 0.03s system 69% cpu 0.073 total
$ time zsh -c "git ls-tree --name-status HEAD |
    grep -v -f <( git status -s | grep '^[^?]' | cut -c4- )"
fileA
fileB
zsh -c   0.02s user 0.03s system 93% cpu 0.057 total

(注: の後|の改行をエスケープしなくても問題ありませんが、エスケープするスタイルが望ましい場合は、喜んで編集します)
[1]:簡単に説明すると、ファイル記述子とは?

于 2021-09-14T23:12:27.437 に答える