Git - 如何使用当前索引(当前状态)的某些非暂时更改来编辑旧(不是以前的)提交? [英] Git - How to edit old (not previous) commit with some of the unstaged changes from current index (current state)?

查看:70
本文介绍了Git - 如何使用当前索引(当前状态)的某些非暂时更改来编辑旧(不是以前的)提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经查看了以前的这些问题:



尽管它们并不完全解决特定的问题 - 索引中还有其他更改!当运行 rebase 命令时,git抱怨:无法重新分配:您有未分配的更改。



场景:

最后一个提交之前的提交(我称之为2 HEADs之前?)是一个重构提交。我目前在索引中有许多未分离的更改,但只有其中一些我想添加到之前的最后一次提交中。



我在想象如何做到这一点将是:


  1. 隐藏所有当前更改

  2. $ li $ code> rebase -i 到上一次提交(改变索引和移动头部,对吗?)
  3. 加载(如何?)

  4. 使用 add -p commit - 修改以选择性地修改这个旧提交

  5. rebase --continue 来完成返回到我开始的位置,索引会发生什么变化?)

  6. 然后弹出/清除存储(索引返回到我开始的位置)。


  7. 这是正确的吗?如果是,我该如何做第3步?如果不是,我应该怎么做呢?



    另外,请注意,我仍然在学习git,但我仍然不能100%确定引用正确使用git(Head,Index,Stash等)的东西。






    解决方案:



    对于其他人可能会有帮助,这些是我实际采取的步骤:



    1. git rebase -i< ID> 添加到 > parent 之前的最后一次提交,改变索引并移动head

    2. git stash apply 将存储加载到我的如果您有冲突, git reset HEAD< file> 来卸载文件,则索引不会改变分期。请确保分段已清除。
    3. 使用添加-p 和<$ c
      $ c $> commit --amend
      来选择性地对变化进行阶段化并提交它们
    4. git reset --hard 放弃索引,以便它匹配头

    5. git rebase --continue 来完成。更新孩子,将头移回到一开始,但现在将变化


      • 历史分为两个版本。另一个分支结束于以前存储在WIP中


    6. ,然后弹出存储以将索引返回到我开始的位置。另一个分支也被清除。


    解决方案

    您的计划听起来不错。 git stash apply git stash pop 会修改工作树和/或索引,但不会更改HEAD,所以你应该可以在rebase编辑时做到这一点。


    I've taken a look at these previous questions already:

    They don't exactly address a particular issue though - there are other changes in the index! When running the rebase command, git complains: Cannot rebase: You have unstaged changes.

    Scenario:

    The commit previous to the last one (do I refer to that as "2 HEADs ago"?) was a refactor commit. I currently have in the index many unstaged changes, but only some of which I want to add to the previous to last commit.

    I'm imagining the way to do this would be to:

    1. stash all of my current changes
    2. rebase -i to the previous to last commit (changing index and moving Head, right?)
    3. load the stash into my index without changing Head (how?)
    4. use add -p and commit --amend to selectively modify this old commit
    5. rebase --continue to finish (updates children, moves Head back to where I started, what happens to index?)
    6. then pop/clear the stash (index back to where I started).

    Is this correct? If it is, how do I do step 3? If it isn't, what should I be doing instead?

    Also, note that I'm still learning about git and am still not 100% sure I'm referencing things in git (Head, index, stash, etc) properly.


    Solution:

    For anyone else this may help, these are the steps I actually took:

    1. git stash all of my current changes
    2. git rebase -i <ID> to the parent of the previous to last commit, changing index and moving Head
    3. git stash apply load the stash into my index without changing Head
      • if you have conflicts, git reset HEAD <file> to unload files staging. Make sure staging is clear.
    4. use add -p and commit --amend to selectively stage changes and commit them
    5. git reset --hard to discard index so it matches Head
    6. git rebase --continue to finish. updates children, moves Head back to very start, but with changes
      • history is now forked into two versions. The other branch ends at the WIP previously stashed
    7. then pop the stash to bring index back to where I started. The other branch is also cleared.

    解决方案

    Your plan sounds good. A git stash apply or git stash pop will modify the working tree and/or index, but will not change HEAD, so you should be able to do it while in a rebase edit.

    这篇关于Git - 如何使用当前索引(当前状态)的某些非暂时更改来编辑旧(不是以前的)提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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