Git cherry-pick和format-patch(给分支打补丁)区别和使用场景



How to take commits from a different repository : Git cherry-pick & format-patch | Void Canvas  http://voidcanvas.com/cherry-pick-format-patch/


总结:

Git cherry-pick <commit-id> : 把一个commit-id的commit应用到当前分支上, 一次只能选择一个"补丁"

git format-patch <old_sha>...<new_sha> -o <patch_dir> : 一次性把多个commit-id做成"补丁", 能同时应用到当前分支上

所以, 当要场景上, 取决于要在当前分支上应用的"commit-id数量", 简单时可用cherry-pick, 复制特性要应用到当前分支时, 可用format-patch



How to take commits from a different repository : Git cherry-pick & format-patch

Don’t know how many of you faced this situation to cherrypick or pull latest commits from an entirely different repo. But this situation is very obvious if you are customizing an existing product for a client and the base product is also getting updated frequently by the team of that.

This article talks about

The articles talks about different processes to to pickup your changes from a different repository. Processes are mentioned in the bullet points below.

By cherry-pick

Cherry picking is a nice process to pickup selective commits from somewhere. It’s easy when you are doing inter branch of the same repo; but a little tricky if you wish to get the commits of a different repository. Below is the code to do so.

What we did here is pulled a certain branch of the repo you want to get commits from. Fetching doesn’t merge the changes of that repo to your existing one (if you want all changes to be merged to your current repo branch you need to use $ git pull)

You can also use git gui to cherry-pick the commits after fetching. Git gui is a tool which is auto installed in your device with git.

–signoff helps to keep track of the person who cherry-picks.

By creating a commit patch

If you have a hell lot of commits to get, the previous way of cherrypicking selective ones may become a stiff task. In that case you can create a patch of multiple successive commits and apply it to your current one. This can be done in two ways. One, cloning the third party repo in some other directory. Two, fetching the required branch of the third party repo in some other branch of your repo.

Way 1 to create patch

Assuming you have the third party repo cloned in your machine and the current branch there is the one from where you want to pick the commits.

So your patch file is created and available with your current repo. You can check the status of the patch using the following command (The command will not apply the patch changes to your directory).

Way 2 to create patch

If you don’t have or don’t want to clone the repo in a separate folder, you can create a branch in your own repo which will hold the changes of the third party repo. After that create the parch from there. Below is the code.

Apply the patch commits

Well, now your patch file is ready to be applied (using any of the two ways mentioned above). Let’s apply it with the commands below.

git am --signoff 0001-some-commit-msg.patch

This will apply the changes to your repo. If conflicts appears you need to resolve them and continue.

–signoff helps to keep track of the person who applied the patch.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值