使用 Git 从之前的提交分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2816715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Branch from a previous commit using Git
提问by dole doug
If I have n commits, how can I branch from the n-3 commit?
如果我有 n 个提交,我如何从 n-3 个提交分支?
I can see the hash of every commit.
我可以看到每次提交的哈希值。
回答by CB Bailey
You can create the branch via a hash:
您可以通过哈希创建分支:
git branch branchname <sha1-of-commit>
Or by using a symbolic reference:
或者通过使用符号引用:
git branch branchname HEAD~3
To checkout the branch when creating it, use
要在创建分支时签出分支,请使用
git checkout -b branchname <sha1-of-commit or HEAD~3>
回答by OneSolitaryNoob
To do this on github.com:
要在 github.com 上执行此操作:
- Go to your project.
- Click on the "Commits".
- Click on the <>("Browse the repository at this point in the history") on the commit you want to branch from.
- Click on the "tree: xxxxxx" up in the upper left. Just below the language statistics bar, you'll get the option to "Find or Create Branch" (just type in a new branch name there)
- 转到您的项目。
- 单击“提交”。
- 单击要从中分支的提交上的<>(“在历史记录中的这一点浏览存储库”)。
- 单击左上角的“树:xxxxxx”。在语言统计栏下方,您将获得“查找或创建分支”选项(只需在此处输入新的分支名称)
回答by Jing Li
The magic can be done by git reset.
魔术可以通过git reset来完成。
Create a new branch and switch to it (so all of your latest commits are stored here)
git checkout -b your_new_branch
Switch back to your previous working branch (assume it's master)
git checkout master
Remove the latest x commits, keep master clean
git reset --hard HEAD~x # in your case, x = 3
创建一个新分支并切换到它(所以你所有的最新提交都存储在这里)
git checkout -b your_new_branch
切换回之前的工作分支(假设它是主分支)
git checkout master
删除最新的 x 提交,保持 master 干净
git reset --hard HEAD~x # in your case, x = 3
From this moment on, all the latest x commits are only in the new branch, not in your previous working branch (master) any more.
从这一刻开始,所有最新的 x 提交都只在新分支中,不再在你之前的工作分支(主)中。
回答by stanm
If you are not sure which commit you want to branch from in advance you can check commits out and examine their code (see source, compile, test) by
如果您不确定要提前从哪个提交分支,您可以通过以下方式检查提交并检查它们的代码(参见源代码、编译、测试)
git checkout <sha1-of-commit>
once you find the commit you want to branch from you can do that from within the commit (i.e. without going back to the master first) just by creating a branch in the usual way:
一旦你找到你想要分支的提交,你可以在提交中完成(即无需先回到主节点),只需以通常的方式创建一个分支:
git checkout -b <branch_name>
回答by Tyler Long
git checkout -b <branch-name> <sha1-of-commit>
回答by Vatsal Parekh
回答by d1jhoni1b
Simply run :
只需运行:
git checkout -b branch-name <commit>
For example :
例如 :
git checkout -b import/january-2019 1d0fa4fa9ea961182114b63976482e634a8067b8
The checkout
command with the parameter -b
will create a new branch ANDit will switch you over to it
该checkout
带有参数的命令-b
将创建一个新的分支和它会切换你交给它
回答by PurpleHymanet
A great related question is: How the heck do you figure this out using the --help
option of git? Let's try this:
一个很好的相关问题是:您如何使用--help
git 选项来解决这个问题?让我们试试这个:
git branch --help
We see this output:
我们看到这个输出:
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.
狼吞虎咽。
Search through the subsequent text for the word "commit". We find this:
在随后的文本中搜索“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.
We're getting somewhere!
我们正在到达某个地方!
Now, focus on this line of the gobbledegook:
现在,专注于 gobbledegook 的这一行:
git branch [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
Condense that to this:
将其浓缩为:
git branch <branchname> [<start-point>]
And done.
并做了。
回答by Saurabhcdt
To do this in Eclipse:
要在 Eclipse 中执行此操作:
- Go to "Git Repository Exploring" Perspective.
- Expand "Tags" and choose the commit from which you want to create branch.
- Right click on the commit and choose "Create Branch".
- Provide a branch name.
- 转到“Git 存储库探索”视角。
- 展开“标签”并选择要从中创建分支的提交。
- 右键单击提交并选择“创建分支”。
- 提供一个分支名称。
It will create a local branch for you. Then whenever you push your changes, your branch will be pushed to the remote server.
它将为您创建一个本地分支。然后,无论何时推送更改,您的分支都会被推送到远程服务器。
回答by Mike Graf
I was able to do it like so:
我能够这样做:
git branch new_branch_name `git log -n 1 --skip 3 --format=%H`
Where you must enter the skip value. 0 is the latest, 1 is the previous, 2 is the commit before that, etc.
您必须在其中输入跳过值。0 是最新的,1 是前一个,2 是之前的提交,依此类推。