使用 Git 从先前的提交分支

发布于 2024-09-01 01:19:31 字数 60 浏览 4 评论 0 原文

如果我有 n 次提交,如何从 n-3 次提交创建分支?

If I have an n number of commits, how can I create a branch from the n-3 commit?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(23

傾旎 2024-09-08 01:19:31

使用提交哈希创建分支:

git branch branch_name <commit-hash>

或使用符号引用:

git branch branch_name HEAD~3

要在创建分支时检出分支,请使用:

git checkout -b branch_name <commit-hash or HEAD~3>

Create the branch using a commit hash:

git branch branch_name <commit-hash>

Or by using a symbolic reference:

git branch branch_name HEAD~3

To checkout the branch while creating it, use:

git checkout -b branch_name <commit-hash or HEAD~3>
优雅的叶子 2024-09-08 01:19:31

要在 github.com 上执行此操作:

  1. 转到您的项目。
  2. 单击“提交”。
  3. 单击要从中分支的提交上的 <>(“浏览历史记录中此时的存储库”)。
  4. 单击左上角的“树:xxxxxx”。在语言统计栏下方,您可以选择“查找或创建分支”(只需在此处输入新的分支名称)“上一次提交的分支”

To do this on github.com:

  1. Go to your project.
  2. Click on the "Commits".
  3. Click on the <> ("Browse the repository at this point in the history") on the commit you want to branch from.
  4. 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) Branch from previous commit
余罪 2024-09-08 01:19:31

这个魔法可以通过git重置来实现。

  1. 创建一个新分支并切换到它(因此所有最新提交都存储在此处)

    git checkout -b your_new_branch
    
  2. 切换回您之前的工作分支(假设它是主分支)

    git checkout master
    
  3. 删除最新的 x 提交,保持 master 干净

    git reset --hard HEAD~x # 在你的情况下,x = 3
    

从这一刻起,所有最新的 x 提交都只在新分支中,不再在你之前的工作分支(master)中。

The magic can be done by git reset.

  1. Create a new branch and switch to it (so all of your latest commits are stored here)

    git checkout -b your_new_branch
    
  2. Switch back to your previous working branch (assume it's master)

    git checkout master
    
  3. Remove the latest x commits, keep master clean

    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.

记忆消瘦 2024-09-08 01:19:31

如果您不确定要提前从哪个提交分支,您可以检查提交并检查其代码(参见源代码、编译、测试),一旦

git checkout <sha1-of-commit>

找到要分支的提交,您就可以在提交中执行此操作(即无需先返回主节点)只需以通常的方式创建一个分支:

git checkout -b <branch_name>

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>
诠释孤独 2024-09-08 01:19:31

只需运行:

git checkout -b branch-name <commit>

例如:

git checkout -b import/january-2019 1d0fa4fa9ea961182114b63976482e634a8067b8

带有参数-bcheckout命令将创建一个新分支并且它将将您切换到它。

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 and it will switch you over to it.

浊酒尽余欢 2024-09-08 01:19:31
git checkout -b <branch-name> <sha1-of-commit>
git checkout -b <branch-name> <sha1-of-commit>
蓦然回首 2024-09-08 01:19:31

这使用一个命令创建分支:与

git push origin <sha1-of-commit>:refs/heads/<branch-name>

上面发布的方法相比,我更喜欢这种方式,因为它立即创建分支(之后不需要额外的推送命令)。

This creates the branch with one command:

git push origin <sha1-of-commit>:refs/heads/<branch-name>

I prefer this way better than the ones published above, because it creates the branch immediately (does not require an extra push command afterwards).

陈独秀 2024-09-08 01:19:31

一个很好的相关问题是:你到底如何使用 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>]

完成。

A great related question is: How the heck do you figure this out using the --help option of Git? Let's try this:

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:

   <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:

git branch [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]

Condense that to this:

git branch <branchname> [<start-point>]

And done.

荭秂 2024-09-08 01:19:31

使用Sourcetree |最简单的方法。

  • 首先,签出您想要进行特定提交以创建新分支的分支。
  • 然后看工具栏,选择Repository>分支...快捷键是 Command + Shift + B。
  • 然后选择您想要进行的特定提交。
    并给出一个新的分支名称,然后创建一个分支!

输入图像描述这里

Using Sourcetree | The easiest way.

  • First, checkout the branch that you want to take the specific commit to make a new branch.
  • Then look at the toolbar, select Repository > Branch ... the shortcut is Command + Shift + B.
  • And select the specific commit you want to take.
    And give a new branch name then create a branch!

enter image description here

黯然#的苍凉 2024-09-08 01:19:31

这就是我所做的:

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 是属于 master 分支的旧提交。

This is what I did:

C:\Users\[path]\build>git checkout -b responsivenavigation 8a75b001096536b3216022484af3026aa9c7bb5b
Switched to a new branch 'responsivenavigation'

C:\Users\jaimemontoya\[path]\app>git branch
  master
* responsivenavigation

In this case, 8a75b001096536b3216022484af3026aa9c7bb5b was and old commit belonging to the master branch.

怀里藏娇 2024-09-08 01:19:31

还没有人提到 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

月竹挽风 2024-09-08 01:19:31

在 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

    enter image description here

愿与i 2024-09-08 01:19:31

要在 Eclipse 中执行此操作:

  • 转至“Git Repository Exploring”透视图。
  • 展开“标签”并选择要从中创建分支的提交。
  • 右键单击提交并选择“创建分支”。
  • 提供分支名称。

它将为您创建一个本地分支。然后,每当您推送更改时,您的分支都会被推送到远程服务器。

To do this in 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.

It will create a local branch for you. Then whenever you push your changes, your branch will be pushed to the remote server.

空城之時有危險 2024-09-08 01:19:31

选择提交

对于 Git GUI 用户,您可以可视化所有历史记录(如有必要),然后右键单击您想要分支的提交并输入分支名称。

输入分支名称

可视化所有历史

Select Commit

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.

Enter Branch name

Visualize all the history

月竹挽风 2024-09-08 01:19:31

你可以在 Stash 中做到这一点。

  1. 提交
  2. 单击屏幕右上角的
  3. ,单击“标记此提交”然后您可以从刚刚创建的标记创建新分支。

You can do it in Stash.

  1. Click the commit
  2. On the right top of the screen click "Tag this commit"
  3. Then you can create the new branch from the tag you just created.
追星践月 2024-09-08 01:19:31

我能够这样做:

git branch new_branch_name `git log -n 1 --skip 3 --format=%H`

您必须在其中输入跳过值。 0 是最新的,1 是上一个,2 是之前的提交,等等。

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.

稀香 2024-09-08 01:19:31

转到 Git 存储库的特定提交

有时,在处理 Git 存储库时,您希望返回到特定提交(修订版)以在以下位置获取项目快照:特定时间。为此,您需要提交的 SHA-1 哈希值,您可以使用以下命令轻松检查日志:

git log --abbrev-commit --pretty=oneline

这将为您提供所有提交的紧凑列表以及 SHA-1 哈希值。

现在您知道了要转到的提交的哈希值,您可以使用以下两个命令之一:

git checkout HASH

git reset --hard HASH

checkout

git checkout ;

告诉 git 将路径的当前状态替换为给定提交中的状态。路径可以是文件或目录。

如果没有给出分支,Git 会假设 HEAD 提交。

git checkout <path> // Restores <path> from your last commit. It is a 'filesystem-undo'.

如果没有给出路径,Git 会将 HEAD 移动到给定的提交(从而更改您正在处理的提交)。

git checkout branch // Means switching branches.

重置

git reset <commit> // Resets the current pointer to the given commit.

如果您在一个分支上(通常应该在),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:

git log --abbrev-commit --pretty=oneline

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:

git checkout HASH

or

git reset --hard HASH

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.

git checkout <path> // Restores <path> from your last commit. It is a 'filesystem-undo'.

If no path is given, Git moves HEAD to the given commit (thereby changing the commit you're sitting and working on).

git checkout branch // Means switching branches.

reset

git reset <commit> // Resets the current pointer to the given commit.

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 move HEAD. 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.

未蓝澄海的烟 2024-09-08 01:19:31

我不知道为什么人们喜欢把事情变得更复杂。这是一个简单的单行

git checkout <commit_id>        # Gives a snapshot of the repository at this commit
git switch -c <new_branch_name> # Creates a new branch from that commit

I don't know why people enjoy making things more complicated. Here is a simple one-liner to

git checkout <commit_id>        # Gives a snapshot of the repository at this commit
git switch -c <new_branch_name> # Creates a new branch from that commit
み青杉依旧 2024-09-08 01:19:31

使用Sourcetree(当前使用的版本:3.1.3)

  1. 打开Sourcetree 中的历史

    “在

  2. 它将在主窗口中列出所有提交

  3. 右键单击所需的提交,然后单击分支...选项。

  4. 在新窗口中为分支命名,然后单击创建分支

  5. 新分支(系统本地)将与其他现有分支一起出现在左侧,您可以将其推送到源以将其发送到存储库,这样其他用户就可以使用它。

With Sourcetree (version currently used: 3.1.3)

  1. Open the History in Sourcetree

    Find history in Sourcetree

  2. It will list all the commits in the main window

  3. Right-click on the desired commit and click on the Branch... option.

  4. Give a name for the branch in the new window and click Create branch.

  5. 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.

柏林苍穹下 2024-09-08 01:19:31

使用 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".

北城半夏 2024-09-08 01:19:31

如果您正在寻找基于命令行的解决方案,您可以忽略我的回答。
我建议你使用 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.

山有枢 2024-09-08 01:19:31

我使用了 Git Gui (Windows 版 Git 附带)。

  1. 在菜单栏上,打开分支菜单,然后选择创建...
  2. 分支名称部分中指定新分支的名称
  3. 选择 < strong>修订表达式:在开始修订部分并输入提交 ID(我刚刚使用了 Visual Studio 中的 8 位 ID,它起作用了)

创建新分支对话框窗口

I used Git Gui (which comes with Git for Windows).

  1. On the menu bar, open the Branch menu and select Create...
  2. Specify the name for the new branch in the Branch Name section
  3. Select Revision Expression: in the Start Revision section and enter the commit ID (I just used the 8 digit ID from Visual Studio and it worked)

Create New Branch dialog window

陌路终见情 2024-09-08 01:19:31

如果您使用Sourcetree,那就非常简单了。

  • 右键单击需要创建新分支的提交
  • 单击“分支”
  • 在出现的对话框中输入新分支的名称,然后单击“创建分支”

If you use Sourcetree that is pretty straightforward.

  • Right click the commit from where you need to create a new branch
  • Click on 'branch'
  • Type name of new branch in the dialog appeared and click 'create branch'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文