我与包含子模块的分支进行了git合并,并忘记为某些子模块添加文件夹(或者在合并期间在解决冲突时意外删除它们)。
更新子模块时出现错误
pathspec '...' did not match any file(s) known to git.
我想我可以手动为子模块添加空文件夹并进行修改,但这似乎是不可能的。
是否有一种方法可以添加所需的文件夹,而不必还原所有内容并再次合并?
下面是我所做的工作(我使用了tortoisegit,但是这个脚本复制了完全相同的行为;问题并不是tortoisegit特定的,tortoisegit只是使不提交所有更改的文件变得更容易,即我的例子中的子模块目录,这里是用git rm -f test2
__模仿的)
rm -rf test2
mkdir test2
cd test2
git init .
echo 2 > test2.cpp
git add .
git commit -a -m 1
git branch -m main
cd ..
rm -rf test
mkdir test
cd test
git init .
echo 1 > test.cpp
git add .
git commit -a -m a1
git branch -m a
git branch b
git submodule add -- ../test2 test2
git commit -m a2
git checkout b
rm -r test2
echo 12 > test.cpp
git commit -a -m b2
git checkout a
echo 13 > test.cpp
git commit -a -m a3
git checkout b
git merge a
echo 123 > test.cpp
git add test.cpp
git rm -f test2
git commit -m b3
git submodule update --init --recursive -- "test2"
发布于 2022-05-09 02:22:08
好的,通过复制器,我可以重现这个问题。最后的合并提交(在test
目录存储库中的分支test2
上)现在根本没有子模块test2
,因为解析步骤删除了它:
$ git status
On branch b
nothing to commit, working tree clean
$ git submodule status
$ cat .gitmodules
$
所以现在我们想把子模块放回去。我们可以像往常一样使用git submodule add
来完成这个任务:
$ git submodule add -- ../test2 test2
A git directory for 'test2' is found locally with remote(s):
origin [...]/test2
If you want to reuse this local git directory instead of cloning again from
[...]/test2
use the '--force' option. If the local git directory is not the correct repo
or you are unsure what this means choose another name with the '--name' option.
这是一个现代的,但不是最新的Git;旧版本的Git在这里可能什么都不说,可能会默默地接受这个尝试,但是既然我有这个版本,我现在遵循git submodule add
打印的建议并使用--force
。
$ git submodule add --force -- ../test2 test2
Reactivating local git directory for submodule 'test2'.
$ git status
On branch b
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: .gitmodules
new file: test2
$ git submodule status
b603f2eb4c83459de3d0796a89f1ab8bc19e1449 test2 (heads/main)
要“更新”合并(而不是添加到合并中的新提交),我们现在可以像往常一样使用git commit --amend
。请注意,这确实只是一个新的提交。--amend
的特别之处在于使新提交的父级与当前提交的父级相同,在git log --all --decorate --oneline --graph
中我们可以看到这一点:
$ git log --all --decorate --oneline --graph
* 1bc7878 (HEAD -> b) b3
|\
| * 3a6d09d (a) a3
| * b11ea5c a2
* | 2d6b761 b2
|/
* 38babd9 a1
$ git commit --amend -C HEAD
[b c5650ba] b3
Date: Mon May 9 03:11:28 2022 -0700
$ git log --all --decorate --oneline --graph
* c5650ba (HEAD -> b) b3
|\
| * 3a6d09d (a) a3
| * b11ea5c a2
* | 2d6b761 b2
|/
* 38babd9 a1
现在,对于分支b
的提交,我们有了一个新的、不同的哈希ID (在我的测试中现在是c5650ba
,但是当然每个人在这里都会得到不同的哈希ID )。但是我告诉Git,在没有编辑的情况下,用-C HEAD
重用来自上一个分支提示提交(-C HEAD
)的提交消息,所以在这一点上没有手动干预就发生了。现在合并提交又有了子模块。
(如果将旧的合并提交添加到git log
命令中,例如:
$ git log --all --decorate --oneline --graph 1bc7878
* c5650ba (HEAD -> b) b3
|\
| | * 1bc7878 b3
| |/|
|/|/
| * 3a6d09d (a) a3
| * b11ea5c a2
* | 2d6b761 b2
|/
* 38babd9 a1
只是没有旧合并提交的名称。由于我们从未将它发送到任何地方--我们没有运行git push
,也不允许任何人与git fetch
-nobody一起潜入我们的机器,否则我们就会知道我们曾经成功过)。
https://stackoverflow.com/questions/72168567
复制相似问题