branch git git-branch

Branch from a previous commit using Git


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


    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>


    • 52

      Git 1.8.2 let me use the short sha1 for the first form.

      Apr 9, 2013 at 20:52

    • 63

      @MattFenwick Git will allow you to use shortened hashes everywhere a hash is allowed, as long as the shortened hash is ”unique” in the repository. So if it didn’t work, try adding another character from the hash.

      – poke

      May 17, 2013 at 12:08

    • 37

      To push the new branch correctly to server.. needed this last step: git push origin BRANCH_NAME

      – Gene Bo

      Oct 15, 2015 at 0:04

    • 4

      to start a branch from <sha1-of-commit> run git checkout -b <name-of-branch> <sha1-of-commit> but if the branch already exists git checkout -B <name-of-branch> <sha1-of-commit>

      – mostafazh

      Sep 18, 2017 at 18:17

    • Also: git branch branchname HEAD^^^

      Jul 31, 2020 at 17:11


    To do this on

    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


    • 98

      Despite the fact that this is Github not git, it was still immensely helpful!

      – Liz

      Jan 25, 2019 at 19:23

    • Unfortunately it still shows changes from other commits, which I wanted to avoid, which is why I searched for this question

      – Maxim

      Oct 1, 2019 at 18:03


    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.


    • 8

      This is what I was looking for since it removes the commits from the Master and makes it as though you had remembered to make the branch before those commits were made. Thanks.

      – superbeck

      Aug 1, 2016 at 20:46

    • 10

      Just don’t forget that a git reset --hard is not a good idea if you already have pushed the commit to origin…

      – LuisF

      May 31, 2017 at 9:09

    • 1

      you can git push --force if you had already pushed the branch before

      – milan

      Jan 29, 2019 at 14:46

    • 1

      But be really careful when using –force

      – peater

      Sep 6, 2019 at 18:57

    • 4

      I don’t understand the logic of this answer. The poster wants to create a new branch with code from the previous commit. This seems to create a branch from the current master, and then revert master to a previous commit. Am I wrong?

      – andrea m.

      Dec 3, 2021 at 8:42