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


当前回答

我使用了Git-Gui(随Git for Windows提供)。

在菜单栏上,打开分支菜单并选择创建。。。在“分支名称”部分中指定新分支的名称在StartRevision部分中选择RevisionExpression:并输入提交ID(我刚刚使用了VisualStudio中的8位ID,它起了作用)

其他回答

使用Sourcetree|最简单的方法。

首先,签出要进行特定提交以创建新分支的分支。然后查看工具栏,选择Repository>Branch。。。快捷方式是Command+Shift+B。然后选择要执行的特定提交。并给出一个新的分支名称,然后创建一个分支!

只需运行:

git checkout -b branch-name <commit>

例如:

git checkout -b import/january-2019 1d0fa4fa9ea961182114b63976482e634a8067b8

带有参数-b的checkout命令将创建一个新分支,并将您切换到该分支

这将使用一个命令创建分支:

git push origin <sha1-of-commit>:refs/heads/<branch-name>

我更喜欢这种方式,因为它会立即创建分支(之后不需要额外的push命令)。

我使用了Git-Gui(随Git for Windows提供)。

在菜单栏上,打开分支菜单并选择创建。。。在“分支名称”部分中指定新分支的名称在StartRevision部分中选择RevisionExpression:并输入提交ID(我刚刚使用了VisualStudio中的8位ID,它起了作用)

一个很好的相关问题是:你怎么用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>]

完成了。