我可以影响“递归"算法所使用的合并策略吗?当应用git stash时? [英] Can I influence the merge strategy used by "recursive" when applying a git stash?

查看:75
本文介绍了我可以影响“递归"算法所使用的合并策略吗?当应用git stash时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在git储藏槽中有一组调试实用程序;我在分支之间移动,这些分支在存储方面的变化仅稍有不同.我在各个分支上 git stash apply 顶部的stash以测试功能.

但是我在其中一个分支中遇到了适当的合并冲突,但是我想更喜欢隐藏中的内容,因此我希望合并策略递归"更喜欢隐藏中的版本(我为他们的",请参见 man git-merge 部分 MERGE-STRATEGIES ,部分 recursive ).

我可以以某种方式告诉 git stash适用使用哪种合并策略?

解决方案

是的,您可以自己进行合并.隐藏提交的名称为 stash .

  git cherry-pick -n -m1 -Xtheirs隐藏 

Cherrypick以Cherrypick的父项为基础进行合并.存储区提交记录有两个父级的工作树状态,即已签出的提交和隐藏的索引.

-m1 告诉cherrypick使用第一个父级作为合并基础,因为在这种情况下,关于要合并的更改到底有什么歧义. -n 表示不提交结果.

这将产生一个工作树和索引,其更改与隐藏的工作树中的匹配.相反,如果您想应用隐藏索引中的更改,请改为使用Cherrypick stash ^ 2 ;否则,请使用

stash ^ 2 .或者,如果您想将隐藏索引(即添加的又名暂存的又称为索引内容)的更改应用到隐藏工作树,请使用 -m2 而不是 -m1 ./p>

如果要仅将 合并到工作树,请执行

  savetree =`git write-tree`git cherry-pick -n -m1 -Xtheirs隐藏git read-tree $ savetree 

这足以解决这里的问题(应用一些方便的更改),但是并不能完全重现stash为您所做的一切.仅将隐藏的索引更改应用到当前索引,而仅将隐藏的工作树更改应用到当前工作树,这很简单.如果有人想看, git stash /usr/libexec/git-core/git-stash 中的脚本.

I have a set of debug utilities living in a git stash slot; I'm moving between branches which differ only very slightly regarding the changes in the stash. I git stash apply the top stash on the respective branch to test a feature.

However I ran into a proper merge conflict in one of the branches, but I want to prefer what's in the stash, so I want the merge-strategy "recursive" to prefer the version coming from the stash (I guess that'd be "theirs", cf. man git-merge section MERGE-STRATEGIES, subsection recursive).

Can I somehow tell git stash apply what merge strategy to use?

解决方案

Yes, you can just do the merge yourself. The stash commit's named stash.

git cherry-pick -n -m1 -Xtheirs stash

Cherrypick does a merge with the cherrypick's parent as the base. Stash commits record the worktree state with two parents, the checked-out commit and the stashed index.

The -m1 tells cherrypick to use the first parent as the merge base, when as here there's any ambiguity about exactly which changes you want merged. The -n says not to commit the result.

This will produce a worktree and index with changes that match the ones in the stashed worktree. If instead you want to apply the changes in the stashed index, cherrypick stash^2 instead; alternately, if you want to apply the changes from the stashed index (i.e. the added aka staged aka indexed content) to the stashed worktree use -m2 rather than -m1.

If you want to merge only to the worktree, do

savetree=`git write-tree`
git cherry-pick -n -m1 -Xtheirs stash
git read-tree $savetree

That's enough to handle the case in the question here (applying a handy set of changes), but it doesn't completely reproduce everything stash does for you. Applying the stashed index changes only to the current index and the stashed worktree changes only to the current worktree is just plain finicky. git stash is a script in /usr/libexec/git-core/git-stash if anybody wants to see it.

这篇关于我可以影响“递归"算法所使用的合并策略吗?当应用git stash时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆