My team is planning to switch from Perforce to Git and I am trying to find a way to make Git ignore pom version differences between branches. This works well in Perforce and I'm not having any luck reproducing the behavior with Git.

Here are my steps:

  1. Checkout parent branch

    ndeckard@ws /c/dev/proj/testgit (master)
    $ git checkout release/1.0
    Switched to branch 'release/1.0'
    Your branch is up-to-date with 'origin/release/1.0'.
    
  2. Create child branch from it

    ndeckard@ws /c/dev/proj/testgit (release/1.0)
    $ git branch branch/FEA-650
    
  3. Switch over to new branch

    ndeckard@ws /c/dev/proj/testgit (release/1.0)
    $ git checkout branch/FEA-650
    Switched to branch 'branch/FEA-650'
    
  4. Update child branch pom version

    <version>1.0.0-FEA-650-SNAPSHOT</version>
    
  5. Add it and commit

    ndeckard@ws /c/dev/proj/testgit (branch/FEA-650)
    $ git status
    On branch branch/FEA-650
    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:   pom.xml
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    ndeckard@ws /c/dev/proj/testgit (branch/FEA-650)
    $ git add pom.xml
    
    ndeckard@ws /c/dev/proj/testgit (branch/FEA-650)
    $ git commit -m "set feature branch pom version"
    [branch/FEA-650 59e156e] set feature branch pom version
    1 file changed, 1 insertion(+), 1 deletion(-)
    
  6. Switch back to parent branch

    ndeckard@ws /c/dev/proj/testgit (branch/FEA-650)
    $ git checkout release/1.0
    Switched to branch 'release/1.0'
    Your branch is up-to-date with 'origin/release/1.0'.
    
  7. Merge child branch into parent auto-accepting parent branch version (using the “ours” merge strategy)

    ndeckard@ws /c/dev/proj/testgit (release/1.0)
    $ git merge branch/FEA-650 -s ours
    Merge made by the 'ours' strategy.
    
  8. Attempt 2nd merge of child into parent (Already up-to-date.) Good. This is what I want

    ndeckard@ws /c/dev/proj/testgit (release/1.0)
    $ git merge branch/FEA-650
    Already up-to-date.
    
  9. Checkout child and merge parent into child (it fast forwards and sticks the parent branches pom version on the child). Not good. I need it to say "already up-to-date" like above and keep the child branch pom version as it already is on the child branch

    ndeckard@ws /c/dev/proj/testgit (release/1.0)
    $ git checkout branch/FEA-650
    Switched to branch 'branch/FEA-650'
    
    ndeckard@ws /c/dev/proj/testgit (branch/FEA-650)
    $ git merge release/1.0
    Updating 59e156e..2f3a2a0
    Fast-forward
    pom.xml | 2 +-
    1 file changed, 1 insertion(+), 1 deletion(-)
    

After step 7, I would like merges in either direction between parent and child to say (Already up-to-date.)

Is there a way with Git to make future merges ignore the version number difference in the pom file between branches?

  • Can't you create an alias that check differences, and only merge if there is any ? If no difference is found you can output whatever you need. – Cristiano Araujo May 23 '15 at 1:28
  • I haven't ever dealt with this but I see three ways to approach the problem: (1) ignore POM files entirely, and auto-generate them from metadata (branch etc); (2) use smudge/clean filters to insert and remove branch specifics (this is an intermediate method between 1 and 3); (3) write custom merge driver(s) for such files. Not having actually done this in practice I can't say which methods really work or what the issues would be. – torek May 23 '15 at 1:48
  • Regarding (1) - Ignoring pom files entirely isn't an option for me. I still care about merging the other lines in those files. Regarding (2) - I came across references to smudge and filters in the documentation earlier and didn't drill into that. I don't fully understand what they are and will start reading about them to see if that can work. Regarding (3) - I'd like to avoid writing a special merge driver. The capability to mark a line difference as merged is something I get for free with perforce and I'd like to find something similar to that in git. – Nathan James Deckard May 23 '15 at 3:24
  • Would you consider non-Git-specific solutions that involve externalizing part of the version calculation out of the POM? If so please add to your question. – javabrett May 23 '15 at 23:29
  • Javabrett, your solution below looks like a nice alternative approach to the problem. I'll give it a try after the holiday. I would still prefer to keep my versions inside the POM and have the VCS track that those lines are already resolved. Perforce does this well following the above steps as stated and I assumed the capability to do something like this would be a basic feature of any modern VCS. – Nathan James Deckard May 24 '15 at 17:32

My initial thought is to automate your POM version numbers by partly externalizing the POM <version> computation, see further down in the answer. If you don't want to do that, then you need to re-evaluate your Git workflow.

Merging two-ways, into master and then back into the feature branch causes problems. Your git merge branch/FEA-650 -s ours using the ours strategy tells Git that you have integrated all commits and changes from the feature branch into master, including your pom.xml version change, which is lost, with master retaining its version. The master branch now considers the feature branch's HEAD to be a common ancestor (it's a parent commit on the merge), so when you merge it back into the feature branch, Git says "everything was resolved when you merged to master, there are no changes ... fast-forward".

The simple answer is that you should re-branch to a new feature branch after merging to master, giving your feature branch a new/later youngest common ancestor, then you need to somehow replay your POM <version> change. You should not continue to work on the original feature branch, since it has been merged. To pick up new changes from master you should re-branch. There seems to be some implication that you may have left changes on the feature-branch unmerged with master, otherwise you wouldn't need it anymore and could re-branch, or it might just be the <version> change commit you are interested in.

There's any number of ways to simplify/speed-up/automate the version-number management on re-branching, from externalizing (see below) to cherry-picking it from a placeholder branch, using a Maven plugin e.g. Versions Maven Plugin .

Original answer

Another approach would be to externally calculate the final/effective <version> outside your POM file, based on branch metadata or other input from your CI. This is slightly tricky to do in Maven without pre-munging pom.xml yourself after checkout, but take a look at the Maven External Version Plugin.

It hooks into the Maven lifecycle and will build a pom.xml.new-version file on the fly (which you can .gitignore), dynamically-replacing all or part of the version number, based on whatever you supply - a feature-branch name, git commit hash etc.

Build/deploy the plugin and add it to your POM:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-external-version-plugin</artifactId>
    <version>0.1.0-SNAPSHOT</version>
    <extensions>true</extensions>
    <configuration>
        <strategy hint="sysprop"/>
    </configuration>
</plugin>

... then get creative with replacing the version-string, for example you can:

mvn install -Dexternal.version-qualifier=$(git symbolic-ref --short HEAD| sed s_^master\$__)

... which will change 1.2.3-SNAPSHOT to 1.2.3-FEA-650-SNAPSHOT if the current checked-out Git branch is FEA-650. You might need to consider replacing / with - in your branch-naming strategy, depending on what Maven thinks of it (I find the /s confusing, but that's just me), or modify the sed accordingly.

At worst this would allow you to remove your version-number from of your POM, so other POM changes can be safely merged and known to be non-version-number related - if necessary you can keep version numbers in another file, using a Maven Properties Plugin to load them and replace the entire version number if required.

  • This looks like a nice alternative approach to the problem. I'll give it a try after the holiday. – Nathan James Deckard May 24 '15 at 17:19
  • This plugin is not playing nicely with the maven-jar-plugin. When I add in maven-external-version-plugin, the jar plugin gets this error (not enough character limit for full stack trace). Caused by: java.lang.NullPointerException at org.apache.maven.project.artifact.AttachedArtifact.getVersion(AttachedArtifact.java:138) – Nathan James Deckard May 28 '15 at 16:04
  • 1
    Just before that I see: [INFO] Building jar: C:\dev\code\myproject\target\myproject-4.0.0-EXT-VERSION-NATHAN-SNAPSHOT.jar and [ERROR] Failed to execute goal org.apache.maven.plugins:maven-jar-plugin:2.6:test-jar (default) on project myproject: Execution default of goal org.apache.maven.plugins:maven-jar-plugin:2.6:test-jar failed. NullPointerException -> [ – Nathan James Deckard May 28 '15 at 16:12
  • command line was: C:\dev\code\myproject>mvn clean install -Dexternal.version=4.0.0-EXT-VERSION-NATHAN-SNAPSHOT -e – Nathan James Deckard May 28 '15 at 16:14
  • What is your mvn version? The external version plugin requires minimum 3.2.0. – javabrett May 28 '15 at 20:09

It'd probably be burdensome to do this for every merge, but here's an idea.

git merge --no-ff --no-commit <other-branch>

git checkout HEAD -- pom.xml

The first command allows you to make content changes before the merge completes, even if there aren't any merge conflicts. The second replaces the pom.xml with whatever it was in the current branch's previous commit - effectively ignoring the pom.xml from the other branch.

Using the merge strategy --ours is problematic if there are conflicts in any file other than the pom.xml, so I'd be weary of using it for this purpose.

  • Thanks for your answer. Ignoring all future merge differences for the entire pom file is not quite what I'm looking for. I still need to be able to merge the rest of the file if other parts of it change in the future with it automatically ignoring just this line difference. – Nathan James Deckard May 23 '15 at 3:30
  • In the version control system i'm moving away from, whenever I create a new branch the very next step is to change just the pom version and then update the merge history such that just that line difference inside the pom is marked as merged. Subsequent merges that have other lines in the file changing can be merged while this pom version line follows the resolution of the first merge that ignored the version difference. A large part of our automated tooling depends on being able to mark individual lines like this as merged. I need git to remember the resolution of this line between merges. – Nathan James Deckard May 23 '15 at 3:30

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.