Branch from a previous commit using Git Branch from a previous commit using Git git git

Branch from a previous commit using 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>


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


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.