gpt4 book ai didi

git - 无法选择性提交更改不存在的文件

转载 作者:行者123 更新时间:2023-12-02 02:16:47 24 4
gpt4 key购买 nike

我想知道为什么 git 不允许挑选一个对不存在的文件引入更改的提交。我的理解是提交代表工作目录的完整快照。因此,挑选包含当前不存在的文件的提交应该简单地创建该文件 - 但我可以看到情况并非如此。

下面是几个命令来重现我正在讨论的情况。

$ git init
Initialized empty Git repository in /home/mzakrze/workspace/tmp/.git/
$ echo "Hello world!" > first_file; git add first_file; git commit -m "Init commit"
[master (root-commit) 7f9478a] Init commit
1 file changed, 1 insertion(+)
create mode 100644 first_file
$ echo "Fox jumps over the layz dog" > test; git add test; git commit -m "Commit with a typo"
[master 776387b] Commit with a typo
1 file changed, 1 insertion(+)
create mode 100644 test
$ echo "Fox jumps over the lazy dog" > test; git add test; git commit -m "Fix typo"
[master 9ea19df] Fix typo
1 file changed, 1 insertion(+), 1 deletion(-)
$ git log
commit 9ea19dfe2597b28eb576e2682e745de3da74733f
Author: mzakrze <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="651d1d1d1d1d1d250208040c094b060a08" rel="noreferrer noopener nofollow">[email protected]</a>>
Date: Sun Apr 11 17:01:13 2021 +0200

Fix typo

commit 776387b563edba9df2a12c5d1a2fd5bffb10c643
Author: mzakrze <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="562e2e2e2e2e2e16313b373f3a7835393b" rel="noreferrer noopener nofollow">[email protected]</a>>
Date: Sun Apr 11 17:00:53 2021 +0200

Commit with a typo

commit 7f9478a83e3938bf57552c1b90ddc7322b1bf315
Author: mzakrze <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c1b9b9b9b9b9b981a6aca0a8adefa2aeac" rel="noreferrer noopener nofollow">[email protected]</a>>
Date: Sun Apr 11 16:59:04 2021 +0200

Init commit
$ git reset --hard 7f9478a83e3938bf57552c1b90ddc7322b1bf315
HEAD is now at 7f9478a Init commit
$ git cherry-pick 9ea19 # cherry-pick "Fix typo"
error: could not apply 9ea19df... Fix typo
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

INB4:我不是在寻找“解决方案”,我只是想了解为什么 git 认为这是一个冲突。

最佳答案

如果你查看cherry-pick命令的文档(例如gitcherry-pick--help),描述是“应用由一些现有的提交”。换句话说,这会查看所选提交及其父级,计算两者之间的差异,并尝试将该更改应用到当前分支。

您尝试cherry-pick的提交引入的更改会影响不存在的文件,因此无法应用它。

如果您只是想要源提交中存在的文件,您可以:

git checkout 9ea19 test

关于git - 无法选择性提交更改不存在的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67046645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com