是否有可能要求git diff在其diff输出中包括未跟踪的文件,或者我最好的选择是使用git添加新创建的文件和我编辑过的现有文件,然后使用:
git diff --cached
?
是否有可能要求git diff在其diff输出中包括未跟踪的文件,或者我最好的选择是使用git添加新创建的文件和我编辑过的现有文件,然后使用:
git diff --cached
?
当前回答
对于我的交互式日常获取(我一直在根据HEAD对工作树进行差异,并且希望在diff中包含未跟踪的文件),add -N/——intent-to-add是不可用的,因为它破坏了git stash。
这是我的git diff替换。这不是一个特别干净的解决方案,但因为我真的只是交互地使用它,所以我可以接受一个hack:
d() {
if test "$#" = 0; then
(
git diff --color
git ls-files --others --exclude-standard |
while read -r i; do git diff --color -- /dev/null "$i"; done
) | `git config --get core.pager`
else
git diff "$@"
fi
}
只输入d将包括diff中未跟踪的文件(这是我在我的工作流中关心的),d参数…将表现得像普通的git差异。
注:
We're using the fact here that git diff is really just individual diffs concatenated, so it's not possible to tell the d output from a "real diff" -- except for the fact that all untracked files get sorted last. The only problem with this function is that the output is colorized even when redirected; but I can't be bothered to add logic for that. I couldn't find any way to get untracked files included by just assembling a slick argument list for git diff. If someone figures out how to do this, or if maybe a feature gets added to git at some point in the future, please leave a note here!
其他回答
使用可以暂存新文件和区分暂存文件的想法,您可以将这两者结合起来查看差异,我发现它使用起来很简单。
添加您想要查看差异的文件。在您的情况下,只添加未跟踪的文件。您可以选择只添加那些想要查看差异的文件。 Git stash && Git添加。git隐藏pop 不同的阶段 Git diff——阶段性的 如果需要,重置暂存文件 git重置
综上所述,
git stash && git add . && git stash pop && git diff --staged && git reset
使用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 diff origin/master
对于我的交互式日常获取(我一直在根据HEAD对工作树进行差异,并且希望在diff中包含未跟踪的文件),add -N/——intent-to-add是不可用的,因为它破坏了git stash。
这是我的git diff替换。这不是一个特别干净的解决方案,但因为我真的只是交互地使用它,所以我可以接受一个hack:
d() {
if test "$#" = 0; then
(
git diff --color
git ls-files --others --exclude-standard |
while read -r i; do git diff --color -- /dev/null "$i"; done
) | `git config --get core.pager`
else
git diff "$@"
fi
}
只输入d将包括diff中未跟踪的文件(这是我在我的工作流中关心的),d参数…将表现得像普通的git差异。
注:
We're using the fact here that git diff is really just individual diffs concatenated, so it's not possible to tell the d output from a "real diff" -- except for the fact that all untracked files get sorted last. The only problem with this function is that the output is colorized even when redirected; but I can't be bothered to add logic for that. I couldn't find any way to get untracked files included by just assembling a slick argument list for git diff. If someone figures out how to do this, or if maybe a feature gets added to git at some point in the future, please leave a note here!
通常,当我与远程位置团队一起工作时,在我遵循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比较。