Git: 应用新文件的差异
Git: Apply Diff of New File
我有一个脚本,对于
中的每个 changed_file
"git diff --name-only master..origin/master"
该文件的差异应用如下:
git diff master..origin/master file_name | git apply.
对于任何新创建的文件(origin master 中的文件但不是最初在 master 中的文件),我得到以下错误:
fatal: ambiguous argument 'my_file_name': unknown revision or path not in the working tree.
error: unrecognized input
在 SO 上搜索后,我在 git add 的 intent-to-add 标志上找到了以下信息:What does git add --intent-to-add or -N do and when should it be used?
所以我尝试了以下操作:对于每个文件,我将在 origin/master 和 master 之间进行差异,如果该文件当前不存在于 master(本地)中,那么我调用 git 在使用该文件名调用 git diff 并应用 diff 之前使用 --intent-to-add 标志添加。
if ! ls "$changed_file" 1> /dev/null 2>&1; then
echo adding new file "$changed_file"
echo foo > "$changed_file"
git add --intent-to-add "$changed_file"
fi
git diff master..origin/master "$changed_file" | git apply
有了这个,我在尝试执行 git 应用(最后一行)时收到以下错误,说文件已经存在于工作目录中:
error: tests/new_file: already exists in working directory
当我尝试删除虚拟文件创建部分时,如下所示,它也没有用。
if ! ls "$changed_file" 1> /dev/null 2>&1; then
echo adding new file "$changed_file"
git add --intent-to-add "$changed_file"
fi
报错信息同上:
fatal: ambiguous argument 'my_file_name': unknown revision or path not in the working tree.
error: unrecognized input
有谁知道这是怎么回事,我该如何解决这个问题? (获取并应用新远程文件的差异)
问题是,一般来说,Git 不知道 file_name
是 文件 名称,还是 分支 名字什么的。解决方法是告诉它:在双破折号--
之后,nothing可以是选项或分支名称:
git diff --name-only master origin/master -- file_name
同样的规则适用于 git checkout
这样的命令。如果您有一个名为 master
的文件并且您想要查看它怎么办?命令:
git checkout master
不会起作用,但是命令:
git checkout -- master
将,因为两个破折号告诉git checkout
这是选项和分支名称的结尾等等:其他所有内容必须 是一个文件名。
git reset --hard origin/master # reset index, worktree and ref
git reset --soft @{1} # put the ref back
就是你所需要的。
我有一个脚本,对于
中的每个 changed_file"git diff --name-only master..origin/master"
该文件的差异应用如下:
git diff master..origin/master file_name | git apply.
对于任何新创建的文件(origin master 中的文件但不是最初在 master 中的文件),我得到以下错误:
fatal: ambiguous argument 'my_file_name': unknown revision or path not in the working tree.
error: unrecognized input
在 SO 上搜索后,我在 git add 的 intent-to-add 标志上找到了以下信息:What does git add --intent-to-add or -N do and when should it be used?
所以我尝试了以下操作:对于每个文件,我将在 origin/master 和 master 之间进行差异,如果该文件当前不存在于 master(本地)中,那么我调用 git 在使用该文件名调用 git diff 并应用 diff 之前使用 --intent-to-add 标志添加。
if ! ls "$changed_file" 1> /dev/null 2>&1; then
echo adding new file "$changed_file"
echo foo > "$changed_file"
git add --intent-to-add "$changed_file"
fi
git diff master..origin/master "$changed_file" | git apply
有了这个,我在尝试执行 git 应用(最后一行)时收到以下错误,说文件已经存在于工作目录中:
error: tests/new_file: already exists in working directory
当我尝试删除虚拟文件创建部分时,如下所示,它也没有用。
if ! ls "$changed_file" 1> /dev/null 2>&1; then
echo adding new file "$changed_file"
git add --intent-to-add "$changed_file"
fi
报错信息同上:
fatal: ambiguous argument 'my_file_name': unknown revision or path not in the working tree.
error: unrecognized input
有谁知道这是怎么回事,我该如何解决这个问题? (获取并应用新远程文件的差异)
问题是,一般来说,Git 不知道 file_name
是 文件 名称,还是 分支 名字什么的。解决方法是告诉它:在双破折号--
之后,nothing可以是选项或分支名称:
git diff --name-only master origin/master -- file_name
同样的规则适用于 git checkout
这样的命令。如果您有一个名为 master
的文件并且您想要查看它怎么办?命令:
git checkout master
不会起作用,但是命令:
git checkout -- master
将,因为两个破折号告诉git checkout
这是选项和分支名称的结尾等等:其他所有内容必须 是一个文件名。
git reset --hard origin/master # reset index, worktree and ref
git reset --soft @{1} # put the ref back
就是你所需要的。