【问题标题】:git apply multi stash at one timegit 一次应用多个存储
【发布时间】:2020-03-14 12:56:13
【问题描述】:

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

我使用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.

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

【问题讨论】:

  • @abby37 它使每个存储都成为提交。
  • 在您运行第一次 stash apply 后,您将文件添加到给定的提交,您再次运行 stash apply,然后将文件添加到给定的提交。因此,所有这些存储更改都将通过使用 git commit --amend 在给定的提交中提交
  • 有没有可能一次性搞定。
  • 我给你的最好建议是:不要。不要尝试像这样组合多个存储。只需应用一个并提交,然后应用下一个并提交,然后使用git rebase -i 压缩这两个提交。

标签: git git-commit git-stash


【解决方案1】:

我认为你可以做到以下几点:

// 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

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

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

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

不过,我从未见过在一个命令中执行此操作的功能。所以现在我的回答是“不,你不能一次应用多个存储”。我很高兴能被更有经验的 git 用户证明。

【讨论】:

  • OP 想要在单个命令中做到这一点,为此他必须编写自己的自定义命令。对吧??
  • 我相信是的,OP 必须为此创建一个自定义脚本,但同样,如果存储之间存在冲突(可能发生),它们必须以某种方式解决(可能手动)
【解决方案2】:

我不认为你可以一次性做到这一点,当然,除非你准备好为此编写自己的脚本。原因是您当前的 HEAD 和两个存储区可能有重叠的更改。即使您编写了一个脚本来为您执行此操作,您也很难解决冲突,因为您的脚本不知道要保留哪些更改以及要丢弃哪些更改。因此,正确的方法是按本书进行,即:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    • 2016-08-08
    • 2016-07-14
    • 1970-01-01
    • 2013-05-08
    相关资源
    最近更新 更多