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.

  • Undo local changes in Git
  • Is there an internal alternative to gist.github.com to run behind a firewall?
  • Invalid channel 117 - Eclipse / EGit
  • How to remove files that are listed in the .gitignore but still on the repository?
  • How to diff a commit with its parent?
  • Git with ldap authorization
  • 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.

  • Developing an npm package and a project that depends on it at the same time
  • Merging ActiveRecord migrations out of order
  • Customize display of git commit message display to create link to issue tracker
  • How do I make git post-receive emails be sent from the commit author?
  • Are the Linux kernel's stable git branches merged back into master?
  • git push taking ages
  • 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.