簡體   English   中英

git - 提交中所有已更改但未刪除的文件的列表

[英]git - list of all changed but not deleted files in a commit

我正在調查 git 存儲庫。 我想在每次提交中獲取更改文件的列表。

所以,我所做的是我通過命令訪問了每個提交

git reset --hard <commitId>

然后,我用

git show --pretty="format:" --name-only #{commitId}

這個問題是它在那個 commitId 中提供了我不想要的已刪除文件。 我也試過:

git ls-files 

它不會返回已刪除的文件,但是,它會返回所有現有文件的列表,這些文件是新的或在先前提交中創建的。

例子:

>commit 1
add "file1"
add "file2"

>commit 2
change "file1"

>commit 3
add "file3"
delete "file2"

所以在這種情況下,我將訪問每個提交。 而且,如果我在提交 1 中,我想獲得“file1”和“file2”的列表。 如果我在提交 2 中,我將獲得“file1”,如果我在提交 3 中,我將獲得“file3”。

任何想法?

嘗試使用此命令:

git show --diff-filter=AM --pretty="format:" --name-only #{commitId}

這是您在原始問題中提到的,添加了--diff-filter標志以限制僅添加( A )或修改( M )的文件。 有關您可以限制的文件類型的完整列表,請查看git show的文檔。

正如@MauricioTrajano 在他的回答中提到的那樣,您無需重置為提交即可使用git show對其進行調查。 您只需要知道提交的 SHA-1 哈希,您只需在相關分支上使用git log即可找到它。

您不必將 HEAD 重置為較早的提交來執行此操作,即您不必丟失本地更改,只要您提供提交哈希, git diff就允許您在任何兩個提交之間進行比較。 在您的情況下,您可以執行以下操作:

git diff {COMMIT_1_HASH} {COMMIT_2_HASH} --name-only --diff-filter=AM

這可用於查看在任意數量的提交之間添加和更改的文件。

如果您只想為一次提交添加和修改文件,那么只需使用您要查看的提交哈希之后的提交哈希。

如何通過--diff-filter=使用git diff過濾器

要獲取所有已更改文件的列表,不包括所有已刪除(通過小寫d過濾器)文件,請使用--diff-filter=d ,如下所示:

# 1. see list of changed, but NOT 'd'eleted files since `from_commit_hash` to
# now
git diff --name-only --diff-filter=d from_commit_hash

# 2. specifying a range of commits:
git diff --name-only --diff-filter=d from_commit_hash to_commit_hash
# or (same thing)
git diff --name-only --diff-filter=d from_commit_hash..to_commit_hash

# 3. use the 3-dot syntax to look at just the changes since the common ancestor
# between `from_commit_hash` and `to_commit_hash`
# See: https://stackoverflow.com/a/7252067/4561887
git diff --name-only --diff-filter=d from_commit_hash...to_commit_hash
# or (same thing)
git diff --name-only --diff-filter=d \
    $(git merge-base from_commit_hash to_commit_hash) to_commit_hash

# 4. looking at all changes in `commit2` which are NOT in `commit1`
git diff --name-only --diff-filter=d ^commit1 commit2

您可以通過使用--name-status查看文件名及其更改狀態來確認更改文件的完整列表:

# 1. 
git diff --name-status from_commit_hash

# 2. specifying a range of commits
git diff --name-status from_commit_hash to_commit_hash
# or (same thing)
git diff --name-status from_commit_hash..to_commit_hash

# etc. etc.

請注意,我認為--diff-filter=d--diff-filter=AM更好,因為前者包括'd'eleted 文件之外的所有內容,而后者排除'A'added 和'修改過的文件。 因此,前者更具包容性,僅排除您真正想要排除的內容,我認為它會更好。

--diff-filter=中,大寫過濾器字母INCLUDE寫過濾器字母排除

使用大寫字母(例如DAM等)包含該類型的文件,使用小寫字母(例如dam等)分別排除該類型的文件。

因此,以下情況屬實:

--diff-filter=AM  # INCLUDE only Added and Modified files
# vs
--diff-filter=am  # EXCLUDE all added and modified files

man git diff (強調添加):

--diff-filter=[(A|C|D|M|R|T|U|X|B)...[*]]

僅選擇已添加 ( A )、已復制 ( C )、已刪除 ( D )、已修改 ( M )、重命名 ( R )、類型(即常規文件、符號鏈接、子模塊...)已更改 ( T ) 的文件, 未合並 ( U ), 未知 ( X ), 或它們的配對已損壞 ( B )。 可以使用過濾字符的任意組合(包括無)。 在組合中添加* (All-or-none)時,如果在比較中存在與其他條件匹配的任何文件,則選擇所有路徑; 如果沒有符合其他條件的文件,則不選擇任何內容。

此外,這些大寫字母可以小寫以排除. 例如d --diff-filter=ad排除添加和刪除a路徑。

請注意,並非所有差異都可以包含所有類型。 例如,如果禁用對這些類型的檢測,則無法顯示復制和重命名的條目。

參考

  1. 請參閱此處git diff --diff-filter=文檔或使用man git diff
  2. 另請參閱此特定答案: 如何獲取在兩次 Git 提交之間更改的所有文件的列表?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM