git 将单个文件提交到另一个分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42467062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Commit a single file to another branch
提问by Jorjon
I'm on branch feature-x
doing some work, when I suddenly encounter an issue which should really be solved on another branch, hotfix
.
我在分支上feature-x
做一些工作时,突然遇到一个应该在另一个分支上真正解决的问题,hotfix
.
I want to create a commit with that change, but on the hotfix
branch.
我想用该更改创建一个提交,但是在hotfix
分支上。
From this questionI understand that the standard procedure would be to
从这个问题我明白标准程序是
# On branch feature-x
git stash
git checkout hotfix
git stash pop
git add -A
git commit -m "Fixed issue #562"
That would work if I didn't have many changes on branch feature-x
already underway, which would throw a conflict with the branch hotfix
. I want to avoid having to resolve unnecessary conflicts.
如果我在 branch 上没有进行很多更改,这会起作用feature-x
,这会与 branch 发生冲突hotfix
。我想避免必须解决不必要的冲突。
To avoid that, I was thinking that I could only extract a single file from the stash, as noted on this answer. So the procedure would be:
为了避免这种情况,我认为我只能从存储中提取一个文件,如本答案所述。所以程序是:
# On branch feature-x
git stash
git checkout hotfix
git checkout stash@{0} -- <src/buggyfile.xxx>
git add -A
git commit -m "Fixed issue #562"
And then I should go back to feature-x
然后我应该回去 feature-x
git checkout feature-x
git stash pop
While there's a wayto bringfiles from another branch directly, I want to know if there's a way to sendfiles to another branch, without all this hassle. The actual modification is only a couple characters.
虽然有一种方法来实现直接从另一个分支的文件,我想知道是否有一种方式发送文件到另一个分支,没有这一切的麻烦。实际的修改只是几个字符。
采纳答案by Arpit Aggarwal
You can commit the change in feature-x
and cherry-pickit in hotfix
, as follows:
您可以在 中提交更改feature-x
并在 中挑选它hotfix
,如下所示:
# On branch feature-x
git add <file>
git commit -m "Fixed issue #562" // Note commit-id
git checkout hotfix
git cherry-pick <commit-id>
git push origin hotfix
Extending the answer as per @torek comment, to use git-worktree, as follows:
根据@torek 评论扩展答案,使用git-worktree,如下所示:
git worktree add -b hotfix ../hotfix origin/master
cd ../hotfix/
git add <file>
git commit -m "Fixed issue #562"
git push origin hotfix
回答by Marina Liu
To commit a file from feature-x
to hotfix
, there is an easy way to do that (assume the commit you just added on feature-x
branch is 4712df727f7303bc95545d0f6a024129be97c5c2
):
为了提交一个文件从feature-x
到hotfix
,有一个简单的方法来做到这一点(假设提交您刚才添加的feature-x
分支4712df727f7303bc95545d0f6a024129be97c5c2
):
# on branch hotfix
git checkout 4712d filename
git commit