是否有某种“git rebase --dry-run”可以提前通知我冲突?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29517440/
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
Is there some kind of 'git rebase --dry-run', which would notify me of conflicts in advance?
提问by Jonathan.Brink
I'm trying to script rebasing and my script will take different paths depending on if the rebase results in any conflicts.
我正在尝试编写 rebase 脚本,我的脚本将采用不同的路径,具体取决于 rebase 是否会导致任何冲突。
Is there a way to determine if a rebase would result in conflicts before executing the rebase?
有没有办法在执行 rebase 之前确定 rebase 是否会导致冲突?
采纳答案by jub0bs
At the time of writing (Git v2.6.1v2.10.0), the git rebase
command offers no --dry-run
option. There is no way of knowing, before actually attempting a rebase, whether or not you're going to run into conflicts.
在撰写本文时 (Git v2.6.1v2.10.0),该git rebase
命令不提供任何--dry-run
选项。在实际尝试变基之前,无法知道您是否会遇到冲突。
However, if you run git rebase
and hit a conflict, the process will stop and exit with a nonzero status. What you could do is check the exit status of the rebase operation, and, if it is nonzero, run git rebase --abort
to cancel the rebase:
但是,如果您运行git rebase
并遇到冲突,该进程将停止并以非零状态退出。您可以做的是检查 rebase 操作的退出状态,如果它不为零,则运行git rebase --abort
以取消 rebase:
git rebase ... || git rebase --abort
回答by joneit
If you just want to see if the rebase would besuccessful but then you want to "roll back," you can alway reposition the branch tipback to the original commit.Just tag or make a note of the original SHA.
如果您只想查看 rebase是否成功,但又想“回滚” ,则始终可以将分支提示重新定位回原始提交。只需标记或记下原始 SHA。
Or perhaps easier, create a new temporary branch in which to "stage" the rebase:
或者更简单,创建一个新的临时分支来“暂存”rebase:
git checkout your-branch
git checkout -b tmp
git rebase other-branch
If it was successful but you want to "roll back," your-branch
is untouched. Just git branch -D tmp
and you're back to where you started from.
如果它成功了但你想“回滚”,your-branch
则原封不动。只是git branch -D tmp
你又回到了你开始的地方。
If there were conflicts and you did some work to resolve them and you now you want to keep the rebase, just reposition your-branch tip to tmp
(and then git branch -D tmp
).
如果存在冲突并且您做了一些工作来解决它们,并且您现在想要保留变基,只需将您的分支提示重新定位到tmp
(然后是git branch -D tmp
)。
回答by Kryten
I suspect that git rebase ... --dry-run
is not possible, for the following reason.
我怀疑这git rebase ... --dry-run
是不可能的,原因如下。
When you're doing a git rebase
, git will rollback to the starting point, then incrementally apply patches for each commit to bring the branch up to date. If it hits a conflict, it will stop & wait for you to resolve the conflict before continuing. The path that the rebase takes after that conflict depends upon how you resolve the conflict - if you resolve it a certain way, that might introduce (or eliminate) later conflicts.
当您执行git rebase
git 时,git 将回滚到起点,然后为每个提交增量应用补丁以使分支保持最新。如果遇到冲突,它将停止并等待您解决冲突,然后再继续。冲突后变基采用的路径取决于您如何解决冲突 - 如果您以某种方式解决它,则可能会引入(或消除)以后的冲突。
Thus, git rebase ... --dry-run
would only be able to give you the firstconflict - reports of later conflicts will depend upon how that first conflict is resolved.
因此,git rebase ... --dry-run
只能给你第一个冲突 - 以后冲突的报告将取决于第一个冲突是如何解决的。
The only way I can think of doing this would be via git diff
between the current position and the last commit in the branch you're rebasing to. But that won't really give you what you're looking for - you really just need a list of conflictingchanges between the two points. There mightbe a way to do it with git diff
, but it's not a normal patch.
我能想到的唯一方法是git diff
在当前位置和您要重新定位的分支中的最后一次提交之间进行。但这并不能真正为您提供您想要的东西——您真的只需要一份这两点之间相互冲突的更改列表。有可能是一个办法做到这一点的git diff
,但它不是一个正常的补丁。
回答by Lukasz Kruszyna
You still can do git rebase, play with it as you want, than recover all the changes from before.
Assuming you have done your rebase of some branch into the master
, and you don't like it:
您仍然可以执行 git rebase,随心所欲地使用它,而不是恢复以前的所有更改。假设您已将某个分支的 rebase 重新设置为master
,并且您不喜欢它:
git reflog -20
- gives you last 20 positions of your HEAD with a little descriptiongit checkout <the_branch_name>
- places your HEAD on the branchgit reset --hard <old_sha1_found_in_reflog>
- places your HEAD and branch on the old ref, this way you can recover old branch.
git reflog -20
- 为您提供 HEAD 的最后 20 个位置,并附有一些说明git checkout <the_branch_name>
- 把你的头放在树枝上git reset --hard <old_sha1_found_in_reflog>
- 将你的 HEAD 和分支放在旧的 ref 上,这样你就可以恢复旧的分支。
There are some mechanics to understand here:
这里有一些机制需要理解:
- You NEVER delete anything in git, not with commands, anyway. Its the garbage collector that comes through and deletes unreferenced branches (default 3 months). So your branch, from before the rebase, still exists.
- Same goes for the on same branch rebase, its just a new tree rewritten next to the old one.
- All the history of
rebase
and your other on HEAD manipulations is written in thereflog
- You can use
@{N}
annotations fromreflog
- 无论如何,您永远不要删除 git 中的任何内容,而不是使用命令。它的垃圾收集器通过并删除未引用的分支(默认为 3 个月)。所以你的分支,在 rebase 之前,仍然存在。
- 同一个分支 rebase 也是如此,它只是在旧树旁边重写了一个新树。
rebase
HEAD 操作的所有历史记录都写在reflog
- 您可以使用
@{N}
注释来自reflog
So, nothing is lost after the rebase
, you just have to know how to find and recover it.
因此,在 之后不会丢失任何东西rebase
,您只需要知道如何找到并恢复它。
For example you can place yourself a tag before the rebase
than revert to it or delete it. it evades you all the SHA1 research step.
例如,您可以在 之前放置一个标签,然后rebase
恢复或删除它。它避开了你所有的 SHA1 研究步骤。
回答by isapir
Building on @joneit's solution:
基于@joneit的解决方案:
Create a new temp
branch from your-branch
and try to rebase that temp branch onto the new-base
:
temp
从创建一个新分支your-branch
并尝试将该临时分支变基到new-base
:
git checkout -b temp <your-branch> && git rebase <new-base>
e.g. to test if branch feature1
can be rebase'd onto master
:
例如,测试分支是否feature1
可以变基到master
:
git checkout -b temp feature1 && git rebase master