Giter Site home page Giter Site logo

jakub-g / git-resolve-conflict Goto Github PK

View Code? Open in Web Editor NEW
50.0 3.0 11.0 32 KB

:heavy_dollar_sign: :heavy_plus_sign: :heavy_minus_sign: :white_check_mark: Resolve merge conflict from command line, in one file, using given strategy (--ours, --theirs, --union)

License: MIT License

Shell 100.00%
git merge-conflicts conflict-resolution git-mergetools resolve-conflicts conflicting-changes

git-resolve-conflict's Introduction

git-resolve-conflict <strategy> <filename>

Resolve merge conflict in just one file, using given strategy (--ours, --theirs, --union)

git resolve-conflict --ours package.json
git resolve-conflict --theirs package.json
git resolve-conflict --union package.json

git resolve-ours package.json
git resolve-theirs package.json
git resolve-union package.json

Why would you need it

To be able to resolve certain well-defined types of merge conflicts, without opening mergetool.

This is particularly useful in automated merge scripts (for example, Jenkins jobs), or if you have large number of well-defined merges to resolve.

Things to be aware of

Note though, this is just a dumb text-based merge resolution script; if you're unlucky, the merged file might be syntactically incorrect.

For example: when using --ours strategy on package.json where both sides added an entry at the end of dependencies or scripts array, the result will be two blocks added without trailing comma between them (hence invalid JSON).

Installation

  • copy /lib/git-resolve-conflict.sh to your .bashrc (this adds just git resolve-conflict)
  • or npm install -g git-resolve-conflict (this also adds 3 other helpers)

Get it on npm

npm installation is the recommended way, so that you can easily get updates in the future.

FAQ

  • Q: Why no updates in 3 years?

  • A: Because it works™. It's feature-complete.

  • Q: Does it work on Windows?

  • A: Yep, but it'a shell script, so you need a unix-y shell. I'm using it with git bash and it works well.

  • Q: Why you distribute through npm?

  • A: Just to make it easier to install if you happen to have node. The script itself is shell.

TL;DR

It's just a tiny wrapper around git-merge-file to simplify the API. See ./lib/git-resolve-conflict.sh. (I used temp files instead of process substitution to make it msys/mingw-friendly).

It's better than git merge -Xours because that would resolve conflicts for all files. Here we can resolve conflict for just one file.

It's better than git checkout --ours package.json because that would lose changes from theirs even if they are not conflicted. Here we can resolve conflict using a three-way merge and keep the non-conflicted changes from both sides.

See also http://stackoverflow.com/q/39126509/245966

Description

Say you have multiple git branches and you want to merge between them, and always resolve conflicts in a particular file with a fixed strategy (say ours).

For instance, you have master (stable) and develop (unstable) branches. When code is stable you freeze the master, and development continues in develop, which is merged to master every few weeks.

But then you find our bugs at regression testing stage, you fix them in master, and you build. In the meantime, you also build develop separately. Each build bumps version field in package.json.

Since those branches can be built separately, a file like package.json will be modified in both branches, and there'll be a merge conflict due to version field being changed in both branches.

How to easily resolve the merge conflict in an automated manner (script) in such a situation?

git built-ins that do not solve the problem

  • git merge -Xours: that would resolve ALL conflicts in ALL files using the same strategy. This might be too much. (For instance, you might want to have an automatic merging script, which can do a successful conflict resolution only if foobar.json is the only file that was modified; on any other files modified, the merge should fail)

  • git checkout --ours filename.txt: that would discard ALL the changes from theirs version, which is brutal. There might be some valid, non-conflicting changes that would be discarded this way.

  • What we need is something like git-resolve-conflict --ours filename.txt

This is what ./lib/git-resolve-conflict.sh from this repo provides.

Details

See README-extended.md

git-resolve-conflict's People

Contributors

bnaul avatar jakub-g avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

git-resolve-conflict's Issues

Allow to pass multiple files

Allow to pass several filenames to CLI commands, so the same strategy is applied to all of them without needing to do multiple calls

Installation istructions fail - at least for confused people like me

copy /lib/git-resolve-conflict.sh to your .bashrc (this adds just git resolve-conflict)

I copied file contents into my .bashrc file. Not sure how it would add git ... but I tried anyway.

Predictably git resolve-conflict --theirs app/src/main/res/values-da/strings.xml failed with

`git: 'resolve-conflict' is not a git command. See 'git --help'.``

(because why adding function to .bashrc file would create a git function?

Maybe .gitconfig also should be modified? I have for example lg = "!gitk --all&" there.

Looking at how it is defined I expected resolve-conflict() --theirs app/src/main/res/values-da/strings.xml to work, but this failed with

bash: syntax error near unexpected token --theirs'`


Feel free to close it with "hahahahaha, learn to use Linux" but I just wanted to let you know that the current installation instruction is a bit confusing - at least to me.

Using `git resolve-conflict` with "both added" conflict shows a fatal error

For "both added" conflicts, git show :1:path/to/file yields a fatal error since there is nothing in stage 1. This does not crash the script since the script does not use set -e, and the file where the output from git show is redirected ends up being empty (which is the right result), but it would be nice to detect that situation and avoid the command in that case.

Adding a '--no-add' option

Hi @jakub-g !

Thanks for this script, it's very useful for resolving simple conflicts. I was wondering if you would be willing to accept a PR to add an optional option --no-add to prevent the script from running git add ?

I often want to check the files with git diff before adding them. This would not change the default behaviour of the script.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.