git一次应用多个存储



我有两次存储,我需要在一次提交中提交这两个存储。

我使用git stash apply来应用最新的存储,但当我再次使用它时,它会抛出以下错误,

error: Your local changes to the following files would be overwritten by merge:
library/HQ/groupsql.sql
Please commit your changes or stash them before you merge.
Aborting
The stash entry is kept in case you need it again.

我如何弹出两个存储然后提交它们。

我认为您可以执行以下操作:

// to see the list of "stashes" (like every time when stash run 'git stash' a new entry is created in the list
> git stash list
// Lest assume you need stash@{1} and stash@{3} for the sake of example
> git stash apply stash@{1} 
git commit -am "Whatever message"
> git stash apply stash@{3}
git commit --amend  // to add changes from stash3 from the previous commit

最重要的是,每个存储应用一旦完成就会在本地分支上产生提交,但只要更改是本地(而不是推送(,您就可以更改历史

我建议你也阅读这个线程,它包含一些巧妙的技巧,从git 2.11开始可用:你可以使用git stash apply n而不是git stash apply stash@{n}

另一个建议是:您可以使用git stash show stash@{3}查看存储库3中更改了哪些文件(如果您不确定要应用哪个存储库(

不过,我从未见过在一个命令中执行此操作的功能。所以现在我的答案是"不,你不能一次申请多个藏匿处"。我很高兴能被更有经验的git用户证明这一点。

我不认为你可以一次完成,当然,除非你已经准备好编写自己的脚本。原因是你可能在当前的HEAD和两个存储中有重叠的更改。即使你为自己写了一个这样做的脚本,你也会很难解决冲突,因为你的脚本不知道哪些更改应该保留,哪些更改应该放弃。因此,正确的方法是通过书本来做到这一点,即:

git stash apply stash@{1}
# resolve conflicts if any
git add .
git commit -m "<commit_message>"
git stash apply stash@{2}

如果你想一次进行更改,那么你可以这样做:

git stash apply stash@{1}
git commit -am "Dummy commit"
git stash apply stash@{2}
git reset HEAD~

在以上序列之后,您将同时更改两个存储库,并且您可以从这两个存储中进行一次提交。

最新更新