使用git从之前的提交中分支出来

如果我有n提交,我怎么能从n-3提交分支? 我可以看到每个提交的散列。

你可以通过散列创build分支,

git branch branchname <sha1-of-commit> 

或通过使用符号参考。

 git branch branchname HEAD~3 

在GitHub.com上做到这一点

  1. 去你的项目。
  2. 点击“提交”。
  3. 点击您要从中分支的提交<> (“浏览历史中的此处的存储库”)。
  4. 点击左上angular的“tree:xxxxxx”,在语言统计栏下方,你会看到“查找或创build分支”选项(只需在那里input新的分支名称) 从之前的提交分支

如果你不确定你想从哪个提交中提前分支,你可以检查提交并检查他们的代码(见源代码,编译,testing)

 git checkout <sha1-of-commit> 

一旦你发现了你想要分支的提交,你可以在提交内部(也就是说,不需要先回到主目录)通过通常的方式创build一个分支来实现:

 git checkout -b <branch_name> 

魔法可以通过git reset来完成。

  1. 创build一个新的分支并切换到它(所以你所有的最新的提交都存储在这里)

    git checkout -b your_new_branch

  2. 切换回你以前的工作分支(假设它是主人)

    git checkout master

  3. 删除最新的x提交,保持主干净

    git reset --hard HEAD~x # in your case, x = 3

从这一刻起,所有最新的x提交都只在新的分支中,而不是在之前的工作分支(主)中。

在Github回购上做一个快速的方法如下:

  • find你的分支的具体提交
  • 在SHA编号旁边,点击“浏览历史上此时的回购”
  • 在这里你可以从这个提交中创build一个新的分支 在这里输入图像描述

为了在Eclipse中做到这一点,

  • 转到“Git Repository Exploring”视图,
  • 展开“标签”并select您想要创build分支的提交,
  • 右键单击提交,然后select“创build分支”
  • 提供分支名称。

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

 git checkout -b <branch-name> <sha1-of-commit> 

我能够这样做:

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

必须input跳过值的位置。 0是最新的,1是以前的,2是之前的提交等等。

你可以在Stash里做。

  1. 点击提交
  2. 在屏幕的右上方点击“标记此提交”
  3. 然后,您可以从刚创build的标签创build新的分支。

您可以使用该提交的散列来创build分支,

 git branch branchname <sha1 of the commit> 

一个很好的相关问题是:如何使用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>] 

官样文章。

search单词“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. 

我们到了某个地方!

现在,专注于这个gobbledegook的这一行:

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

对此凝结:

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

并做了。