开发者

Manual Merge to Different Commits in Git

开发者 https://www.devze.com 2023-04-04 19:14 出处:网络
I have a hierarchy of commits in my local repo and I want to roll back to an old commit and apply some, not all, of the changes from the HEAD commit. How do I do this?

I have a hierarchy of commits in my local repo and I want to roll back to an old commit and apply some, not all, of the changes from the HEAD commit. How do I do this?

Consider the following scenario. I have the following commits in my repo:

Commit 6 (HEAD)

Commit 5

Commit 4

Commit 3

Commit 2

Commit 1

I reset to commit 2 as below

git reset --hard commit2

Now I want, for example, to merge all the changes applied up until commit 6 but without those in commit 3 and with some of those in commit 5.

If I do this:

git merge commit6

I will end up having everything in the HEAD without the select开发者_开发问答iveness I am after.

How can I achieve my target?

Cheers AF


You may either cherrypick commits that you want.

Or use interactive rebase if you want to modify history in place (should never be done if you have already upstreamed your changes):

git rebase -i HEAD~6
# in editor that popped up remove line that corresponds to Commit3, save and quit


In this case you can use rebasing

You don't need to reset to commit 2

In your HEAD, type

git rebase -i HEAD~6

And interactive editor will be prompted up.

You can manipulate how's your commit would look like e.g. Delete the line commit 3

Save it

then your target result is what you are looking for


You can create a new branch with just the selected commits.

git checkout branch
git checkout -b new_branch

# remove unwanted commits
git rebase -i HEAD~6

# continue with merge

Or you can always cherry pick commits.

0

精彩评论

暂无评论...
验证码 换一张
取 消