gpt4 book ai didi

git - 在提交之间移动文件

转载 作者:IT王子 更新时间:2023-10-29 00:56:45 24 4
gpt4 key购买 nike

我有两个相应的提交,在本地历史的某个地方,一个文件被错误地添加到第二个文件中。我想解决这个问题。

我不明白应该如何使用交互式 rebase 。我执行了 git rebase -i HEAD~10 并选择使用文件编辑提交以便从那里检查它。我使用 git guit 但是,在提交区域中看不到任何文件。我可以选择修改之前的提交然后我看到文件。但是,我无法将放错位置的文件添加到之前的提交中,因为我在当前提交中看不到该文件。

最佳答案

因此,当 rebase 时,选择同时编辑您错误添加文件的提交,以及您要按顺序添加文件的提交。如果该文件在较晚的提交中,但应该在较早的提交中,则您将不得不重新排序这些行。例如,我从

pick 8de731b Commit with missing file.
pick bbef925 Commit with too many files.
pick 52490ce More history.

我需要把它改成

edit bbef925 Commit with too many files.
edit 8de731b Commit with missing file.
pick 52490ce More history.

然后,

# In the commit containing an extra file
git reset HEAD^ badfile.c
git commit --amend
git rebase --continue

# Now in the commit to add it to
git add badfile.c
git commit --amend
git rebase --continue

不幸的是,当在一个分支中编辑历史时,我不知道有什么方法可以避免在所有分支中编辑历史。应尽早进行 rebase 以避免此类问题。在我这里的简单案例中,我可以 merge master 和另一个分支,但提交不 merge ,然后我必须在 master 中重新设置基准,重新排序和压缩提交,如下所示:

pick 7cd915f Commit with missing file.
fixup 8de731b Commit with missing file. #This was the higher of the two entries
pick 8b92c5a Commit with too many files.
fixup bbef925 Commit with too many files. #This was the higher of the two entries
pick 94c3f7f More history.
fixup 52490ce More history. #This was the higher of the two entries

后期编辑:我刚刚注意到我不小心将提交历史记录重新排序为我原始答案的遗留物。交换 rebase 中的行会更改您提交的顺序;编辑后,您可以再次 rebase 并将它们交换回来以返回到原始提交顺序。

关于git - 在提交之间移动文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18569931/

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