如果我有N个提交,我如何从N-3个提交分支?
当前回答
我可以这样做:
git branch new_branch_name `git log -n 1 --skip 3 --format=%H`
必须在其中输入跳过值。0是最新的,1是先前的,2是之前的提交,等等。
其他回答
如果您使用的是非常直接的源代码树。
右键单击需要从中创建新分支的提交单击“分支”在对话框中键入新分支的名称,然后单击“创建分支”
如果您不确定要提前从哪个提交分支,可以检查提交并检查其代码(参见源代码、编译、测试)
git checkout <sha1-of-commit>
一旦找到了要从中分支的提交,就可以在提交中(即,不必先返回到主提交),只需按通常的方式创建一个分支:
git checkout -b <branch_name>
这是我所做的:
C:\Users\[path]\build>git checkout -b responsivenavigation 8a75b001096536b3216022484af3026aa9c7bb5b
Switched to a new branch 'responsivenavigation'
C:\Users\jaimemontoya\[path]\app>git branch
master
* responsivenavigation
在本例中,8a75b001096536b3216022484af3026aa9c7bb5b是属于主分支的旧提交。
一个很好的相关问题是:你怎么用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 -b branch-name <commit>
例如:
git checkout -b import/january-2019 1d0fa4fa9ea961182114b63976482e634a8067b8
带有参数-b的checkout命令将创建一个新分支,并将您切换到该分支
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- 在GitHub上有一个公共回购的私人分支?
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式