thisisdee
thisisdee

Reputation: 878

How to merge back cherry-picked commits in git?

I have 2 main branches master and development. I recently had to push a hotfix that are commits already merged to development, and to do so I cherry-picked commits from development, created a new hotfix branch, added another commit for the fix, and merged that to master. My question is, how can I now merge the new commit to development without messing up the git commit history?

Roughly, what I did:

git cherry-pick <commit SHA>
git checkout -b hotfix-branch
[make more changes]
git commit -m "Fix issue"
git checkout master
git merge hotfix-branch

Now, if I look at git diff between master and development, the old commits from development is showing up because they're cherrypicked, therefore different commits with the same changes. What is the best way to merge back to development and "fix" these diff?

Edit to add: Alternatively, what should I have done in a situation like this, where a hotfix consists of commits in development branch?

Upvotes: 3

Views: 1747

Answers (1)

sevo
sevo

Reputation: 4609

You need to cherry pick the fix "downwards" the history tree so that later you can cleanly merge it "upwards" to other branches.

  1. Checkout the merge-base (typically, sometimes further down the tree).
  2. Cherry-pick A resulting in a new commit X.
  3. Merge X back to both of the branches.

After this, both branches will have X in common, which means they both contain the fix.

Upvotes: 1

Related Questions