Dana
Dana

Reputation: 676

Git command to only stage files without conflict markers?

Bazaar has a convenient bzr resolve command that will recursively mark conflicted files as resolved, but only if their conflict markers have been removed. Is there an equivalent command or script for git?

My repository contains minified Javascript (I'm aware that it's not ideal to track compiled files) that often conflicts after a merge, even if the un-minified source merges cleanly. It's easily fixed by re-minifying and resolving, and doesn't require any manual conflict resolution. My team is migrating from Bazaar to Git and we've come to rely on bzr resolve's smart handling of files with conflict markers: it's safe to blindly run it on the project root after minification. In the future I'd like to automate this process and get the compiled code out of the repository completely, but for now I'm looking for a way to mirror our current process in Git.

I'm aware that git diff --check will report leftover conflict markers, but it's still extra effort to run it and inspect its output before running git add. And running a pre-commit hook that looks for conflict markers isn't ideal unless the hook can also un-stage the offending files and mark them as unresolved (which I haven't discovered how to do). Are there any better options?

Upvotes: 4

Views: 740

Answers (3)

Rafał Rawicki
Rafał Rawicki

Reputation: 22690

When resolving this type of conflicts you can use one of these commands:

git checkout --theirs <filename>

(or ours instead of theirs if you prefer local changes) to select the correct version, or even do that automatically for the whole merge with:

git merge -s recursive -Xtheirs

(or respectively -Xours)

This is an answer, I've found in the question: git merge recursive theirs, how does it work?

Upvotes: 0

ralphtheninja
ralphtheninja

Reputation: 133138

When you get conflicts in git during merge or rebase, the files with no conflicts will automatically be staged, it's only files with conflicts that you need to worry about, i.e. solve the conflicts and stage them manually. There is no need to resolve files with no conflict markers, they are already resolved.

Upvotes: 0

Lily Ballard
Lily Ballard

Reputation: 185811

You should try git mergetool. It re-runs the merge, but it does so interactively, with your merge tool of choice, and automatically stages the file when you're done.

Upvotes: 1

Related Questions