Search code examples
gitgit-mergemerge-conflict-resolution

git add - flag to add 'both modified:' files only


Let me present a scenario - I'm working on a repo, under my_branch branch, and I need to merge changes from other_branch to my branch.

I ran the command: git merge origin/feature/other_branch

Here is a sample git status after running this command:

$ git status
On branch feature/my_branch
Your branch is up-to-date with 'origin/feature/my_branch'.
You have unmerged paths.
  (fix conflicts and run "git commit")

Changes to be committed:

        modified:   file1
        modified:   file2

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   file3
        both modified:   file4

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   file5
        modified:   file6        

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        file7
        file8

As you can see, changes to file1 and file2 have been accepted from other_branch and have been staged for commit.

I've encountered a merge-conflict in file3 and file4, which I've fixed manually on my editor.

I've made some changes to file5 and file6, but I'm not willing to commit those yet...

I've added file7 and file8 to the workspace, but I'm not willing to commit those either.

To add the conflicted files to stage, I need to run:

git add file1
git add file2

Is there some flag in git add, using which I can only the files in both modified state, whilst ignoring modified and unstaged files ?

Thanks


Solution

  • git add itself has no such flag.

    But it can take as parameters a list of files.
    And you can list unmerged files:

    git diff --name-only --diff-filter=U
    

    So, something like this could work (using this to bring all files in one line):

    git add $(git diff --name-only --diff-filter=U | xargs)