Clean solution
The following solution seems to be much cleaner to me and it's also suggested by the Git itself — try to execute git status
in the repository with a conflict:
Unmerged paths:
(use "git restore --staged <file>..." to unstage)
(use "git add <file>..." to mark resolution)
Note: The restore
command has been introduced in Git version 2.23.0. Older versions of Git suggested to use the command git reset HEAD <file>...
instead of git restore --staged <file>...
. You could also use git reset
to unstage any and all files in the staging area (called the index). Restore command's equivalent is git restore --staged .
(the dot is necessary and it specifies any file). Currently, any of those commands may be used and the outcome is the same. If you want to learn about the differences between those commands, check the documentation.
So let's do what Git suggests (without making and reverting any pointless commits):
- Manually (or ideally using some merge tool, see below) resolve the conflict(s).
- Use
git restore --staged .
to mark conflict(s) as resolved and unstage all files in the staging area. If you want to unstage only specific files, use the command git restore --staged <file>
instead. You don't have to execute git add
before.
- Finally, remove the stash with
git stash drop
, because Git doesn't do that automatically on conflict.
Translated to the command-line commands:
$ git stash pop
# ...resolve conflict(s)
$ git restore --staged .
$ git stash drop
Explanation of the default behavior
There are two ways of marking conflicts as resolved: git add
and git restore --staged <file>...
. While git restore --staged <file>...
marks the conflicts as resolved and removes files from the index, git add
also marks the conflicts as resolved, but keeps files in the index.
Adding files to the index after a conflict is resolved is on purpose. This way you can differentiate the changes from the previous stash and changes you made after the conflict was resolved. If you don't like it, you can always use git restore --staged .
to remove everything from the index.
Merge tools
I highly recommend using any of 3-way merge tools for resolving conflicts, e.g. KDiff3, Meld, etc., instead of doing it manually. It usually solves all or the majority of conflicts automatically itself. It's huge time-saver!
git add
your resolved conflict files, effectively staging them in the index, and you'd want to not have them in our index?git stash pop
has when no conflict occurs (but with notification which files need to be merged).