CB *_*ley 2327
您可以通过哈希创建分支:
git branch branchname <sha1-of-commit>
Run Code Online (Sandbox Code Playgroud)
或者使用符号引用:
git branch branchname HEAD~3
Run Code Online (Sandbox Code Playgroud)
要在创建分支时签出分支,请使用
git checkout -b branchname <sha1-of-commit or HEAD~3>
Run Code Online (Sandbox Code Playgroud)
One*_*oob 238
要在github.com上执行此操作:
Jin*_* Li 78
魔法可以通过git reset来完成.
创建一个新分支并切换到它(所以你的所有最新提交都存储在这里)
git checkout -b your_new_branch
切换回上一个工作分支(假设它是主人)
git checkout master
删除最新的x提交,保持master清理
git reset --hard HEAD~x # in your case, x = 3
从此刻起,所有最新的x提交仅在新分支中,而不再在您之前的工作分支(master)中.
sta*_*anm 68
如果您不确定要提前分支哪个提交,可以检查提交并检查其代码(请参阅源代码,编译,测试)
git checkout <sha1-of-commit>
Run Code Online (Sandbox Code Playgroud)
一旦你发现你想要分支的提交,你就可以在提交中做到这一点(即不先回到master),只需通过常规方式创建一个分支:
git checkout -b <branch_name>
Run Code Online (Sandbox Code Playgroud)
Tyl*_*ong 21
git checkout -b <branch-name> <sha1-of-commit>
Run Code Online (Sandbox Code Playgroud)
Jai*_*oya 16
这就是我所做的:
C:\Users\[path]\build>git checkout -b responsivenavigation 8a75b001096536b3216022484af3026aa9c7bb5b
Switched to a new branch 'responsivenavigation'
C:\Users\jaimemontoya\[path]\app>git branch
master
* responsivenavigation
Run Code Online (Sandbox Code Playgroud)
在本例中,8a75b001096536b3216022484af3026aa9c7bb5b
was 和旧提交属于该master
分支。
小智 14
还没人提到git switch吗?
你可以做:
git checkout <commit-hash>
或者使用符号引用:
git checkout HEAD~3
进而:
git switch -c my-new-feature-branch
Pur*_*ket 12
一个很好的相关问题是:您如何使用--help
git 选项来解决这个问题?让我们试试这个:
git branch --help
Run Code Online (Sandbox Code Playgroud)
我们看到这个输出:
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>]
Run Code Online (Sandbox Code Playgroud)
狼吞虎咽。
在随后的文本中搜索“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.
Run Code Online (Sandbox Code Playgroud)
我们正在到达某个地方!
现在,专注于 gobbledegook 的这一行:
git branch [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
Run Code Online (Sandbox Code Playgroud)
将其浓缩为:
git branch <branchname> [<start-point>]
Run Code Online (Sandbox Code Playgroud)
并做了。
Ale*_*lov 10
这将使用一个命令创建分支:
git push origin <sha1-of-commit>:refs/heads/<branch-name>
Run Code Online (Sandbox Code Playgroud)
我更喜欢这种方式而不是上面发布的方式,因为它会立即创建分支(之后不需要额外的推送命令)。
在Github回购中快速完成此操作的方法如下:
使用Sourcetree | 最简单的方法。
只需运行:
git checkout -b branch-name <commit>
Run Code Online (Sandbox Code Playgroud)
例如 :
git checkout -b import/january-2019 1d0fa4fa9ea961182114b63976482e634a8067b8
Run Code Online (Sandbox Code Playgroud)
checkout
带有参数的命令-b
将创建一个新分支,并将您切换到该分支