I want to know how to pull with rebase from the origin, and if the files have conflicts I want to keep the latest modified file.
Is there a way in git to get the modified date of each file in a commit or just the commit date? Or is there a way to list the files with differences from your local branch vs the origin and tell on which side is the latest modified file?
Example Scenario:
origin/mybranch:
- File A (modified today)
- File B (modified yesterday)
local/mybranch:
- File A (modified yesterday)
- File B (modified today)
If I git pull --rebase
I want to keep the latest versions of File A and File B.
I'm using GitPython to manipulate the repository, so I need an automatic way of resolving any conflicts using this criteria (keep the changes from the latest modified file).
First of all there's no such merge strategy as "favor the latest change by the commit date" out of the box. Here's the list of supported strategies
From this list it seems that "recursive" strategy with "ours/theirs" option might solve some part of your example case:
Because you want to use "rebase", it means that it would take "origin/mybranch" as the base of your merge, and then try to apply your new commits on top of it.
Then there are 2 cases: either your change happened later - in this case it will apply cleanly,
or your change happened earlier - and in this case you say that you want to apply their version (that is --strategy-option
"ours" or "theirs", I think it is "ours" for rebase, this is confusing...).
The problem with this is that if your change happened today, and their change happened yesterday, but you forgot to sync in the morning, then even that your change happened later (by commit date) it will still produce a conflict, and be resolved inappropriately to their version instead of yours.
If that's not exactly what you want, you can probably write a custom automatic merge tool that when presented with a conflict is going to find out commit dates that caused it, and choose ours/theirs depending on which commit date is latest.
Then you can write a script that applies rebase and your custom merge tool on each conflict.
Given a conflicting file name, and knowing remote and local branch names, you can find out a commit at which this file was last modified for each branch - see How do I find the most recent git commit that modified a file?
Then get dates of those 2 commits and compare them
and take appropriate merge decision: copy over LOCAL or REMOTE.