git copy one commit to new branch

时间:2016-09-23 13:37:16

标签: git

我想创建一个只有一个特定提交副本的新分支(来自master,即)。我该怎么做?

我最接近的是:

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

基本上,我希望上面没有“MESSAGE MESSAGE MESSAGE”提交。

我挑选后出错:

$ 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'

1 个答案:

答案 0 :(得分:2)

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

<new_branch>

     

创建一个名为<start_point>的新孤立分支,从中开始   git checkout <start_point>然后切换到它。第一次提交这个新的   分支将没有父母,它将成为新的根   历史完全脱离了所有其他分支和   提交。

     

索引和工作树的调整就像您一样   以前运行“<start_point>”。这允许你   开始记录一组类似的路径的新历史记录   git commit -a通过轻松运行“#!/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'" ”来创建根目录   提交。

修改

以下脚本将一次完成这项工作(请注意,为了消除可能的失败原因,它需要从未跟踪的文件中清除工作树,但它会要求获得这样做的许可):< / p>

<强> copy_as_root_commit

copy_as_root_commit <commitid> <new_branch>

<强>用法

copy_as_root_commit master~4 NEW_ROOT

copy_as_root_commit ce04aa6 NEWBRANCH

<强>实施例

{{1}}