Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Branch from a previous commit using Git

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

I can see the hash of every commit.

like image 874
dole doug Avatar asked May 12 '10 07:05

dole doug


People also ask

How do you branch from a previous commit?

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!

How do you checkout a branch from a commit?

To pull up a list of your commits and their associated hashes, you can run the git log command. To checkout a previous commit, you will use the Git checkout command followed by the commit hash you retrieved from your Git log.

How do I switch to a previous branch?

Use the git switch - (Or git checkout - ) to switch to the previous branch you were working with. This is pretty similar to the cd - command, which is used to switch to the previous directory.


2 Answers

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> 
like image 86
CB Bailey Avatar answered Sep 29 '22 19:09

CB Bailey


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
like image 27
OneSolitaryNoob Avatar answered Sep 29 '22 20:09

OneSolitaryNoob