如果我有N个提交,我如何从N-3个提交分支?


当前回答

魔法可以通过git重置来实现。

创建一个新的分支并切换到它(因此所有最新的提交都存储在这里)git结帐-b your_new_branch切换回以前的工作分支(假设它是主分支)切换到主分支删除最新的x个提交,保持master干净git reset--hard HEAD~x#在您的情况下,x=3

从这一刻起,所有最新的x提交都只在新分支中,不再在上一个工作分支(master)中。

其他回答

还没人提到git开关?

您可以执行以下操作:

git checkout<commit hash>

或使用符号引用:

git结帐头~3

然后:

git开关-c我的新功能分支

使用提交哈希创建分支:

git branch branch_name <commit-hash>

或使用符号引用:

git branch branch_name HEAD~3

要在创建分支时签出分支,请使用:

git checkout -b branch_name <commit-hash or HEAD~3>

我可以这样做:

git branch new_branch_name `git log -n 1 --skip 3 --format=%H`

必须在其中输入跳过值。0是最新的,1是先前的,2是之前的提交,等等。

魔法可以通过git重置来实现。

创建一个新的分支并切换到它(因此所有最新的提交都存储在这里)git结帐-b your_new_branch切换回以前的工作分支(假设它是主分支)切换到主分支删除最新的x个提交,保持master干净git reset--hard HEAD~x#在您的情况下,x=3

从这一刻起,所有最新的x提交都只在新分支中,不再在上一个工作分支(master)中。

一个很好的相关问题是:你怎么用git的--help选项来解决这个问题?让我们试试看:

git branch --help

我们看到这样的输出:

NAME
       git-branch - List, create, or delete branches    

SYNOPSIS
       git branch [--color[=<when>] | --no-color] [-r | -a]
               [--list] [-v [--abbrev=<length> | --no-abbrev]]
               [--column[=<options>] | --no-column]
               [(--merged | --no-merged | --contains) [<commit>]] [--sort=<key>]
               [--points-at <object>] [<pattern>...]
       git branch [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
       git branch (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>]
       git branch --unset-upstream [<branchname>]
       git branch (-m | -M) [<oldbranch>] <newbranch>
       git branch (-d | -D) [-r] <branchname>...
       git branch --edit-description [<branchname>]

Gobbledegook。

在随后的文本中搜索单词“commit”。我们发现:

   <start-point>
       The new branch head will point to this commit. It may be given as a branch name, a
       commit-id, or a tag. If this option is omitted, the current HEAD will be used instead.

我们正在前进!

现在,关注这一行官样文章:

git branch [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]

将其概括为:

git branch <branchname> [<start-point>]

完成了。