git create new branch from a specific branch history and commit

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.

  • Rebuild loose missing tree in Git when there are no other missing objects
  • TFS /Source Control: How to manage hotfixes
  • For bitbucket, what is the difference between having a team work on a repo and having a personal repo that everyone on the team works on?
  • Where to place Git repository
  • Does TFS 2013 have shelvesets anymore (when using GIT)?
  • Web-based Git Repository Manager
  • Did a merge occur from a specific branch into master?
  • An error was raised by libgit2. Category = Os (Error)
  • 2 Solutions collect form web for “git create new branch from a specific branch history and commit”

    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
    

    These commands

    git checkout -b branchB <commit_id>
    git checkout -b branchB branchA
    

    mean I want to create a branch X based on branch (or commit) Y, thus you don’t have to be in Y so that works.

    The case were you would need to move to the base branch first is:

    git checkout branchA
    git checkout -b branchB
    

    See that I didn’t mention the base branch in the second line.

    Also, remember that a commit is not specific to a branch, which is simply a pointer and nothing more. A branch can be deleted, move, renamed, etc. But the commits won’t change.

    Git Baby is a git and github fan, let's start git clone.