如何在 git 变基中只保留头部变化
How to keep only head changes in a git rebase
我目前正在经历十几个提交的冗长变基过程。我在我的开发过程中构建了一些东西,这样只有来自 HEAD
的更改是我想要保留的 - 所有其他冲突(例如提交哈希 b06a1dd
)应该被删除。
有没有办法简单地删除与 >>>>>>> b06a1dd
相关的所有更改并保留 Git 将一次性标记为 <<<<<<< HEAD
的更改,所以我没有继续输入 git rebase --continue
,处理来自更多提交哈希的更多冲突,并仅保留 HEAD
更改?
如果您愿意重新开始 rebase (git rebase --abort
),那么这应该可以满足您的需要:
git rebase -X ours upstream
其中 upstream
是您要变基的分支。
如 this answer 和其他地方所述,ours
与 theirs
标签对于变基比合并更容易混淆。开始变基后,Git 创建一个匿名分支并开始向其应用提交。由于 ours
表示 "keep changes from the current branch",当前分支将是 HEAD
,其中包含 upstream
和 rebase
.
已经应用的任何更改
为了完整起见,这是我从当前问答中的先前答案和评论中学到的(感谢他们的作者):
- 如果您愿意重新开始并且为所有提交选择同一侧,当前选择的答案
git rebase --abort
然后git rebase -X ours upstream
可以做把戏。
但我想在实践中你不会想在不查看每个提交的情况下盲目地使用 ours
或 theirs
。当 "in the middle of a lengthy rebase" 时,您可能希望 为每个提交 做出个案决定。因此,您的实际选择将是:
要使用上游:
git checkout --ours path/to/a/specific/file
git add path/to/a/specific/file
甚至更好,在这种情况下,您只需使用:
git reset HEAD path/to/a/specific/file
使用你的功能分支:
git checkout --theirs path/to/a/specific/file
或手动处理编辑器中的每个 <<<< ... ==== ... >>>>
。
PS:The ours
and theirs
have special meaning when doing rebase.:
Note that during git rebase and git pull --rebase, ours and theirs may appear swapped; --ours gives the version from the branch the changes are rebased onto, while --theirs gives the version from the branch that holds your work that is being rebased.
This is because rebase is used in a workflow that treats the history at the remote as the shared canonical one, and treats the work done on the branch you are rebasing as the third-party work to be integrated, and you are temporarily assuming the role of the keeper of the canonical history during the rebase. As the keeper of the canonical history, you need to view the history from the remote as ours (i.e. "our shared canonical history"), while what you did on your side branch as theirs (i.e. "one contributor’s work on top of it").
有一个类似的问题,在我的例子中,我要重新定位的分支是一个新分支(我刚刚创建它,因此是空的)所以我只需要一个好的旧拉:
git rebase --abort && git pull origin my/remote_branch
我目前正在经历十几个提交的冗长变基过程。我在我的开发过程中构建了一些东西,这样只有来自 HEAD
的更改是我想要保留的 - 所有其他冲突(例如提交哈希 b06a1dd
)应该被删除。
有没有办法简单地删除与 >>>>>>> b06a1dd
相关的所有更改并保留 Git 将一次性标记为 <<<<<<< HEAD
的更改,所以我没有继续输入 git rebase --continue
,处理来自更多提交哈希的更多冲突,并仅保留 HEAD
更改?
如果您愿意重新开始 rebase (git rebase --abort
),那么这应该可以满足您的需要:
git rebase -X ours upstream
其中 upstream
是您要变基的分支。
如 this answer 和其他地方所述,ours
与 theirs
标签对于变基比合并更容易混淆。开始变基后,Git 创建一个匿名分支并开始向其应用提交。由于 ours
表示 "keep changes from the current branch",当前分支将是 HEAD
,其中包含 upstream
和 rebase
.
为了完整起见,这是我从当前问答中的先前答案和评论中学到的(感谢他们的作者):
- 如果您愿意重新开始并且为所有提交选择同一侧,当前选择的答案
git rebase --abort
然后git rebase -X ours upstream
可以做把戏。 但我想在实践中你不会想在不查看每个提交的情况下盲目地使用
ours
或theirs
。当 "in the middle of a lengthy rebase" 时,您可能希望 为每个提交 做出个案决定。因此,您的实际选择将是:要使用上游:
git checkout --ours path/to/a/specific/file git add path/to/a/specific/file
甚至更好,在这种情况下,您只需使用:
git reset HEAD path/to/a/specific/file
使用你的功能分支:
git checkout --theirs path/to/a/specific/file
或手动处理编辑器中的每个
<<<< ... ==== ... >>>>
。
PS:The ours
and theirs
have special meaning when doing rebase.:
Note that during git rebase and git pull --rebase, ours and theirs may appear swapped; --ours gives the version from the branch the changes are rebased onto, while --theirs gives the version from the branch that holds your work that is being rebased.
This is because rebase is used in a workflow that treats the history at the remote as the shared canonical one, and treats the work done on the branch you are rebasing as the third-party work to be integrated, and you are temporarily assuming the role of the keeper of the canonical history during the rebase. As the keeper of the canonical history, you need to view the history from the remote as ours (i.e. "our shared canonical history"), while what you did on your side branch as theirs (i.e. "one contributor’s work on top of it").
有一个类似的问题,在我的例子中,我要重新定位的分支是一个新分支(我刚刚创建它,因此是空的)所以我只需要一个好的旧拉:
git rebase --abort && git pull origin my/remote_branch