1

When I use Git checkout master~X, I will get the Xth merged commit, what I really want is the Xth real commit (not merged commits only).

How can I do that?

Thanks.

kambi
  • 3,291
  • 10
  • 37
  • 58

3 Answers3

1

Try something like the following:

git checkout $(git log --no-merges --skip=2 -1 --format='%H')

Change --skip=2 to the number of non-merge commits you want to skip over. If you want the first non-merge commit, use --skip=0; the second non-merge commit, use --skip=1; the third, use --skip=2; etc.


You could set up an alias (using this answer as a guide):

git config alias.co-non-merge '!git_co_non_merge() { git checkout `git log --no-merges --skip=$(($1 - 1)) -1 --format="%H"` ; } ; git_co_non_merge'

Add --global to make it a global configuration change. Then you could just use git co-non-merge X.

Community
  • 1
  • 1
Go Dan
  • 15,194
  • 6
  • 41
  • 65
0

You could get a list of commits that are no merge commits using

git log --no-merges

In that list, simply take the X-th result to do the checkout.

For your use case, the --online flag could additionally be used since the short display should be enough.

eckes
  • 64,417
  • 29
  • 168
  • 201
0

Merge commits can be replaced with rebase. Check this article out. However it only has effect on future branches.

michaelliu
  • 1,667
  • 2
  • 13
  • 13