是否有可能要求git diff在其diff输出中包括未跟踪的文件,或者我最好的选择是使用git添加新创建的文件和我编辑过的现有文件,然后使用:

git diff --cached

?


当前回答

通常,当我与远程位置团队一起工作时,在我遵循git阶段untrack- > staging ->commit之前,我事先了解其他团队在同一个文件中所做的更改对我来说很重要 为此,我写了一个bash脚本,这有助于我避免不必要的解决合并冲突与远程团队或使新的本地分支,并比较和合并在主分支

#set -x 
branchname=`git branch | grep -F '*' |  awk '{print $2}'`
echo $branchname
git fetch origin ${branchname}
for file in `git status | grep "modified" | awk "{print $2}" `
do
echo "PLEASE CHECK OUT GIT DIFF FOR "$file 
git difftool FETCH_HEAD $file ;
done

在上面的脚本中,我获取远程主分支(不需要它的主分支)到FETCH_HEAD,它们只列出我修改过的文件,并将修改过的文件与git difftool进行比较

这里git支持许多difftools。我配置'Meld Diff查看器'为良好的GUI比较。

其他回答

我相信您可以通过简单地提供两个文件的路径来区分索引文件和未跟踪文件中的文件。

git diff --no-index tracked_file untracked_file
git add -A
git diff HEAD

生成补丁,如果需要,然后:

git reset HEAD

使用git stash Hack:

# Stash unstaged changes
git stash --keep-index --include-untracked --message="pre-commit auto-stash"
git stash show --only-untracked stash@{0}
git stash pop

我需要在使用git stash(即预提交git钩子)的脚本上下文中使用这个。以下是我的完整工作示例: (在macOS Big Sur上的git v2.34.1上编写/测试)

# Stash unstaged changes
# NOTE: we always create a stash - possibly even a totally empty one.
git stash --keep-index --include-untracked --message="pre-commit auto-stash"
diffTracked=$(git diff --stat --staged stash@{0})
diffUntracked=$(git stash show --only-untracked stash@{0})
[[ $diffTracked || $diffUntracked ]] && {
  echo "Stashed diff:"
  # Ensure diffs have standard coloring:
  git diff --stat --staged stash@{0}
  git stash show --only-untracked stash@{0}
}

通常,当我与远程位置团队一起工作时,在我遵循git阶段untrack- > staging ->commit之前,我事先了解其他团队在同一个文件中所做的更改对我来说很重要 为此,我写了一个bash脚本,这有助于我避免不必要的解决合并冲突与远程团队或使新的本地分支,并比较和合并在主分支

#set -x 
branchname=`git branch | grep -F '*' |  awk '{print $2}'`
echo $branchname
git fetch origin ${branchname}
for file in `git status | grep "modified" | awk "{print $2}" `
do
echo "PLEASE CHECK OUT GIT DIFF FOR "$file 
git difftool FETCH_HEAD $file ;
done

在上面的脚本中,我获取远程主分支(不需要它的主分支)到FETCH_HEAD,它们只列出我修改过的文件,并将修改过的文件与git difftool进行比较

这里git支持许多difftools。我配置'Meld Diff查看器'为良好的GUI比较。

对于一个文件:

git diff --no-index /dev/null new_file

对于所有新文件:

for next in $( git ls-files --others --exclude-standard ) ; do git --no-pager diff --no-index /dev/null $next; done;

别名:

alias gdnew="for next in \$( git ls-files --others --exclude-standard ) ; do git --no-pager diff --no-index /dev/null \$next; done;"

对于所有修改和新文件组合为一个命令:

{ git --no-pager diff; gdnew }