`
java-mans
  • 浏览: 11339647 次
文章分类
社区版块
存档分类
最新评论

git cherry-pick简介

 
阅读更多
本文编辑整理自:
git cherry-pick用于把另一个本地分支的commit修改应用到当前分支。
实际问题
在本地master分支上做了一个commit(38361a68138140827b31b72f8bbfd88b3705d77a) , 如何把它放到 本地 old_cc 分支上?
办法之一: 使用cherry-pick. 根据git 文档:
Apply the changes introduced by some existing commits
就是对已经存在的commit 进行apply (可以理解为再次提交)
简单用法
git cherry-pick <commit id>
例如:
$ git checkout old_cc
$gitcherry-pick38361a68
1.如果顺利,就会正常提交。结果:
Finished one cherry-pick.
# On branch old_cc
# Your branch is ahead of 'origin/old_cc' by 3 commits.
2. 如果在cherry-pick 的过程中出现了冲突
Automatic cherry-pick failed. After resolving the conflicts,
mark the corrected paths with 'git add <paths>' or 'git rm <paths>'
and commit the result with:

git commit -c 15a2b6c61927e5aed6718de89ad9dafba939a90b

就跟普通的冲突一样,手工解决:
执行git status 看哪些文件出现冲突
$ git status
both modified: app/models/user.rb
接着手动解决冲突的文件,然后通过git add把改到添加到索引,最后执行git commit提交修改。
$ vim app/models/user.rb
$ git add app/models/user.rb
git commit -c <原commit号>
git-cherry-pick(1)
==================

NAME
----
git-cherry-pick - Apply the changes introduced by some existing commits

SYNOPSIS
--------
'git cherry-pick' [--edit] [-n] [-m parent-number] [-s] [-x] [--ff] <commit>...

DESCRIPTION
-----------

Given one or more existing commits, apply the change each one
introduces, recording a new commit for each. This requires your
working tree to be clean (no modifications from the HEAD commit).

When it is not obvious how to apply a change, the following
happens:

1. The current branch and `HEAD` pointer stay at the last commit
successfully made.
2. The `CHERRY_PICK_HEAD` ref is set to point at the commit that
introduced the change that is difficult to apply.
3. Paths in which the change applied cleanly are updated both
in the index file and in your working tree.
4. For conflicting paths, the index file records up to three
versions, as described in the "TRUE MERGE" section of
linkgit:git-merge[1]. The working tree files will include
a description of the conflict bracketed by the usual
conflict markers `<<<<<<<` and `>>>>>>>`.
5. No other modifications are made.

See linkgit:git-merge[1] for some hints on resolving such
conflicts.

OPTIONS
-------
<commit>...::
Commits to cherry-pick.
For a more complete list of ways to spell commits, see
linkgit:gitrevisions[7].
Sets of commits can be passed but no traversal is done by
default, as if the '--no-walk' option was specified, see
linkgit:git-rev-list[1].

-e::
--edit::
With this option, 'git cherry-pick' will let you edit the commit
message prior to committing.

-x::
When recording the commit, append to the original commit
message a note that indicates which commit this change
was cherry-picked from. Append the note only for cherry
picks without conflicts. Do not use this option if
you are cherry-picking from your private branch because
the information is useless to the recipient. If on the
other hand you are cherry-picking between two publicly
visible branches (e.g. backporting a fix to a
maintenance branch for an older release from a
development branch), adding this information can be
useful.

-r::
It used to be that the command defaulted to do `-x`
described above, and `-r` was to disable it. Now the
default is not to do `-x` so this option is a no-op.

-m parent-number::
--mainline parent-number::
Usually you cannot cherry-pick a merge because you do not know which
side of the merge should be considered the mainline. This
option specifies the parent number (starting from 1) of
the mainline and allows cherry-pick to replay the change
relative to the specified parent.

-n::
--no-commit::
Usually the command automatically creates a sequence of commits.
This flag applies the changes necessary to cherry-pick
each named commit to your working tree and the index,
without making any commit. In addition, when this
option is used, your index does not have to match the
HEAD commit. The cherry-pick is done against the
beginning state of your index.
+
This is useful when cherry-picking more than one commits'
effect to your index in a row.

-s::
--signoff::
Add Signed-off-by line at the end of the commit message.

--ff::
If the current HEAD is the same as the parent of the
cherry-pick'ed commit, then a fast forward to this commit will
be performed.

--strategy=<strategy>::
Use the given merge strategy. Should only be used once.
See the MERGE STRATEGIES section in linkgit:git-merge[1]
for details.

-X<option>::
--strategy-option=<option>::
Pass the merge strategy-specific option through to the
merge strategy. See linkgit:git-merge[1] for details.

EXAMPLES
--------
git cherry-pick master::

Apply the change introduced by the commit at the tip of the
master branch and create a new commit with this change.

git cherry-pick ..master::
git cherry-pick ^HEAD master::

Apply the changes introduced by all commits that are ancestors
of master but not of HEAD to produce new commits.

git cherry-pick master{tilde}4 master{tilde}2::

Apply the changes introduced by the fifth and third last
commits pointed to by master and create 2 new commits with
these changes.

git cherry-pick -n master~1 next::

Apply to the working tree and the index the changes introduced
by the second last commit pointed to by master and by the last
commit pointed to by next, but do not create any commit with
these changes.

git cherry-pick --ff ..next::

If history is linear and HEAD is an ancestor of next, update
the working tree and advance the HEAD pointer to match next.
Otherwise, apply the changes introduced by those commits that
are in next but not HEAD to the current branch, creating a new
commit for each new change.

git rev-list --reverse master \-- README | git cherry-pick -n --stdin::

Apply the changes introduced by all commits on the master
branch that touched README to the working tree and index,
so the result can be inspected and made into a single new
commit if suitable.

The following sequence attempts to backport a patch, bails out because
the code the patch applies to has changed too much, and then tries
again, this time exercising more care about matching up context lines.

------------
$ git cherry-pick topic^ <1>
$ git diff <2>
$ git reset --merge ORIG_HEAD <3>
$ git cherry-pick -Xpatience topic^ <4>
------------
<1> apply the change that would be shown by `git show topic^`.
In this example, the patch does not apply cleanly, so
information about the conflict is written to the index and
working tree and no new commit results.
<2> summarize changes to be reconciled
<3> cancel the cherry-pick. In other words, return to the
pre-cherry-pick state, preserving any local modifications you had in
the working tree.
<4> try to apply the change introduced by `topic^` again,
spending extra time to avoid mistakes based on incorrectly matching
context lines.

分享到:
评论

相关推荐

    git 一个可以提高开发效率的命令:cherry-pick详解

    主要介绍了git 一个可以提高开发效率的命令:cherry-pick,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

    learn-git-cherry-pick:一个展示如何使用基本的cherry-pick 命令的repo

    git cherry-pick基本上使您能够在 HEAD 所指的当前位置下复制一系列提交。 基本的 git cherry-pick 命令 $ git cherry-pick commit1 commit2 commit3 ... commitN (1) 每个提交都引用一个 SHA-1 哈希值。 请注意...

    ZhongJinHacker#notes#git_cherry-pick总结1

    cherry-pick : 精心挑选,挑选一个我们需要的 commit 进行操作。它可以用于将在==其他分支==上的 commit 移植到==当前的分支==。

    git 视频教程

    git视频教程.8.1.Git 命令 - git cherry-pick.mp4 git视频教程.8.2.Git 命令 - git rebase.mp4 git视频教程.8.3.Git 命令 - git rebase.mp4 git视频教程.8.4.Git 命令 - git revert.mp4 git视频教程.9.Git 命令 - ...

    Git-2.21.0-64-bit.zip

    * "git cherry-pick/revert" learned a new "--skip" action. * The tips of refs from the alternate object store can be used as starting point for reachability computation now. * Extra blank lines ...

    cherry-pick:可以在课堂上使用的绘图系统,这也是我的第一个Vue项目

    樱桃采摘 一个Vue.js项目,用于在类(或任何其他方案)上绘制...git clone https://github.com/BirkhoffLee/cherry-pick cd cherry-pick chmod +x ./build.sh ./build.sh docker-compose up -d docker-compose logs -f

    Git-2.23.0/win64/32/mac 多文件

    *“git cherry-pick / revert”学会了一个新的“--skip”动作。 *来自备用对象库的引用提示可用作 现在可达性计算的起点。 *“git status”输出中的额外空行已减少。 *存储库中的提交可以用多个来描述 现在...

    annotated-git-session:带注释的git会话,演示一些基本的git概念

    注释的Git会话 用于向Git引入新手的一些基本思想。 这是一种糟糕的静态版本,它坐在使用Git的人旁边,告诉那些人在做什么时,他们在做什么,对于... git cherry-pick git commit git diff git fetch (提到了git pu

    git常用命令总结 word文档

    git常用命令总结,里面的常用命令我自己都敲过一遍,有图有注释,觉得有用,回来给个五星,下面是目录 Git 命令 -by 挂件 2017-9-5 2 一、 准备工作 2 ...1.git cherry-pick 8 2.git reflog 9 十五、 遇到的问题 9

    【最新版】Tower-5.0-240.zip【亲测可用】最好的Git客户端

    拉取请求 • 单行登台 • 交互式基础库 • 子模块 • Git LFS • Git-Flow • 文件历史记录 • 责备 • Cherry-Pick • 获得Git的所有功能。 高产的 快速操作 • 单击克隆 • 自动存储和获取 • 快速打开 • 使用...

    dev-goodies:用于源代码树和重构工作的脚本和工具

    这是我们必须提供的:Git 脚本bin/git-cherry-files :类似于git cherry-pick ,但允许您仅指定提交中所需的文件。 bin/git-hatchet :在一个分支上进行一次提交,并根据文件路径将其拆分为跨多个分支的多个提交。 ...

    Git学习笔记.pdf

    Git中关于rebase, merge, cherry-pick, rm等的深入讲解

    Linux环境下Git命令自动补全脚本

    When you git&lt;tab&gt;&lt;tab&gt; add cherry diff instaweb rebase show-ref am cherry-pick fast-export log relink st annotate ci fetch los

    迷你壳

    m " message "$ git checkout main //go to main to fetch this commit 从工作分支到主分支的提货提交$ git reflog //copy your commit ID$ git cherry-pick ID //paste the ID 推送请求之前,请更新本地文件夹$ git...

    gitbin

    进一步精简git命令 背景与目标 ...git cherry-pick 英石 git状态 状态 git状态 日志 git日志 ll git log --graph --all --pretty =格式:'%Cred%h%Creset-%C(黄色)%d%Creset%s%Cgreen(%cr)%

    java8源码-java8-source-code:Java8API源代码

    java8 源码 Java8-Source-Code 简介 Java8源码学习 ├── com/ ├── java/(常用代码都在此文件夹下) │ ├── lang/ │ ...git cherry-pick. 技术交流 博客地址: QQ群:专注的程序猿 282087535

    git-state::test_tube:探查git仓库状态

    CherryPick CherryPickSequence Bisect Rebase RebaseInteractive RebaseMerge ApplyMailbox ApplyMailboxOrRebase 用法 命令行用法: cd my-repository/ git-state # or git-state my-repository/ 执照 该项目是...

    manifest:我的个人local_manifest.xml以及构建exynos5410 ROM所需的所有命令

    git fetch refs / changes / 96/22096/11 && git cherry-pick FETCH_HEAD cd ../ .. cd external / libselinux git am ../../device/samsung/exynos5410-common/patches/external_libselinux/556a9e9250

    Cherry Pie-crx插件

    语言:English 此扩展程序在Github的Pull Request页面上添加了一个按钮...由于您的提交历史记录不可读,因此您不能使用git cherry-pick并希望您可以返回过去并将其转换为可以在git日志中很好地播放的内容。 现在你可以。

Global site tag (gtag.js) - Google Analytics