Specific commit merge

Asked

Viewed 3,555 times

3

I’m currently working on an SCM migration in my company, we’re moving from CVS to Git, but the code in CVS was versioned in a different way, each branch is a different code-base, so, I’d like to know how to merge only one commit at a time with Git.

1 answer

6


Cherry Picking

Each commit in a repository corresponds to a Tree full of files. Normally, these files have been created over several commits. But sometimes it is necessary to take the delta between two commits, and apply it to a different branch, as in your case.

In this case, you don’t want to take the current state of Tree (which may have unfinished or untested changes); you just want to take the delta associated with this change.

In other version control systems, you would only have to create a diff based on the most recent change, and then fix the change in your release branch. Instead, with Git, we can use the command cherry-pick to do the work for us:

git checkout master
echo Working >> file.txt
git commit -m "Working" file.txt
echo BugFix >> bugfix.txt
git commit -m "BugFix" bugfix.txt
echo More Working >> file.txt
git commit -m "More working" file.txt
# Vamos aplicar o 'bugfix' para release
git checkout release10
git cherry-pick master~1
[release10 41037ab] BugFix
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 bugfix.txt

This allowed us to make a single change - described here with master~1 - and copy the delta into the release branch.

If we had several changes, we could have master~3.. master~1. As opposed to just generating a diff and patch from the current Tree, this will copy the commits (and their relations) over the new branch.

Origin

Finally, it’s interesting to note that when you copy a change using this mechanism, the commit hash will change (notably because it will have a different hierarchy). Sometimes it doesn’t matter, but if you want to record where the original change came from, you can run git Cherry-pick-x. This inserts a commit message indicating where the original change came from:

git checkout release10
git cherry-pick -x master~1
git cherry-pick master~1
[release10 41037ab] BugFix (cherry picked from commit 938a4c0bbb3985524192aa8a926ea6757263e94b)
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 bugfix.txt

You can also make Cherry-pick from the SHA of the commit, just find the commit you want to bring to your branch and have it executed with the command:

git cherry-pick -x b53af978b3581509557c6e24923edf1c8a683e02

Creating a new history

Whenever you’re using Cherry-pick, especially if you’re reordering commits, you’re creating a new history. However, you never really throw away the old one, everything is available from the reflogs. You’re not destroying the history, you’re creating other alternatives. All Cherry picking gives you the ability to apply patches from other branches in a safe and error-free way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.