2

I'm in commit C (I just did a git checkout hash_of_C) of a repository.

A - B - C - ...<19>... - D - ...<many many commits> - Z

Which Z is the last commit in the master branch

Now, I want to move forward 20 commits.

Anastasios Andronidis
  • 6,310
  • 4
  • 30
  • 53
  • In general I guess it's not possible by using "20 commits in the future". It might work only on simplest trees (no merges, no forks, means no branches). – 0andriy Jan 28 '17 at 02:20
  • @0andriy Yeah, if you have to commit hash of the +20 commit, then moving HEAD forward is trivial. But, you are completely right about the "simple trees" part. – Darryl Johnson Jan 28 '17 at 02:31
  • @DarrylLJohnson, yeah, I just recall the term: *linear*. In non-linear cases it's non-trivial task. – 0andriy Jan 28 '17 at 23:05

3 Answers3

6

If I am understanding correctly, you want to move forward 20 commits from C, and Z is the last commit on the master branch but not necessarily the 20th commit from C.

From C do

C > git checkout $(git rev-list --topo-order HEAD..master | tail -20 | head -1)

It will checkout the 20 commit forward if it exists.

I am not totally confident about it but here is what it does.

git rev-list --topo-order HEAD..master will give you list of all the commits from C to master/one per line.

> ➦ b17c0a7 > git rev-list --topo-order HEAD..master          
3c5b47003a5bc576d6c5d13b065bb70aef28828f
306fee6443d0e2ebb0a5372dfdff0d55728950f3
02acfd4b6987dfc8bc689a18d21ee82ad8132839

When we do tail -20 we basically want to cut a piece out of this list from bottom 20 line up. If we do head -1, we cut the list further, taking only the top item. This way we get the 20th item forward from the current commit.

Which is $(git rev-list --topo-order HEAD..master | tail -20 | head -1)

  • And if you have fork to two branches and then merge them back and there are two `+20` commits in the history. Which one is correct? – 0andriy Jan 28 '17 at 23:04
  • @0andriy Fork to two branches (Fork as in repo ?). The above solution is only correct if someone goes to a detached state from the master branch. –  Jan 29 '17 at 03:39
2

So, you want to move the branch pointer that you are currently on to D?

This can be done a few ways:

  1. Using the commit hash for D, git merge <commit-id> will move the pointer forward using the fast-forward merge process
  2. If there is a branch pointer that points to D (like if D is pointed to by master, develop, etc) then git merge <branch name> will move the pointer forward using the fast-forward merge process

The Git Book - Basic Branching and Merging goes into more detail.

Darryl Johnson
  • 431
  • 3
  • 10
  • I'm in detached HEAD. I just did a random `git checkout` and I need to move forward from there. – Anastasios Andronidis Jan 28 '17 at 02:17
  • @ΑναστάσηςΑνδρονίδης to **where**? – 0andriy Jan 28 '17 at 02:21
  • @ΑναστάσηςΑνδρονίδης This depends entirely on your list topology. If your HEAD pointer is currently pointing to C and the topology can be completely described as `A - B - C - ...<19>... - D` then `git reset HEAD --hard ` will move the HEAD pointer to D. If you have done some other work and then checked out, different story. – Darryl Johnson Jan 28 '17 at 02:25
  • @0andriy From commit C to +20 commits – Anastasios Andronidis Jan 28 '17 at 02:25
0

Now, I want to move forward 20 commits.

You simply need to checkout master. since master latest commit it the Z which you want to checkout do this:

git checkout master

and that it. you will see the latest commit on your master.

Update

If commit D was not part of the original branch you can use chery-pick with a commit range to "import" your 20 commits to the repo

You can cherry-pick all the commits from one branch to another with the commit range ...

git cherry-pick <first commit>..<last-commtit>
Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • 1
    I think this answer has been downvoted but is actually correct. –  Jan 28 '17 at 07:43
  • 1
    @TheMagician doesnt op need to get to D? But the last commit in master is Z. Unless im missing something, i think itll get op to the wrong commit – jbu Jan 28 '17 at 11:07
  • You want get D since D is commited on a detached HEAD since the checkout of C created it so when you checkout master it will not be there unless it was there in first place. – CodeWizard Jan 28 '17 at 11:09
  • @jbu My answer is based relative to master branch. Maybe you are right. I still don't completely understand the question op is asking for (my answer is just a guess, hope it's right). –  Jan 28 '17 at 11:12
  • Your answer might be right buy with cherry-pick its much more simple and clean. – CodeWizard Jan 28 '17 at 11:14
  • @TheMagician, guys, had you really understood the question? I don't think so.\ – 0andriy Jan 28 '17 at 23:08