Branch from a previous commit using Git

ID : 386

viewed : 49

Tags : gitbranchgit-branchgit

Top 5 Answer for Branch from a previous commit using Git

vote vote

95

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> 
vote vote

88

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
vote vote

71

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.

vote vote

61

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> 
vote vote

54

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

Top 3 video Explaining Branch from a previous commit using Git

Related QUESTION?