简体   繁体   中英

How to merge only one commit to master and not all commits of a branch?

I have two branches named dev and master . Currently the commits looks like the following:

dev    master
---    ------
A       A
B       B
C       
D
E

Now I want to merge only commit E to branch master . So here would be the expected result:

dev    master
---    ------
A       A
B       B
C       E
D
E

Any idea how can I do that?


Also how should I manage to be able merge commits C , D later to branch master for being fully sync.

TL;DR

At first, E shouldn't depend either C or D . Otherwise, you probably will have to solve some conflicts or you will be in a state that is not complete.

Problems generally have one more solution. I will try to offer some.

1. Cherry Pick

A--B(master)--C--D--E(dev)

Create a branch where master branch points: git checkout -b topic master

A--B(master, topic)--C--D--E(dev)

Cherry pick commit E : git cherry-pick E

A--B(master)--C--D--E(dev)
     \
      E'(topic)

Merge topic branch into the master branch: git checkout master git merge topic

A--B--C--D--E(dev)
     \
      E'(master, topic)

At this point, if you rebase dev branch onto master branch, E commit will be disappeared because they are(should be) the same: git checkout dev git rebase master

A--B--E'(master)--C--D(dev)

2. Interactive Rebase

A--B(master)--C--D--E(dev)

Interactive rebase dev branch onto master branch and move E commit at before C commit: git checkout dev git rebase -i master

A--B(master)--E--C--D(dev)

Create a branch where E commit points: git checkout -b topic E

A--B(master)--E(topic)--C--D(dev)

Merge topic branch into master branch: git checkout master git merge topic

A--B--E(master)--C--D(dev)

3. Rebase --onto

A--B(master)--C--D--E(dev)

Create a branch where dev points: git checkout -b topic dev

A--B(master)--C--D--E(dev, topic)

Run git rebase --onto BD topic

A--B(master)--C--D--E(dev)
     \
      E'(topic)

At this point, if you rebase dev branch onto master branch, E commit will be disappeared because they are(should be) the same: git checkout dev git rebase master

A--B--E'(master)--C--D(dev)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM