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


当前回答

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

完成了。

其他回答

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

完成了。

如果您不确定要提前从哪个提交分支,可以检查提交并检查其代码(参见源代码、编译、测试)

git checkout <sha1-of-commit>

一旦找到了要从中分支的提交,就可以在提交中(即,不必先返回到主提交),只需按通常的方式创建一个分支:

git checkout -b <branch_name>

转到git存储库的特定提交

有时,在处理git存储库时,您希望返回到特定的提交(修订),以便在特定的时间获得项目的快照。要做到这一点,您需要提交的SHA-1哈希,您可以使用以下命令轻松查看日志:

git log --abbrev-commit --pretty=oneline

这将为您提供所有提交和SHA-1哈希的简短版本的紧凑列表。

现在您知道了要转到的提交的哈希值,可以使用以下2个命令之一:

git checkout HASH

or

git reset --hard HASH

结账

git checkout<commit><path>

告诉git将路径的当前状态替换为给定提交中的状态。路径可以是文件或目录。

如果没有给出分支,git将接受HEAD提交。

git checkout <path> // restores path from your last commit. It is a 'filesystem-undo'.

如果没有给定路径,git将HEAD移动到给定的提交(从而更改您正在进行的提交)。

git checkout branch //means switching branches.

重置

git reset <commit> //re-sets the current pointer to the given commit.

如果您在一个分支上(通常应该是),HEAD和这个分支将被移动到提交。

如果您处于分离HEAD状态,git reset只会移动HEAD。要重置分支,请首先检查它。

如果你想了解更多关于git重置和git结账的区别,我建议你阅读git官方博客。

在Github repo上快速执行此操作的方法如下:

从分支机构查找特定提交在SHA id旁边,单击“浏览历史记录中此时的回购”在这里,您可以从此提交创建一个新分支

要在Eclipse中执行此操作:

转到“Git存储库探索”透视图。展开“标记”并选择要从中创建分支的提交。右键单击提交并选择“创建分支”。提供分支名称。

它将为您创建一个本地分支。然后,每当您推送更改时,您的分支将被推送到远程服务器。