简体   繁体   English

git copy 一个提交到新分支

[英]git copy one commit to new branch

I would like to create a new branch with a copy of one particular commit only (from master, ie).我想创建一个新分支,只包含一个特定提交的副本(来自 master,即)。 How do I do that?我怎么做?

The closest I got to that is something like:我最接近的是:

git checkout --orphan NEWBRANCH
git rm -rf .
git commit "MESSAGE MESSAGE MESSAGE"
git cherry-pick -x <hash>

Basically, I would like the above without the "MESSAGE MESSAGE MESSAGE" commit.基本上,我希望上面没有“MESSAGE MESSAGE MESSAGE”提交。

I got an error after cherry-pick:挑选樱桃后出现错误:

$ git cherry-pick -x 68cc6733a14ec571c0abb0d4e77f53d93446f009
error: could not apply 68cc673... asdvasdflmdamfvla
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
git checkout --orphan NEWBRANCH <commitid>
git commit -a

git checkout --orphan <new_branch> [<start_point>]

Create a new orphan branch, named <new_branch> , started from <start_point> and switch to it.创建一个新的孤立分支,名为<new_branch> ,从<start_point>开始并切换到它。 The first commit made on this new branch will have no parents and it will be the root of a new history totally disconnected from all the other branches and commits.在这个新分支上进行的第一次提交将没有父级,它将成为与所有其他分支和提交完全断开的新历史的根。

The index and the working tree are adjusted as if you had previously run " git checkout <start_point> ".索引和工作树的调整就像您之前运行过“ git checkout <start_point> ”一样。 This allows you to start a new history that records a set of paths similar to <start_point> by easily running " git commit -a " to make the root commit.这允许您通过轻松运行“ git commit -a ”来进行根提交,从而开始记录一组类似于<start_point>的路径的新历史记录。


EDIT编辑

The following script will do the job in a single shot (note that in order to eliminate possible causes for failure, it will need to clean the working tree from untracked files, but it will ask for permission of doing so):以下脚本将一次性完成这项工作(请注意,为了消除可能的失败原因,它需要从未跟踪的文件中清除工作树,但它会请求这样做的许可):

copy_as_root_commit copy_as_root_commit

#!/bin/sh

myname="$(basename "$0")"

if [ $# -ne 2 ]
then
    echo 1>&2 "Usage: $myname <commitid> <new_branch>"
    exit 1
fi


commitid=$(git rev-parse "$1")
new_branch="$2"

set -e
untracked_stuff="$(git clean -dxf -n)"
if [ "$untracked_stuff" ]
then
    echo "$myname needs to clean the working tree before proceeding:"
    printf "%s\n" "$untracked_stuff"
    while read -p "Remove above files? (y/n) " answer
    do
        case "$answer" in
            [yY]) break ;;
            [nN]) exit 1 ;;
        esac
    done
fi

git clean -dxf                                               \
&& git checkout --orphan "$new_branch" "$commitid"           \
&& git commit -m "Initial commit (a copy of $commitid)"      \
&& echo "Successfully created new root branch '$new_branch'" \
|| echo "Failed to create new root branch '$new_branch'"

Usage :用法

copy_as_root_commit <commitid> <new_branch>

Examples :例子

copy_as_root_commit master~4 NEW_ROOT

copy_as_root_commit ce04aa6 NEWBRANCH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM