您可以通过哈希创建分支:
git branch branchname <sha1-of-commit>
或使用符号引用:
git branch branchname HEAD~3
要在创建分支时签出分支,请使用
git checkout -b branchname <sha1-of-commit or HEAD~3>
要在 github.com 上执行此操作:
魔术可以通过git reset来完成。
创建一个新分支并切换到该分支(因此所有最新提交都存储在此处)
git checkout -b your_new_branch
切换回上一个工作分支(假设它是主分支)
git checkout master
删除最新的 x 提交,保持主数据干净
git reset --hard HEAD~x # in your case, x = 3
从这一刻起,所有最新的 x 提交都仅在新分支中,而不再在先前的工作分支(主节点)中。
如果您不确定要从哪个分支提前分支,可以通过以下方式检查并检查其代码(请参见源代码,编译,测试):
git checkout <sha1-of-commit>
一旦找到要分支的提交,您就可以在提交内执行此操作(即,无需先返回主节点),只需以通常的方式创建分支即可:
git checkout -b <branch_name>
git checkout -b <branch-name> <sha1-of-commit>
只需运行:
git checkout -b branch-name <commit>
例如 :
git checkout -b import/january-2019 1d0fa4fa9ea961182114b63976482e634a8067b8
参数为-b
的checkout
命令将创建一个新分支,并将您切换到该分支
要在 Eclipse 中执行此操作:
它将为您创建一个本地分支。然后,每当您推送更改时,您的分支便会被推送到远程服务器。
我能够这样做:
git branch new_branch_name `git log -n 1 --skip 3 --format=%H`
您必须在其中输入跳过值的位置。 0 是最新的,1 是前一个,2 是在此之前的提交,依此类推。
一个很好的相关问题是:如何使用 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.
我们到了某个地方!
现在,关注 gobbledegook 的这一行:
git branch [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
对此进行压缩:
git branch <branchname> [<start-point>]
并做了。