Branch from a previous commit using Git

GitBranchGit Branch

Git Problem Overview


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

I can see the hash of every commit.

Git Solutions


Solution 1 - Git

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>

Solution 2 - Git

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

Solution 3 - Git

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.

Solution 4 - Git

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>

Solution 5 - Git

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

Solution 6 - Git

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

Solution 7 - Git

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

Solution 8 - Git

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.

Solution 9 - Git

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

Solution 10 - Git

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.

Solution 11 - Git

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

Solution 12 - Git

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.

Solution 13 - Git

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.

Solution 14 - Git

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.

Solution 15 - 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 2 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> //re-sets 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.

Solution 16 - Git

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

Solution 17 - 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.

Solution 18 - Git

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

Solution 19 - Git

if you use source tree that is pretty straight forward.

  • 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'

Solution 20 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondole dougView Question on Stackoverflow
Solution 1 - GitCB BaileyView Answer on Stackoverflow
Solution 2 - GitOneSolitaryNoobView Answer on Stackoverflow
Solution 3 - GitJing LiView Answer on Stackoverflow
Solution 4 - GitstanmView Answer on Stackoverflow
Solution 5 - Gitd1jhoni1bView Answer on Stackoverflow
Solution 6 - GitTyler LiuView Answer on Stackoverflow
Solution 7 - GitAlexander SamoylovView Answer on Stackoverflow
Solution 8 - GitJaime MontoyaView Answer on Stackoverflow
Solution 9 - GitPenggunaView Answer on Stackoverflow
Solution 10 - GitPurplejacketView Answer on Stackoverflow
Solution 11 - GitVatsal ParekhView Answer on Stackoverflow
Solution 12 - GitSaurabhcdtView Answer on Stackoverflow
Solution 13 - GitMike GrafView Answer on Stackoverflow
Solution 14 - GitDavid RuanView Answer on Stackoverflow
Solution 15 - GitL Y E S - C H I O U K HView Answer on Stackoverflow
Solution 16 - GitIvanView Answer on Stackoverflow
Solution 17 - Gitshamiul97View Answer on Stackoverflow
Solution 18 - GitTrispedView Answer on Stackoverflow
Solution 19 - GitIbtisam AsifView Answer on Stackoverflow
Solution 20 - GitjlaurensView Answer on Stackoverflow