Sergey Kolesnik
Sergey Kolesnik

Reputation: 3650

Compare changes from a set of commits to a prior version

I have a branch with commit A with a version of an application with verified working state. Than there are N commits that applied some potentially irrelevant/breaking changes. Followed by M commits which purpose was to implement some feature, but other potentially irrelevant/breaking changes were introduced. I need to select and apply changes that are only relevant to introduced feature and get rid of those that are irrelevant/potentially breaking.
So, it looks like: A -> N... -> M... -> P....
How can I compare changes from a set of M.. to A?


Similar question: Git diff between two commits without some commits in the middle

Upvotes: 0

Views: 71

Answers (1)

LeGEC
LeGEC

Reputation: 52151

I would suggest to create a branch, which starts from A, and contains only commits of the M... range.

You can do this using git rebase -i :

# create a new branch from your current branch :
git checkout -b feature

# run :
git rebase -i <A>

# an editor opens : the instructions on what to do with this file are
# explained as comments at the bottom of the file

# in your case : remove all lines mentioning commits in
# the 'N...' and 'P...' ranges

# save and exit

You may have to fix conflicts along the way, but the end result should be : a new branch, with a copy of all M... commits on top of A.

Comparing commits from this branch against A will be more straightforward.


Actually, you can have git rebase select the range of commits M... range of commits :

  • spot <lastN>, the hash of the parent of the first commit in M...,
  • spot <lastM>, the hash of the last commit in M...
  • run the following commands :
git branch feature <lastM>
git rebase --onto <A> <lastN> feature

You can add -i to the git rebase command, if you want to confirm that you selected all the commits you expect.


Cherry-picking each commit in M... on top of <A>, as you suggested in your comment, is also a valid way to create that branch :

# to list the commits :
git rev-list <lastN>..<lastM>

# to use it in a cherry-pick command :
git checkout -b feature <A>  # start from <A>
git cherry-pick $(git rev-list <lastN>..<lastM>)

All three ways will give the same result.

Upvotes: 1

Related Questions