Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to git cherrypick all changes introduced in specific branch

Background info:

Due to restrictions in workflow with out existing systems, we need to set up a somewhat unorthodox git process.

(patch)    A-B---F              |   | (hotfix)     C-D-E                  | (dev)      1-2-3-G 

On the patch branch, there are some commits. The files here are similar but not identical to the ones on dev (sync scripts switch around the order of settings in many of the files, making them appear changed while they are functionally the same).

A fix is needed on this branch so a hotfix branch is created and worked on. This branch is then merged back into patch, so far, so good.

This same fix needs to be deployed to the dev branch so it stays relatively in sync with patch, but trying to merge the hotfix branch leads to git trying to merge all the unrelated and 'unchanged' files from A and B as well, rather than only C,D and E.

Question:

It seems that cherry-pick does what we want in terms of only getting changes from selected commits, but I would really like a way to cherry-pick all commits in a given branch at once, without having to look up the commit ids every time.

like image 289
Valyrion Avatar asked Feb 16 '16 16:02

Valyrion


People also ask

Can we cherry pick from one branch to another?

You can cherry-pick a commit on one branch to create a copy of the commit with the same changes on another branch. If you commit changes to the wrong branch or want to make the same changes to another branch, you can cherry-pick the commit to apply the changes to another branch.

How do I cherry pick a branch in git?

Cherry picking is the act of picking a commit from a branch and applying it to another. git cherry-pick can be useful for undoing changes. For example, say a commit is accidently made to the wrong branch. You can switch to the correct branch and cherry-pick the commit to where it should belong.

Does git cherry pick rewrite history?

Running git rebase typically rewrites history and can appear to move entire branches around. This is likely the source of the undesired history you are seeing. In contrast, git cherry-pick replays the delta from one or more commits elsewhere in a repository's history.


2 Answers

It seems that cherry-pick does what we want in terms of only getting changes from selected commits, but I would really like a way to cherry-pick all commits in a given branch at once, without having to look up the commit ids every time.


Using cherry-pick

git cherry-pick allows you to pick any commits you made in any branch to any other branch. In your case you can simply checkout master branch and then cherry-pick all the commits from any branch that you wish (cherry-pick supports ranges so you can specify start and end commit instead of listing all the commits).

This way you can control the way your commits will appear in the desired branch.

For example:

git cherry-pick ebe6942..905e279 

# Find the range of commits you wish to re-add to your branch. # then use cherry-pick to add them back to the branch git cherry-pick start..end  # If you wish to include the start commit as well add the ^ # This will result in a cherry-pick of the start commit included as well  git cherry-pick start^..end 

How to find first commit of branch?

git log

 # print out the latest commit (first one) of the given branch  git log  --oneline | tail -1 

merge-base

Use the merge-base command to find where the branch was split from the original branch:

git merge-base A B 
like image 132
CodeWizard Avatar answered Oct 13 '22 01:10

CodeWizard


If you want to cherry-pick all commits from branch dev.

Try:

git cherry-pick ..dev

like image 30
Linji Avatar answered Oct 13 '22 00:10

Linji