简体   繁体   English

在另一个分支中从diff创建git commit

[英]Create git commit from diff in another branch without switching

I am wondering if it is possible to commit changes represented as a diff in a separate local branch without switching to this branch. 我想知道是否有可能在不切换到该分支的情况下在单独的本地分支中提交表示为diff的更改。

To put things in a perspective let's assume that I am working on the master branch and have a diff represented as a file raw.diff . 为了使事情更raw.diff ,让我们假设我在master分支上工作,并且把diff表示为raw.diff文件。 Now I would like to apply this diff to the new local branch (or a tag) needs_change and commit changes without switching from the master . 现在,我想将此diff应用于新的本地分支(或标记) needs_change并提交更改,而无需从master切换。

I know that in a regular flow I would do something like this: 我知道我会定期执行以下操作:

git checkout -b needs_change             # create and switch to local branch
git apply raw.diff                       # apply diff in needs_change
git commit -am "New commit with changes" # commit changes
git checkout master                      # switch back to master branch

However in this sequence of steps I have to switch to the needs_change branch and I am wondering if I can avoid this somehow. 但是,在这一系列步骤中,我必须切换到needs_change分支,我想知道是否可以避免这种情况。

Thanks in advance! 提前致谢!

Technically, Git makes new commits from whatever is in the index , so you could swap out the current index (which normally matches the current commit) for some other index contents matching some other commit, and then adjust the index contents and use that to make another commit. 从技术上讲,Git从索引中的任何内容进行新提交,因此您可以将当前索引(通常与当前提交匹配)换成与其他提交匹配的其他索引内容,然后调整索引内容并使用它来进行另一个提交。 The problem with doing this is that (a) it's really hard and (b) swapping out the index contents means overwriting the current work-tree anyway. 这样做的问题是(a)确实很困难,并且(b)换出索引内容意味着无论如何都要覆盖当前的工作树。

Hence, the way to do this without making a separate clone is to add another work-tree and index , which you can do with git worktree add , provided your Git is at least 2.5. 因此,不进行单独克隆的方法是添加另一个工作树和索引 ,只要您的Git至少为2.5,就可以使用git worktree add 1 This actually adds a set of three items: 1这实际上添加了三个项目:

  • a new work-tree, as implied by the command git worktree add ; 一个新的工作树,如git worktree add命令git worktree add
  • a new index to provide the index / staging-area for that new work-tree; 一个新索引,为该新工作树提供索引/暂存区; and
  • a new HEAD to keep track of which branch is in that new work-tree. 一个新的HEAD跟踪哪个分支是新的工作树。

You can now cd into the new work-tree, do any work you like there, git add files to copy them back into the index for that work-tree, and git commit there to use that work-tree's index to build a new commit that updates the branch that is checked out in that work-tree. 现在,您可以cd到新的工作树中,在其中执行您喜欢的任何工作, git add文件以将它们复制回该工作树的索引中,并且git commit在那里使用该工作树的索引来构建新的提交更新在该工作树中检出的分支。

(If you don't have git worktree , just make another clone.) (如果您没有git worktree ,则只需进行另一个克隆即可。)


1 Your Git should preferably be at least 2.15 since there were some important bug fixes leading up to 2.15. 1您的Git最好至少为2.15,因为有一些重要的错误修复程序导致2.15。 The most important one is that in 2.5 through 2.14, if you work in an added work-tree, and then get interrupted and leave it for 14 or more days, it's possible for Git to wreck what you were doing in that work-tree. 最重要的是,在2.5到2.14中,如果您在添加的工作树中工作,然后被打扰并离开14天或更长时间,则Git可能破坏您在该工作树中所做的工作。 If you just do something quickly, then remove the added work-tree, these bugs won't bite. 如果您只是快速执行某项操作,然后删除添加的工作树,这些错误将不会被咬住。

Not really. 并不是的。 You need to switch to the other branch in order to apply the patch/commit. 您需要切换到另一个分支才能应用补丁/提交。 There's no way around that (on a single local repo, just in case). 没有办法解决(以防万一,在一个本地仓库中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM