使用 GIT,如何有选择地将一个提交的更改合并到另一个“分叉”上?
采取这样的场景:
- 我决定在 github.com 上“分叉”一个代码库,并开始执行我的例程:编辑 - 提交 - 推送;又名黑客黑客黑客。
- 在我做了一些更改之后,我看到其他人对同一个项目做了一些更改,我喜欢它们!
- 我决定将它们合并到我的中。问题是,我只想要他所做的多次提交中的一个特定提交的“一部分”。
将这些选定数量的更改合并到我的“分叉”中的最有效方法是什么?
Take this scenario:
- I decide to 'fork' a codebase on github.com, and start doing my routine: Edit - Commit - Push; aka hack hack hack.
- After I made some changes, I see some changes another person has made on the same project, and I like them!
- I decide I want to merge them into mine. Problem is, I only want a 'portion' of one particular commit, out of several commits he has made.
What would be the most efficient method of getting these select amount of changes, merged into my 'fork'?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 IRC 世界中掀起波澜之后,有人给出了一个很好的解决方案:
希望这可以帮助其他有同样问题的人!
编辑:好吧,也许事情没那么简单。以下是完整步骤:
首先,您必须位于存储库中,执行必要的
cd
-ing 操作以访问您的工作目录。您现在需要添加远程分支,然后获取它。通过以下方式执行此操作:
git 远程添加 someUser git://github.com/someUser/someRepo.git
git fetch someUser
您现在可以运行诸如
git log someUser/master
以查找要“部分”合并的提交 SHA1。获得 SHA 后,您现在可以运行:
gitcherry-pick-n SHA1
其中
SHA1
是提交 SHA,呃!很可能会出现冲突,具体取决于提交的时间以及项目特定区域更改的频率。抱歉,您必须通过值得信赖的编辑器手动解决这些问题。因此,拿出 VIM 或您使用的其他任何东西,并解决冲突,直到您到达您喜欢更改的阶段。
您现在必须将索引重置为 HEAD 版本,然后您可以使用可靠的 GIT
add --patch
命令来选择您想要的更改:git 重置 HEAD
git add --patch
或git add -p
耶!提交时间:
git commit -m“我合并了选定数量的更改”
清理混乱(你在
git add --patch) 并且仅将选定的更改保留在工作存储库中,运行:
git reset --hard HEAD
显然
git checkout -f
也是另一个选择。After trolling the waves of the IRC world, someone gave a great solution:
Hopefully that helps anyone else with the same question!
EDIT: OK, maybe it isn't quite that simple. Here are the full steps:
First you have to be in your repository, do the necessary
cd
-ing to get to your working directory.You now need to add the remote branch, and then fetch it. Do this by:
git remote add someUser git://github.com/someUser/someRepo.git
git fetch someUser
You can now run commands such as
git log someUser/master
to find the commit SHA1 you want to merge in 'partially'.Once you have the SHA, you can now run:
git cherry-pick -n SHA1
where
SHA1
is the commit SHA, duh!There quite possibly will be conflicts, depending how old the commit is, and how often that particular area of the project changes. Sorry, but you have to manually resolve these with your trusty editor. So pull out VIM, or whatever else you use, and hack away at the conflicts until you get to the stage where you like the changes.
You now have to reset the index to the HEAD revision, then you can then use the trusty GIT
add --patch
command to pick and choose what changes you want:git reset HEAD
git add --patch
orgit add -p
Yay! Commit time:
git commit -m "I merged a select amount of changes"
To clean up the mess (The stuff you said no to in
git add --patch
) and only keep the selected changes in your working repository, run:git reset --hard HEAD
Apparently
git checkout -f
is another option as well.我真的很喜欢 Tim 的解决方案,但有时我喜欢在 vimdiff 中进行修改。我对这个问题的解决方案很粗糙,但它对我有用,因为我喜欢 vim。
我将 vimdiff 设置为我的 diff 工具,然后有选择地合并我比较分支:
然后我转到包含当前分支版本的窗格并在 vim 中编辑原始版本(有时这不是必需的,但有时 vimdiff 会在/tmp) 并禁用只读模式:
现在我可以使用 vim 的修补工具(例如
do
和dp
)来应用我想要的内容,并随时进行其他小编辑。完成后,我保存文件,退出 vim,然后像常规编辑一样在 git 中暂存并提交文件。正如我所说,这并不是特别复杂,但它非常强大,而且仍然纯粹在命令行中。请务必添加明确的提交消息,因为 git 不会自动为您包含合并消息。
I really like Tim's solution, however sometimes I like tinkering in vimdiff. My solution to this problem is crude, but it works for me because I like vim.
I have vimdiff set as my difftool, and then to selectively merge I diff the branch:
Then I go to the pane with the current branch's version and edit the original in vim (sometimes this isn't necessary, but sometimes vimdiff opens a version in /tmp) and disable read-only mode:
Now I can use vim's patch tools such as
do
anddp
to apply what I want, and make other little edits as I go. When I am done, I save the file, exit out of vim, and then stage and commit the file in git like a regular edit.As I said, this isn't particularly sophisticated, but it is very powerful, and still purely in the command line. Just be sure to add a clear commit message, as git won't automatically include a merge message for you.
我不清楚你想要什么。如果您只想引入其他分支的某些提交,可以使用 gitcherry-pick SHA。关于 gitready 的完整说明。
I'm not clear on what you want. If you want to bring in only certain commits from the other forks, you can use
git cherry-pick SHA
. Full explanation on gitready.如果您只想要一个提交的端口,那么您最好选择您想要的提交并重置您不想触及的文件。
当然,如果您在一个文件中有多个修改的部分,并且只想保留其中的一些部分,那么您别无选择,只能手动编辑文件,删除它们的更改。
If you only want a port of a commit, you’re probably best off by cherry-picking the commit you want and resetting files you don’t want touched.
Of course, if you have several modified parts in a file and only want to keep some of them you have no choice but to edit the file manually, cutting out their changes.