git rebase -i 用于特定提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32614147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 11:23:40  来源:igfitidea点击:

git rebase -i for specific commits?

gitgit-rebase

提问by jkj2000

I have a branch with a few commits I'd like to interactively rebase. However, after pulling and merging there are now other commits interleaved with mine so I can't just do something like git rebase -i HEAD~3

我有一个包含一些提交的分支,我想以交互方式对其进行变基。然而,在拉取和合并之后,现在有其他提交与我的交错,所以我不能只做类似的事情git rebase -i HEAD~3

Is it possible to do an interactive rebase while choosing individual commits? Something like git rebase -i 23duirs 3eujsfe ...

是否可以在选择单个提交时进行交互式变基?就像是git rebase -i 23duirs 3eujsfe ...

Another possibility-- If I simply run git rebase -iI believe it's possible to cut and paste my commit lines to be consecutive, and squash from there, is that correct? Is there a best practice for doing so, to minimize the chance of conflicts with the unrelated commits in there?

另一种可能性——如果我只是运行,git rebase -i我相信可以将我的提交行剪切和粘贴为连续的,然后从那里压缩,这是正确的吗?是否有这样做的最佳实践,以尽量减少与不相关提交发生冲突的可能性?

采纳答案by Gabriele Petronella

Given this

鉴于这种

pick <unrelated a>
pick A
pick <unrelated b>
pick B
pick <unrelated c>

you can definitely do

你绝对可以

pick <unrelated a>
pick <unrelated b>
pick <unrelated c>
pick A
squash B

Provided that your commit are reallyunrelated to the other changes, this will work just fine.

如果您的提交确实与其他更改无关,这将正常工作。

回答by jakub.g

Rebasing after a merge is a pain in git.

合并后的变基在 git 中是一种痛苦。

I generally try to avoid this issue, by not merging others' work, but instead I fetch and rebase

我通常会尽量避免这个问题,不合并其他人的工作,而是我获取和变基

git fetch upstream
git rebase upstream/master

(you can also use git pull --rebase upstream master, but this has a drawback of not updating your remotes/upstream/mastertracking branch, which git fetchdoes; hence I prefer fetch && rebaseinstead of pull --rebase).

(您也可以使用git pull --rebase upstream master,但这有一个缺点,即不更新您的remotes/upstream/master跟踪分支,git fetch确实如此;因此我更喜欢fetch && rebase而不是pull --rebase)。

That way, my commits are always on top of everything in master, until my development is ready, then I open a pull request with a clean history without merges I.e. at any point in time, last n commits in the branch that I am working on, are my commits, and I can easily reword, squash them etc.

这样,我的提交总是在 master 中的所有内容之上,直到我的开发准备就绪,然后我打开一个带有干净历史的拉取请求,没有合并,即在任何时间点,我正在处理的分支中的最后 n 次提交, 是我的提交,我可以轻松地改写、压缩它们等。

The easiest way to amend some old commits is git rebase -i, and the param you pass to git rebase -iis a single commit-ish; all the commits newerthan the provided commit will be displayed in the interactive rebase screen. I don't know of any way for this screen to just display somecommits newer than the param.

修改一些旧提交的最简单方法是git rebase -i,您传递给的参数git rebase -i是单个 commit-ish;所有比提供的提交更新的提交都将显示在交互式 rebase 屏幕中。我不知道这个屏幕有什么方法可以只显示一些比参数更新的提交。

The alternative to change old commits is git filter-branch, but it's rather much more complicated to use.

更改旧提交的替代方法是git filter-branch,但使用起来要复杂得多。

回答by Balaji Radhakrishnan

Little hacky solution.

小黑客解决方案。

Create a new local branch(LOCAL_B) from the origin. cherry-pick your commits from LOCAL_A to LOCAL_B and squash them using git rebase -i HEAD~3.

从原点创建一个新的本地分支(LOCAL_B)。挑选你的提交从 LOCAL_A 到 LOCAL_B 并使用git rebase -i HEAD~3.

Now, take a backup of LOCAL_A for safety.

现在,为了安全起见备份 LOCAL_A。

Delete LOCAL_A. Create new local branch LOCAL_A from the remote. Cherry-pick the squashed commit from LOCAL_B to LOCAL_A.

删除 LOCAL_A。从远程创建新的本地分支 LOCAL_A。挑选从 LOCAL_B 到 LOCAL_A 的压缩提交。

you are done.

你完成了。