簡體   English   中英

如何顯示在 git 工作樹中修改的分支上更改的文件

[英]How to show files changed on a branch that are modified in the git working tree

我需要僅 Linux shell 管道或單個腳本,以顯示我已在本地修改(暫存或未暫存)的文件列表,這些文件已提交到某個不同的分支,調用該topic-branch ,而無需先提交我的更改到任何分支。 通常, topic-branch將是master ,但並非總是如此。 當前的 git 存儲庫可能會也可能不會檢出到topic-branch中。

理想情況下,上述內容應表示為單個git 命令,我將分支名稱傳遞給它,並以該命令發出的格式輸出列表:

git ls-files --full-name

我不是要求比較已經檢查到兩個單獨分支的內容,因為已經有答案了。

這是我現在得到的答案,它可以工作:我將以下腳本添加到我的$PATH目錄中,名為git-cfm ,賦予它執行權限,然后可以通過簡單地調用它:

git cfm

但是,這不是我正在尋找的解決方案,因為說服我的開發人員在他們的啟動腳本( ~/.bashrc~/.cshrc等)中修改他們的$PATH以包含中央會產生社會負擔git-cfm文件所在的目錄。 奇怪的是,這在技術上是可行的,但在我的社會背景下在社會學上是困難的:

#!/bin/bash

# git-cfm: <c>hanged <f>iles on <m>aster
#
# List files that have been changed on $branch, that are
# also locally modified (or staged for commit).
# 
# This could be simplified by using, say, Python.
#

# Default $branch to master
branch=${1:-master}

(
  # Get the status using -s which shows both unstaged and staged files
  # in a line-sequential output format, rip off the first column using
  # cut, and then call git-ls-files multiple times on each one to get
  # the full-name of each of them (e.g., a path relative to whatever
  # directory would be returned by "git rev-parse --show-toplevel") .
  # Use the -r option on the call to git-ls-files to avoid calling
  # git-ls-files _unless_ there is actual output from the command,
  # because without the -r, git-ls-files will dump ALL of the files
  # relative to the current working directory, which is NOT what we
  # want.  What we want is the full path name, relative to the
  # top-level directory of the working tree. :
  git status -s 2>/dev/null | cut -c4- | xargs -n100 -r git ls-files --full-name

  # In addition to the above possibly empty output, get all files from
  # all commits on the master branch, that are not changed from
  # commits on the current branch.  Uniquify the output, because of
  # commits that change the same file will result in duplicate files
  # being listed. The empty --pretty option prevents git log default
  # output to include the commit hash, and instead only give us the
  # filenames by using the --name-only option. This comes out as
  # "full-path"'s as indicated above, and is the reason for the
  # --full-name above, too:
  git log --pretty= --name-only @..master | sort | uniq

  # Explanation of the subsequent pipeline below: The file sort and
  # uniq -c emits lines that give counts of occurances in the input.
  # Any occurrances greater than 1 are the ones that we want, so we
  # use awk to show only those. This is just set-theory (AND
  # operation) of the two sets (one set of files per pipeline above)
  # above:
) | sort | uniq -c | awk '{ if ($1 != 1) { print $2; } }'   

暫無
暫無

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

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