使用 Git 从先前的提交分支
如果我有 n
次提交,如何从 n-3
次提交创建分支?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如果我有 n
次提交,如何从 n-3
次提交创建分支?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(23)
使用提交哈希创建分支:
或使用符号引用:
要在创建分支时检出分支,请使用:
Create the branch using a commit hash:
Or by using a symbolic reference:
To checkout the branch while creating it, use:
要在 github.com 上执行此操作:
To do this on github.com:
这个魔法可以通过git重置来实现。
创建一个新分支并切换到它(因此所有最新提交都存储在此处)
切换回您之前的工作分支(假设它是主分支)
删除最新的 x 提交,保持 master 干净
从这一刻起,所有最新的 x 提交都只在新分支中,不再在你之前的工作分支(master)中。
The magic can be done by git reset.
Create a new branch and switch to it (so all of your latest commits are stored here)
Switch back to your previous working branch (assume it's master)
Remove the latest x commits, keep master clean
From this moment on, all the latest x commits are only in the new branch, not in your previous working branch (master) any more.
如果您不确定要提前从哪个提交分支,您可以检查提交并检查其代码(参见源代码、编译、测试),一旦
找到要分支的提交,您就可以在提交中执行此操作(即无需先返回主节点)只需以通常的方式创建一个分支:
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
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:
只需运行:
例如:
带有参数
-b
的checkout
命令将创建一个新分支并且它将将您切换到它。Simply run:
For example:
The
checkout
command with the parameter-b
will create a new branch and it will switch you over to it.这使用一个命令创建分支:与
上面发布的方法相比,我更喜欢这种方式,因为它立即创建分支(之后不需要额外的推送命令)。
This creates the branch with one command:
I prefer this way better than the ones published above, because it creates the branch immediately (does not require an extra push command afterwards).
一个很好的相关问题是:你到底如何使用 Git 的
--help
选项来解决这个问题?让我们试试这个:我们看到这个输出:
Gobbledegook。
在后续文本中搜索单词“commit”。我们发现:
我们已经取得进展了!
现在,把注意力集中在这行晦涩难懂的书上:
将其压缩为:
完成。
A great related question is: How the heck do you figure this out using the
--help
option of Git? Let's try this:We see this output:
Gobbledegook.
Search through the subsequent text for the word "commit". We find this:
We're getting somewhere!
Now, focus on this line of the gobbledegook:
Condense that to this:
And done.
使用Sourcetree |最简单的方法。
并给出一个新的分支名称,然后创建一个分支!
Using Sourcetree | The easiest way.
And give a new branch name then create a branch!
这就是我所做的:
在本例中,
8a75b001096536b3216022484af3026aa9c7bb5b
是属于master
分支的旧提交。This is what I did:
In this case,
8a75b001096536b3216022484af3026aa9c7bb5b
was and old commit belonging to themaster
branch.还没有人提到 git switch 吗?
您可以执行以下操作:
git checkout
或者使用符号引用:
git checkout HEAD~3
然后:
git switch -c my-新功能分支
No one mentioned git switch yet?
You can do:
git checkout <commit-hash>
Or by using a symbolic reference:
git checkout HEAD~3
And then:
git switch -c my-new-feature-branch
在 GitHub 存储库上执行此操作的快速方法如下:
从分支中查找特定提交
在 SHA id 旁边,单击“浏览历史记录中此时的存储库”
在这里您可以从此提交创建一个新分支
A quick way to do it on your GitHub repository would be as followed:
Find the specific commit from your branch
Beside the SHA id, click on 'Browse the repository at this point in the history'
Here you can create a new branch from this commit
要在 Eclipse 中执行此操作:
它将为您创建一个本地分支。然后,每当您推送更改时,您的分支都会被推送到远程服务器。
To do this in Eclipse:
It will create a local branch for you. Then whenever you push your changes, your branch will be pushed to the remote server.
对于 Git GUI 用户,您可以可视化所有历史记录(如有必要),然后右键单击您想要分支的提交并输入分支名称。
For Git GUI users you can visualize all the history (if necessary) and then right click on the commit you wish to branch from and enter the branch name.
你可以在 Stash 中做到这一点。
You can do it in Stash.
我能够这样做:
您必须在其中输入跳过值。 0 是最新的,1 是上一个,2 是之前的提交,等等。
I was able to do it like so:
Where you must enter the skip value. 0 is the latest, 1 is the previous, 2 is the commit before that, etc.
转到 Git 存储库的特定提交
有时,在处理 Git 存储库时,您希望返回到特定提交(修订版)以在以下位置获取项目快照:特定时间。为此,您需要提交的 SHA-1 哈希值,您可以使用以下命令轻松检查日志:
这将为您提供所有提交的紧凑列表以及 SHA-1 哈希值。
现在您知道了要转到的提交的哈希值,您可以使用以下两个命令之一:
或
checkout
git checkout;
告诉 git 将路径的当前状态替换为给定提交中的状态。路径可以是文件或目录。
如果没有给出分支,Git 会假设 HEAD 提交。
如果没有给出路径,Git 会将
HEAD
移动到给定的提交(从而更改您正在处理的提交)。重置
如果您在一个分支上(通常应该在),
HEAD
和该分支将被移动到提交。如果您处于分离的
HEAD
状态,Git 重置只会移动HEAD
。 要重置分支,请先检查它。如果您想了解更多有关 git reset 和 Git checkout 之间的区别,我建议您阅读 官方 Git 博客。
Go to a particular commit of a Git repository
Sometimes when working on a Git repository you want to go back to a specific commit (revision) to have a snapshot of your project at a specific time. To do that all you need it the SHA-1 hash of the commit which you can easily find checking the log with the command:
which will give you a compact list of all the commits and the short version of the SHA-1 hash.
Now that you know the hash of the commit you want to go to you can use one of the following two commands:
or
checkout
git checkout <commit> <paths>
Tells git to replace the current state of paths with their state in the given commit. Paths can be files or directories.
If no branch is given, Git assumes the HEAD commit.
If no path is given, Git moves
HEAD
to the given commit (thereby changing the commit you're sitting and working on).reset
If you are on a branch (you should usually be),
HEAD
and this branch are moved to commit.If you are in detached
HEAD
state, Git reset does only moveHEAD
. To reset a branch, first check it out.If you wanted to know more about the difference between git reset and Git checkout I would recommend to read the official Git blog.
我不知道为什么人们喜欢把事情变得更复杂。这是一个简单的单行
I don't know why people enjoy making things more complicated. Here is a simple one-liner to
使用Sourcetree(当前使用的版本:3.1.3)
打开Sourcetree 中的历史
它将在主窗口中列出所有提交
右键单击所需的提交,然后单击
分支...
选项。在新窗口中为分支命名,然后单击
创建分支
。新分支(系统本地)将与其他现有分支一起出现在左侧,您可以将其推送到源以将其发送到存储库,这样其他用户就可以使用它。
With Sourcetree (version currently used: 3.1.3)
Open the History in Sourcetree
It will list all the commits in the main window
Right-click on the desired commit and click on the
Branch...
option.Give a name for the branch in the new window and click
Create branch
.The new branch (local to your system) will come on the left side along with the other existing branches, which you can push to the origin to get it to the repository, that way it becomes available to other users.
使用 GitHub Desktop,显示历史记录窗格并右键单击所需的提交。然后选择菜单项“从提交创建分支”。
With GitHub Desktop, display the history pane and right click on the commit you want. Then choose the menu item "Create branch from commit".
如果您正在寻找基于命令行的解决方案,您可以忽略我的回答。
我建议你使用 GitKraken。这是一个非凡的 git UI 客户端。它在主页上显示了 Git 树。您只需查看它们即可了解该项目的进展情况。只需选择一个特定的提交,右键单击它并选择“在此处创建分支”选项。它会给你一个文本框来输入分支名称。输入分支名称,选择“确定”即可完成设置。它真的非常容易使用。
If you are looking for a command-line based solution, you can ignore my answer.
I am gonna suggest you to use GitKraken. It's an extraordinary git UI client. It shows the Git tree on the homepage. You can just look at them and know what is going on with the project. Just select a specific commit, right-click on it and select the option 'Create a branch here'. It will give you a text box to enter the branch name. Enter branch name, select 'OK' and you are set. It's really very easy to use.
我使用了 Git Gui (Windows 版 Git 附带)。
I used Git Gui (which comes with Git for Windows).
如果您使用Sourcetree,那就非常简单了。
If you use Sourcetree that is pretty straightforward.