简体   繁体   English

Git rebase 在每次提交之前停止,然后编辑和 rebase --continue

[英]Git rebase to stop just before each commit to then edit and rebase --continue

I notice when I rebase and have a merge conflict, it has the staged changes for the current commit, so it is easy to find what was done in the commit and change it, but then you can also just do 'git rebase --continue' and it will apply the change with the same commit message as before.我注意到当我变基并发生合并冲突时,它具有当前提交的分阶段更改,因此很容易找到提交中所做的并更改它,但是您也可以只执行 'git rebase --continue ' 并且它将使用与以前相同的提交消息应用更改。

Is there a way to force this to happen for specific commits, or all commits, so I can easily rebase and edit the previous changes I made to work with the changes I have rebased on?有没有办法强制针对特定提交或所有提交发生这种情况,以便我可以轻松地重新设置和编辑我之前所做的更改以处理我重新设置的更改? It seems like this would be much easier than just setting edit on all the commits and then doing 'git reset --soft HEAD~', so that the changes are visible and I can check they make sense for the new HEAD, editing, and then having to do 'git commit -m "message" '.似乎这比仅在所有提交上设置编辑然后执行“git reset --soft HEAD~”要容易得多,这样更改是可见的,我可以检查它们对新 HEAD、编辑和然后不得不做'git commit -m“message”'。

My use case is that the repository I am working with has had its file structure heavily refactored and git is unable to make the appropriate changes, but says the merge is successful.我的用例是我正在使用的存储库已对其文件结构进行了大量重构,git 无法进行适当的更改,但表示合并成功。

Thanks!谢谢!

I think what you are looking for is interactive rebasing and rewriting git history.我认为您正在寻找的是交互式变基和重写 git 历史。 It will let you squash commits as well as change the commit messages.它可以让你压缩提交以及更改提交消息。

git rebase -i branch # <branch> should be the parent branch i.e. develop/master

Rewriting git history 重写 git 历史

# for instance
>>> git rebase -i branch
pick 8705209 first
edit 7979797 second # Use edit wherever you want to view and edit commit
squash 9793757 third # Use squash to squash commits into the previous one.

Just do git commit --amend before git rebase --continue .只需在git rebase --continue git commit --amend执行 git commit --amend 。

Alternatively - use git-cherry-pick --no-commit commit1..commitN .或者 - 使用git-cherry-pick --no-commit commit1..commitN

force this to happen for specific commits强制这种情况发生在特定的提交上

Start an interactive rebase with git rebase -i HEAD~~~ .使用git rebase -i HEAD~~~启动交互式变基。

This opens an editor with a list of commits:这将打开一个带有提交列表的编辑器:

pick 1234567 fix stuff
pick 789abcd new features
pick 0102030 break everything

Change the pick to edit on a commit (or commits) that you want to edit.在要edit的提交(或提交)上更改要编辑的pick Save, and exit the editor.保存并退出编辑器。

Then, git will bring you back to these commits, one by one, and you can edit them.然后,git 会一一带你回到这些提交,你可以编辑它们。 I think what you're missing is that, at this point, you can view diffs that are staged, with git diff --cached .我认为您缺少的是,此时,您可以使用git diff --cached查看暂存的差异。

After you've edited each one, use git rebase --continue to continue.编辑完每一项后,使用git rebase --continue继续。

这不是一个真正的答案,但我最初目标的替代解决方案现在只是进行变基,然后使用lazygit编辑提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM