Git: An alias to speed up fixing merge conflicts
There you are, minding your own business, rebasing your branch and… surprise! A merge conflict!
Fixing merge conflicts can be a little time consuming. The alias in this post can smooth that process out a bit.
Git calls files with conflicts unmerged, because it couldn’t merge parallel edits. You can see the filenames in the “Unmerged paths” section of
$ git status ... Unmerged paths: (use "git restore --staged <file>..." to unstage) (use "git add <file>..." to mark resolution) both added: drink.txt no changes added to commit (use "git add" and/or "git commit -a")
You can output just the unmerged files with the
--diff-filter option for
$ git diff --name-only --diff-filter U drink.txt
Run this command to add an alias for editing unmerged files:
$ git config --global alias.edit-unmerged '!git diff --name-only --diff-filter U | xargs -r $(git var GIT_EDITOR)'
Git will add this to your
[alias] edit-unmerged = !git diff --name-only --diff-filter U | xargs -r $(git var GIT_EDITOR)
Now, when you have a merge conflict, you can open all the unmerged files with:
$ git edit-unmerged
You can step through each in your text editor, fixing them as necessary. Then, you can add them all with the
-u option (
$ git add -u
…and you’re ready to commit:
$ git status --short M drink.txt $ git rebase --continue [detached HEAD d80c05f] Frappucino 1 file changed, 5 insertions(+) Successfully rebased and updated refs/heads/barstucks.
Unfortunately, I can’t remember where I picked up this alias. When I searched, I found a version in the GitAlias project, which gave me the hint to adjust to use
GIT_EDITOR rather than hardcoding the editor commmand.
The GitAlias project has other aliases for editing files in other states like “staged”, which you might like. But personally, I don’t think I’ve needed them, though I use the above “unmerged” alias all the time.
Make your development more pleasant with Boost Your Django DX.
One summary email a week, no spam, I pinky promise.
- How to rebase stacked Git branches
- Git: How to set up a global ignore file
- Git: How to automatically stash while rebasing or merging