Reputation: 36108
If I have an n
number of commits, how can I create a branch from the n-3
commit?
Upvotes: 2701
Views: 1410364
Reputation: 587
With GitHub Desktop, display the history pane and right click on the commit you want. Then choose the menu item "Create branch from commit".
Upvotes: 1
Reputation: 6005
I used Git Gui (which comes with Git for Windows).
Upvotes: -1
Reputation: 5090
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 two 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> // Resets 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.
Upvotes: 4
Reputation: 264
A quick way to do it on your GitHub repository would be as followed:
Find the specific commit from your branch
Beside the SHA id, click on 'Browse the repository at this point in the history'
Here you can create a new branch from this commit
Upvotes: 11
Reputation: 2148
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.
Upvotes: 21
Reputation: 8065
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.
Upvotes: 90
Reputation: 75
If you use Sourcetree that is pretty straightforward.
Upvotes: -2
Reputation: 2405
Open the History in Sourcetree
It will list all the commits in the main window
Right-click on the desired commit and click on the Branch...
option.
Give a name for the branch in the new window and click Create branch
.
The new branch (local to your system) will come on the left side along with the other existing branches, which you can push to the origin to get it to the repository, that way it becomes available to other users.
Upvotes: 2
Reputation: 309
I don't know why people enjoy making things more complicated. Here is a simple one-liner to
git checkout <commit_id> # Gives a snapshot of the repository at this commit
git switch -c <new_branch_name> # Creates a new branch from that commit
Upvotes: 2
Reputation: 441
No one mentioned git switch yet?
You can do:
git checkout <commit-hash>
Or by using a symbolic reference:
git checkout HEAD~3
And then:
git switch -c my-new-feature-branch
Upvotes: 14
Reputation: 793119
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>
Upvotes: 3849
Reputation: 15116
The magic can be done by git reset.
Create a new branch and switch to it (so all of your latest commits are stored here)
git checkout -b your_new_branch
Switch back to your previous working branch (assume it's master)
git checkout master
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.
Upvotes: 139
Reputation: 7711
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.
Upvotes: 16
Reputation: 4951
Using Sourcetree | The easiest way.
Upvotes: 16
Reputation: 323
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.
Upvotes: 1
Reputation: 2578
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).
Upvotes: 30
Reputation: 4783
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.
Upvotes: 4
Reputation: 5757
To do this on github.com:
Upvotes: 377
Reputation: 1168
To do this in Eclipse:
It will create a local branch for you. Then whenever you push your changes, your branch will be pushed to the remote server.
Upvotes: 4
Reputation: 5317
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.
Upvotes: 3
Reputation: 794
You can do it in Stash.
Upvotes: 3
Reputation: 3321
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>
Upvotes: 99