Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Git 如何在上一次提交中更改文件名_Git - Fatal编程技术网

Git 如何在上一次提交中更改文件名

Git 如何在上一次提交中更改文件名,git,Git,我已经做了五次提交,但还没有推送,我发现我的第一次提交有一个错误,所以我想在我尝试的第一次提交中更改文件名 git reset --soft HEAD^ git reset HEAD path/to/file git mv oldName NewName git commit --amend 但是这会创建一个新的提交,如何在以前的提交中更改文件名?您可以使用git-rebase-i HEAD^然后r重命名提交。假设提交是a-B-C-D-E并且a是第一个提交 git reset A --har

我已经做了五次提交,但还没有推送,我发现我的第一次提交有一个错误,所以我想在我尝试的第一次提交中更改文件名

git reset  --soft HEAD^
git reset HEAD path/to/file
git mv oldName NewName
git commit --amend

但是这会创建一个新的提交,如何在以前的提交中更改文件名?

您可以使用
git-rebase-i HEAD^
然后
r
重命名提交。

假设提交是
a-B-C-D-E
并且
a
是第一个提交

git reset A --hard
git mv oldName newName
git commit --amend --no-edit
git cherry-pick A..E
5次提交的sha1值将是新的。事实上,创建了5个新提交,分支从旧的
E
移动到新的
E
。如果您忘记了旧的
E
是什么,您可以运行
git reflog
来找到它。或者,您可以事先在旧的
E
上创建一个新分支,并在作业完成后将其删除。当您知道旧的
E
是什么时,
git log E
告诉您
ABCD
是什么

如果在重命名文件之前,
BCDE
中的任何一个触摸该文件,则在选择提交时会遇到冲突。假设
B
D
触摸重命名的文件,但
C
E
不触摸。然后运行以下命令代替git cherry pick A..E

git format-patch -1 B
git format-patch -1 D
#Two *.patch files are created. 
#Open and edit the *.patch, change the oldName to the newName in them.
git am <edited-B.patch>
git cherry-pick C
git am <edited-D.patch>
git cherry-pick E
rm *.patch
git格式修补程序-1b
git格式修补程序-1d
#创建了两个*.patch文件。
#打开并编辑*.patch,将其中的旧名称更改为新名称。
吉特阿姆
吉特樱桃皮克C
吉特阿姆
吉特樱桃皮克
rm*.patch

我尝试过并有效的解决方案:

git rebase -i HEAD~5
git mv oldeNAme.ext NEWNAME.ext
git commit --amend
git rebase --continue