Branch from a previous commit using Git

2019-01-01 14:12发布

If I have n commits, how can I branch from the n-3 commit?

I can see the hash of every commit.

13条回答
低头抚发
2楼-- · 2019-01-01 14:17

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
查看更多
泪湿衣
3楼-- · 2019-01-01 14:17

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.

查看更多
明月照影归
4楼-- · 2019-01-01 14:27

This is what I did:

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

C:\Users\jaimemontoya\Dropbox\CuponClub\androidapp\build>git branch
  master
* responsivenavigation

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

查看更多
与风俱净
5楼-- · 2019-01-01 14:28

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>
查看更多
看风景的人
6楼-- · 2019-01-01 14:29

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.

查看更多
裙下三千臣
7楼-- · 2019-01-01 14:31

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 enter image description here
查看更多
登录 后发表回答