git: create results of another disconnected commit

Let’s say I have git repo with two disconnected root commits, referenced by branches master and new. I’d like to create a commit on master making that branch identical to the new commit. That is, looking like:

* "new" changes <- master
* Initial commit

  • How to apply this mutt sidebar patch?
  • How do I use Notepad++ (or other) with msysgit?
  • EGit Push: How to store the remote repository info
  • JGit FetchCommand
  • Git ignore files with particular extension in a particular folder name
  • How to generate key on git bash
  • such that

    git diff master new

    is empty.

    • I don’t want a merge commit of master and new.
    • I know the exact state I want to end up in, so I shouldn’t need to figure out merge conflicts.


    TLDR answer

    git read-tree --reset -u new
    git commit -m "new commit"

    For more info, see the accepted answer, and even more, see Edmundo’s answer and comments therein.

  • Clearcase : Migrate from Git to ClearCase
  • Differences between git fetch and git fetch origin master
  • Why is Git not prompting me for password?
  • git doesn't upload files in the view/*.jade folder in express.js
  • Getting Exception while trying to creating branch using GIT
  • How to input password to git pull command?
  • 3 Solutions collect form web for “git: create results of another disconnected commit”

    Edmundo’s answer is one of the shortest ways to do this. Another shortest way to do it is, while on branch master, to use git read-tree --reset -u new followed by git commit to make a new commit. Note that git read-tree --reset -u will wreck any in-progress work (a la git reset --hard) so be sure that you are not in the middle of something first.

    Unfortunately all short and easy ways involve using some of the not-really-meant-for-humans “plumbing” commands like git commit-tree or git read-tree. The main advantage to git read-tree over git commit-tree is that the latter is a more strict bondage-and-discipline type command, requiring that you specify the tree hash ID directly (e.g., new^{tree}) rather than just a commit ID that resolves to a tree. See the gitrevisions documentation for why new^{tree} works here. (And, of course, you must also provide the hash ID of the master commit, though just master works there.)

    The longer and uglier way that uses only “normal” commands is git rm -rf . followed by git checkout new -- . followed by git commit: basically “remove everything” then “extract all from new” then commit. This must be run at the top of the work-tree.

    If you want a single commit to be created on top of master that makes the working tree be the way it is at new, you can do this: get the object ID for the tree on new (git cat-file -p new). Once you have the id of the tree, create a new revision using master as the parent with commit-tree:

    git commit-tree -p master -m "Here's a new revision" <id-of-tree>

    This will give you the ID of the new revision. You can then checkout master and reset –hard to the revision or create a new branch on it.

    git checkout new
    git rebase master
    git checkout master
    git merge new

    If you want your master state to be identical to new, the quickest thing to do is just:

    git checkout master
    git reset --hard new
    Git Baby is a git and github fan, let's start git clone.