简体   繁体   中英

Branch from a previous commit using Git

If I have N commits, how do I branch from the N-3 commit?

You can create the branch via a hash:

git branch branchname <sha1-of-commit>

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)从先前的提交分支

The magic can be done by git reset<\/strong> .

  1. "

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>

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

git checkout -b <branch-name> <sha1-of-commit>

This creates the branch with one command:

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

A great related question is: How the heck do you figure this out using the --help<\/code> option of git? Let's try this:

git branch --help

A quick way to do it on your Github repo would be as followed:

  • Find the specific commit from your branch
  • Beside the SHA id, click on 'Browse the repo at this point in the history'
  • Here you can create a new branch from this commit在此处输入图像描述

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!

在此处输入图像描述

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

To do this in Eclipse:

  • <\/li>
  • <\/li>
  • <\/li>
  • <\/li><\/ul>

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

    "

I was able to do it like so:

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

You can do it in Stash.

  1. <\/li>
  2. <\/li>
  3. <\/li><\/ol>"

Go to a particular commit of a git repository<\/em><\/strong>

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

To do the accepted answer in Visual Studio 2015 & 2017:

点击更改

Click in alterations (red arrow above)

单击操作以查看历史记录

Click in Actions (red arrow above) and click in View History on the DropDown Menu

And new Tab will open:

历史记录标签

And you should right click the previous commit that you want your code to revert to: 右键单击上一次提交

Choose to checkout a new branch and voilá!

Below, although not part of OP question, but I do both a lot and this one is a trick step, at least to me: if you want to revert to a previous commit, without checkout a new branch, DO NOT choose revert(!?); you should choose redefine --mixed or --hard:

右键单击上一个提交并重新定义

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)

创建新分支对话框窗口

With GitHub Desktop, display the history pane and right click on the commit you want, then choose the menu item "Create branch from commit".

With Source Tree [Version currently used: 3.1.3]

  1. Open the History in Source Tree

在源代码树中查找历史记录

  1. It will list all the commits in the main window
  2. Right-click on the desired commit and click on the Branch... option.
  3. Give a name for the branch in the new window and click Create branch .
  4. 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.

if you use source tree that is pretty straight forward.

  • <\/li>
  • <\/li>
  • <\/li><\/ul>"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM