Search code examples
gitgit-mergegit-commit

Commit only part of resolved files?


When I'm trying to merge two branches and I get lots of conflicts. What I want to achieve to split merge process into several steps.

Is there a possibility to resolve some files in git and commit them on my branch?


Solution

  • One thing you can do is to rebase instead of merge... that forces you to fix conflicts for every single commit. Let's say you have branch master and branch feature and feature is not pushed, but you want to merge stuff from master back into feature.

    git checkout feature
    git rebase master
    

    This rewinds all your changes since the feature branch diverged from master, moves the branch point up to the tip of master, then replays your changes on top. Note that this can be dangerous if you have already pushed feature to your remote, because it may rewrite already pushed history... you should know this beforehand. Also see http://www.vogella.com/tutorials/Git/article.html#rebase_branches

    But what you probably want to do is merge the whole thing, but have multiple commits instead of one monolithic merge commit. Well, this is sort of possible, but you can only have one merge commit. What you can do is:

    git merge feature
    git mergetool
    

    Then fix ALL conflicts and save and exit the editor. Now all the stuff is staged, but not committed. Now you can use e.g. git gui to unstage changes from the merge commit (select the lines and right-click on the selection, then choose 'Unstage Lines From Commit'), commit the preliminary merge commit and then re-stage and commit the rest step by step. You will still have only one merge commit, but several follow-up commits.

    I still think the idea is dangerous, because I doubt that those commits in-between are good commits (high change of e.g. being non-compilable).