git cherry-pick简介(转载)

转自:http://hubingforever.blog.163.com/blog/static/1710405792012587115533/

本文编辑整理自:

http://sg552.iteye.com/blog/1300713

http://web.mit.edu/bitbucket/git-doc/git-cherry-pick.txt

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.

时间: 2024-08-23 07:17:28

git cherry-pick简介(转载)的相关文章

Android提供的LruCache类简介[转载]

转自:here 1 package android.util; 2 3 import java.util.LinkedHashMap; 4 import java.util.Map; 5 6 /** 7 * A cache that holds strong references to a limited number of values. Each time 8 * a value is accessed, it is moved to the head of a queue. When a

北大ACM题库习题分类与简介(转载)

在百度文库上找到的,不知是哪位大牛整理的,真的很不错! zz题 目分类 Posted by fishhead at 2007-01-13 12:44:58.0 -------------------------------------------------------------------------------- acm.pku.edu.cn 1. 排序 1423, 1694, 1723, 1727, 1763, 1788, 1828, 1838, 1840, 2201, 2376, 23

Git 学习笔记&lt;简介与安装&gt; (一)

Git,开源中国以及GitHub所使用的系统, Is A 一个分布式版本控制系统 Be Used to 为团队合作写代码提供方便的管理系统.几乎满足你所有关于合作写代码的幻想. Has 本地端:工作区.版本库 (版本库还含有一个暂存区) 远程仓库:版本库(用来储存版本库的服务器) How To Install Linux: 首先,先输入git,看看是否安装Git: $ gitThe program 'git' is currently not installed. You can install

Java 理论与实践: 非阻塞算法简介--转载

在不只一个线程访问一个互斥的变量时,所有线程都必须使用同步,否则就可能会发生一些非常糟糕的事情.Java 语言中主要的同步手段就是synchronized 关键字(也称为内在锁),它强制实行互斥,确保执行 synchronized 块的线程的动作,能够被后来执行受相同锁保护的synchronized 块的其他线程看到.在使用得当的时候,内在锁可以让程序做到线程安全,但是在使用锁定保护短的代码路径,而且线程频繁地争用锁的时候,锁定可能成为相当繁重的操作. 在 “流行的原子” 一文中,我们研究了原子

XMPP 简介 转载

XMPP : The Extensible Messaging and Presence Protocol 中文全称:可扩展通讯和表示协议 简介:可扩展通讯和表示协议 (XMPP) 可用于服务类实时通讯.表示和需求响应服务中的XML数据元流式传输.XMPP以Jabber协议为基础,而Jabber是即时通讯中常用的开放式协议.XMPP is the IETF's formalization of the base XML streaming protocols for instant messag

git 冲突解决(转载)

gerrit是不会解决冲突的,如果两个人同时改了一个文件的同一行,就会冲突,你将会看到Review in Progress并且最下面会有Your change could not be merged due to a path conflict. 如果在冲突提交者机器上解决远程冲突 cd demo git fetch origin git rebase origin/develop 修改冲突文件 git add . git rebase --continue git push origin 不会

GitHub超详细图文攻略 - Git客户端下载安装 GitHub提交修改源码工作流程 Git分支 标签 过滤 Git版本工作流(转载)

最近听同事说他都在使用GitHub,GitHub是程序员的社区,在里面可以学到很多书上学不到的东西,所以最近在准备入手这方面的知识去尝试学习,正好碰到这么详细完整的文章,就转载了,希望对自己和大家有帮助. GitHub操作总结 : 总结看不明白就看下面的详细讲解. GitHub操作流程 : 第一次提交 : 方案一 : 本地创建项目根目录, 然后与远程GitHub关联, 之后的操作一样; -- 初始化Git仓库 :git init ; -- 提交改变到缓存 :git commit -m 'desc

git学习之简介(一)

一.前言 史上最浅显易懂的Git教程! 为什么要编写这个教程?因为我在学习Git的过程中,买过书,也在网上Google了一堆Git相关的文章和教程,但令人失望的是,这些教程不是难得令人发指,就是简单得一笔带过,或者,只支离破碎地介绍Git的某几个命令,还有直接从Git手册粘贴帮助文档的,总之,初学者很难找到一个由浅入深,学完后能立刻上手的Git教程. 既然号称史上最浅显易懂的Git教程,那这个教程有什么让你怦然心动的特点呢? 首先,本教程绝对面向初学者,没有接触过版本控制概念的读者也可以轻松入门

Git的原理简介和常用命令

Git和SVN是我们最常用的版本控制系(Version Control System, VCS),当然,除了这二者之外还有许多其他的VCS,例如早期的CVS等.顾名思义,版本控制系统主要就是控制.协调各个版本的文档内容的一致性,这些文档包括但不限于代码文件.图片文件等等.早期SVN占据了绝大部分市场,而后来随着Git的出现,越来越多的人选择将它作为版本控制工具,社区也越来越强大.相较于SVN,最核心的区别是Git是分布式的VCS,简而言之,每一个你pull下来的Git仓库都是主仓库的一个分布式版