Search code examples
gitgit-mergegit-commit

Git manually specify parent commits


To merge two divergent branches (say I'm on master and want to merge topic), sometimes it's easier to manually merge files by inspecting git diff and applying the changes manually (or parts of them automatically using git apply). However, this just changes the working directory files, so my commit will just have current master as a parent commit, rather than appearing as a merge commit between master and topic. Is there a way to manually specify parent commits to git commit?


Solution

  • torek's comment indeed is closest to what I'd prefer:

    git commit-tree -p <parent1> -p <parent2> -m <message> $(git write-tree)
    

    This outputs a new commit hash, that I can use with git reset --hard <hash> to get my desired result.