Git move n commits forward

让人想犯罪 __ 提交于 2019-12-08 03:24:12

问题


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.


回答1:


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)




回答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.




回答3:


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>


来源:https://stackoverflow.com/questions/41905686/git-move-n-commits-forward

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!