How can I keep the latest commits to avoid merge conflicts?

Solution 1:

Unfortunately, most likely you can't.

When you have multiple commits that are conflicting during a rebase, each commit is replayed one by one. From the point of view of conflicts at each commit, the changes in the last commit don't exist yet, so it wouldn't be possible to automate choosing the final version of that file.

So what can you do? There are some options that might work for you:

  1. If you are willing to squash your commits into a fewer number of commits, you will have less total conflicts to work through.
  2. If you don't wish to squash your commits, and the pain caused by the rebase is greater than the benefit of rebasing in the first place, then you could merge instead, and then you will only have one set of conflicts to deal with.

Also, as chepner mentioned in a comment, you may be able to use rerere to assist with resolving similar conflicts on a per commit basis during your rebase.