git create new branch from a specific branch history and commit

git, version-control

Solution

Just

git checkout -b branchB <commit>

The hash of the commit is unique within the whole repository, thus there is no reason to change to `branchA` before, because the result is completely the same.

Also this is just a shortcut for

git checkout <commit>
git branch branchB
git checkout branchB

As you can see the first command is a normal checkout, that will supersede your `git checkout branchA` anyway.

I was thinking of simply doing `git checkout -b branchB <commit_id>` but if I'm in branchC that probably won't work cause the log/history of branchC might not be the same as branchA.

There is only one history in one repository.

My intension for this specific situation would be to quickly revert to a previous commit in case a bug was introduced to production. I however still want to keep my new code and then discard of the new branch one the old code was pushed back to the server.

Sounds like tags ;)

git tag -a -m "Release 1.2.3" v1.2.3
# Deploy 1.2.3
#something broken!
git checkout v1.2.2
# Deploy 1.2.2

Problem

I basically want to do: ``` git checkout branchA git checkout -b branchB <commit_id> ``` which creates the new `branchB` branch from `<commit_id>` on `branchA`. Question: I'm getting into details here but the reason I'm asking is to help understand how git history works and to save a bit of typing. (the command above could of been four commands instead of two...) How do I get it to one with native git functions? Question: Is there an easier/one line way of doing the same as above? I was thinking of simply doing `git checkout -b branchB <commit_id>` but if I'm in `branchC` that probably won't work cause the log/history of `branchC` might not be the same as `branchA`. Background: My intension for this specific situation would be to quickly revert to a previous commit in case a bug was introduced to production. I however still want to keep my new code and then discard of the new branch one the old code was pushed back to the server.

Original source