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.
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.
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)
So, you want to move the branch pointer that you are currently on to D?
This can be done a few ways:
git merge <commit-id>
will move the pointer forward using the fast-forward merge processgit merge <branch name>
will move the pointer forward using the fast-forward merge processThe Git Book - Basic Branching and Merging goes into more detail.
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.
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>