[英]With Git, how can I commit some changes in the working copy to a different branch?
我在一个分支机构工作,得到了一份真正不干净的工作副本。 在查看提交的更改时,我希望将一些oneliner修复程序提交到master
分支。
在这种情况下,使用git stash
实际上并没有帮助,因为我的工作副本还有许多其他更改,但尚未与master合并。
有没有更有效的方法来解决这种情况? (例如,提交,移动它的父级?)
您可以使用git add -i
来使用交互模式。 在那里,您可以指定,提交什么以及跳过什么。
这样您就可以将oneliner作为单独的提交提交。 通过使用git cherry-pick
您可以将它们合并到您的主服务器中。
使用git add -i
选择要为此分支提交的内容,然后更改为master并提交其余分支。
使用add -i
您可以选择要为提交准备哪些文件的哪些部分,然后提交它们,同时将相同文件的其他部分保留在提交之外。
git add -p
会直接进入补丁模式,以便按照@ arkaitz-jimenez正确推荐的流程进行操作。
我不知道这是否是您想要的,但我只是检查另一个分支(不会丢失未提交的更改),然后有选择地检查您要提交的更改。
基于之前的建议,这是我提出的解决方案:
解决方案1与樱桃挑选
只需在分支本身提交单个更改:
git add --patch <files> # record every change to move to master
git commit
转向掌握,挑选樱桃
git stash
git checkout master
git cherry-pick <commitid>
回到分支机构,它可以重新定位。
git checkout <branch>
git rebase master
对于每次重复提交,系统都会提示您输入:
git rebase --skip
重复提交将从分支中的补丁集中过滤掉,并且历史记录是干净的。 最终的git merge
仍然可以快速进行。
解决方案2,无需先在分支中提交
首先提取所有内容以移至master:
git add --patch <files> # record every change to move to master
然后切换到master提交:
git stash --keep-index # clear the working copy only
git checkout master -m # merge the index.
git commit
回到分支机构,它可以直接重新定位到主机的尖端:
git checkout <branchname>
git rebase master # move to branch start to the tip of master.
git stash apply # restore working copy, auto merges the changes
解决方案3,复制当前主分支
如果你不介意有多个工作副本(我总是用SVN实际做到这一点),还有第三个解决方案:
mkdir ../newrepos
cd ../newrepos
git init
git remote add origin /path/to/your/repository
git fetch master:remotes/origin/master # fetch remote master to local remotes/origin/master
git checkout -t origin/master # make new "master" branch, link to remote, checkout.
git commit
git push origin master # inject the change in the original repository.
克隆设置在此处手动完成,因为git clone
始终克隆当前活动的分支。
对于更复杂的情况,总是有一个额外的安全防范git diff > to-master.patch
和git apply to-master.patch
。 这使您可以更自由地重置所有内容,并尝试直到您做对了。
在这种情况下,我们正在处理两个分支中存在的文件中的单行修复。 这不会给出任何合并冲突,并允许一些快捷方式,如checkout -m
。
而不是使用git add -i
/ git add -p
你也可以使用git gui
交互式添加模式
(可能是其他git图形界面,在clude提交工具中,例如QGit,有此功能)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.