Reputation: 3678
I have made several commits in the master branch and then merged them to the dev branch.
I want to create a branch from a specific commit in the dev branch, which was first committed in the master branch.
I used the commands:
git checkout dev
git branch <branch name> <commit id>
However, this creates the branch from the master branch, not the dev branch I expected. The commit id is same in the master branch and dev branch. So, how can I distinguish same commit id in a different branch?
PS: I made an example at GitHub.
I used the commands:
git checkout dev
git branch test 07aeec983bfc17c25f0b0a7c1d47da8e35df7af8
I expect that the test branch contains aa.txt, bb.txt, and cc.txt. However, the test branch only contains aa.txt and cc.txt. It most likely created the branch from the master branch.
Upvotes: 174
Views: 233807
Reputation: 23756
You can do this locally as everyone mentioned using
git checkout -b <branch-name> <sha1-of-commit>
Alternatively, you can do this in GitHub itself, follow the steps:
In the repository, click on the Commits.
on the commit you want to branch from, click on <> to browse the repository at this point in the history.
Click on the Tree: xxxxxx in the upper left. Just type in a new branch name there. Click Create branch xxx as shown below.
Now you can fetch the changes from that branch locally and continue from there.
Upvotes: 53
Reputation: 41945
If you are using this form of the branch
command (with start point), it does not matter where your HEAD
is.
What you are doing:
git checkout dev
git branch test 07aeec983bfc17c25f0b0a7c1d47da8e35df7af8
First, you set your HEAD
to the branch dev
,
Second, you start a new branch on commit 07aeec98
. There is no bb.txt at this commit (according to your github repo).
If you want to start a new branch at the location you have just checked out, you can either run branch with no start point:
git branch test
or as other have answered, branch and checkout there in one operation:
git checkout -b test
I think that you might be confused by that fact that 07aeec98
is part of the branch dev
. It is true that this commit is an ancestor of dev
, its changes are needed to reach the latest commit in dev
. However, they are other commits that are needed to reach the latest dev
, and these are not necessarily in the history of 07aeec98
.
8480e8ae
(where you added bb.txt) is for example not in the history of 07aeec98
. If you branch from 07aeec98
, you won't get the changes introduced by 8480e8ae
.
In other words: if you merge branch A and branch B into branch C, then create a new branch on a commit of A, you won't get the changes introduced in B.
Same here, you had two parallel branches master and dev, which you merged in dev. Branching out from a commit of master (older than the merge) won't provide you with the changes of dev.
If you want to permanently integrate new changes from master into your feature branches, you should merge master
into them and go on. This will create merge commits in your feature branches, though.
If you have not published your feature branches, you can also rebase them on the updated master: git rebase master featureA
. Be prepared to solve possible conflicts.
If you want a workflow where you can work on feature branches free of merge commits and still integrate with newer changes in master, I recommend the following:
dev
branch on a commit of masterdev
.Do not commit into dev
directly, use it only for merging other branches.
For example, if you are working on feature A and B:
a---b---c---d---e---f---g -master
\ \
\ \-x -featureB
\
\-j---k -featureA
Merge branches into a dev
branch to check if they work well with the new master:
a---b---c---d---e---f---g -master
\ \ \
\ \ \--x'---k' -dev
\ \ / /
\ \-x---------- / -featureB
\ /
\-j---k--------------- -featureA
You can continue working on your feature branches, and keep merging in new changes from both master and feature branches into dev
regularly.
a---b---c---d---e---f---g---h---i----- -master
\ \ \ \
\ \ \--x'---k'---i'---l' -dev
\ \ / / /
\ \-x---------- / / -featureB
\ / /
\-j---k-----------------l------ -featureA
When it is time to integrate the new features, merge the feature branches (not dev
!) into master.
Upvotes: 208
Reputation: 301037
You have to do:
git branch <branch_name> <commit>
(you were interchanging the branch name and commit)
Or you can do:
git checkout -b <branch_name> <commit>
If in place of you use branch name, you get a branch out of tip of the branch.
Upvotes: 11
Reputation: 496742
You have the arguments in the wrong order:
git branch <branch-name> <commit>
and for that, it doesn't matter what branch is checked out; it'll do what you say. (If you omit the commit argument, it defaults to creating a branch at the same place as the current one.)
If you want to check out the new branch as you create it:
git checkout -b <branch> <commit>
with the same behavior if you omit the commit argument.
Upvotes: 80
Reputation: 681
Try
git checkout <commit hash>
git checkout -b new_branch
The commit should only exist once in your tree, not in two separate branches.
This allows you to check out that specific commit and name it what you will.
Upvotes: 13