Partager via


Git and Visual Studio 2017 part 7 : Cheery-picking changes from other branch

In previous article, we see merge and rebase as part of branching related functions. Then I cannot avoid “cherry-pick”, which takes several commits from a branch into current one.

Cheery-pick in Git

Before start doing anything, lets reset the current status so that master has Patch1.cs commit where as dev branch has Class5.cs and Class6.cs commits.

1. Run ‘git log --oneline --graph --all’. This is right after I completed rebasing in the previous article.

image

2. I want to reset rebasing by reset dev pointing to previous “Add Class6.cs” commit, but I forget the SHA1 hash value. Run ‘git reflog’ which shows all the history with any ref changes, such as new commit, reset, rebase, merge, etc. From the list, find the commit with “Add Class6.cs” comment.

image

3. After you checkout dev, run ‘git reset --hard 096f270’ and ‘git log --oneline --graph --all’. Now it is back to before rebasing.

image

4. Now, rather than merge or rebase, I simply want to take the change of “Add Patch1.cs” into dev branch. Run ‘git cherry-pick 7064b89’. You can also specify master instead of 7064b89 as well. You see warning that the is conflicts.

image

5. We already know how to resolve the conflicts. Run ‘git mergetool’ and resolve the conflicts using Visual Studio.

image

6. Then run ‘git commit -am “cherry-pick Patch1.cs” to add and commit at the same time. Another choice is to run ‘git cherry-pick --continue’ like rebase. It opens up commit comment editor.

image

7. Run ‘git log --oneline --graph --all’. Master points to same commit as before, but dev has new commit from master and now it has three more commits. Unlike rebase, the Patch1.cs commit comes as latest commit.

image

8. Let’s say if I committed to master branch by mistake, then I can reset the master to previous commit. Run ‘git checkout master’ and ‘git reset --hard HEAD~1’. When you run ‘git log --oneline --graph --all’, you see it is straight graph again and master points to commit 473591c.

image

9. Run ‘git reset --hard 7064b89’ at master branch, and ‘git reset --hard 096f270’ at dev branch so that it goes back to original state.

image

Cherry-pick multiple commit in Git

Let do the other way around. In this case, I cherry-pick two commits from dev to master.

1. Run ‘git checkout master’ and run ‘git cherry-pick d47a278 096f270’ to accept two commits. As these are last two commits of dev branch, you can run ‘git cherry-pick ..dev’ as well.

2. It shows you conflict message for csproj. Resolve the conflict by ‘git mergetool’, then run ‘git cherry-pick --continue’. It opens an editor to modify commit message. As I simply want to use the same message, type “:q” and hit enter.

image

3. You get another conflict. Run ‘git mergetool’ to merge and ‘git cherry-pick --continue’. Do the same for comment editor.

image

4. Run ‘git log --oneline --graph --all’ to see the history. Master branch has two new commits from dev branch. If you don’t need these commits in dev branch, you can reset it.

image

Cherry-pick in VS

1. Cherry-pick should be done in dev branch, so checkout dev branch. As I want to cherry-pick from master branch, right click master branch and click “Cherry-pick”.

image

2. As expected, Resolve Conflicts windows comes up. Merge it and resolve the conflict.

image image

3. Click “View Change” after resolved all conflicts. Click “Continue” link.

image

4. At this point, see the message carefully that it says. “Cherry-Pick completed and committed”.

image

5. Check history and Solution Explorer, as well.

image image

Cherry-pick multiple commit in VS

1. Checkout to master branch in master branch.

2. Right click dev branch and click “View History”.

image

3. Now you see history of dev branch while you are on master branch. You can see tab name to make sure what history you are seeing. Right click d47a278d commit and click “Cheery-Pick”.

image

4. You see conflicts as before. Resolve everything and complete the cherry pick. Repeat for commit 096f2703. If you see the history in master, it shows two commits are coming to master now.

image

Summary

Same as merge and rebase, Git and Visual Studio works slightly different for cherry-pick. However, once you understand what you are doing, then you have all the controls. Go to next article.

Ken

Comments

  • Anonymous
    October 03, 2017
    Thank you for your highly educated article. I really enjoyed
    • Anonymous
      October 03, 2017
      Thanks!
  • Anonymous
    October 03, 2017
    How 1990's of you to have a 7 part blog with no links to the previous articles, if you are going to say Part 7 then you should always include links to the other sections.
    • Anonymous
      October 03, 2017
      You are absolutely right. I am just lazy to not add any link by hoping you figure it out by going back to top page. Well maybe i should add link to at least previous article.
      • Anonymous
        October 04, 2017
        I, at least, put link back to previous article in the beginning of each article.
  • Anonymous
    October 03, 2017
    Hi Nakamura san, Look forward for the part 8. :-DRegards,Rui
    • Anonymous
      October 04, 2017
      Thanks! I just posted it :)