A simple guide for Git cherry picking in TortoiseGit š
And resolving conflicts in VS Code
A few weeks ago I wrote an article about Git submodules. This is a follow-up on that topic, because I am going to describe Git cherry picking using TortoiseGit. Also I am going to show you how to resolve conflicts in the commits which might contain also submodules. I work with Visual Studio Code, so I also have a tip for using these two programs side by side.
What is a Git cherry pick?
Git cherry pick is a command that enables us to pick a commit that we want from one branch and apply it to another branch. I think thatās the most common scenario. As an example letās assume we have 2 branches. One of them is the develop branch where we commit all the changes and another one is a feature branch. At some point we need to select (pick) certain commits from the develop branch and apply them to the feature branch.
In the image below, we pick 3 commits from the history of the develop branch and apply them to the feature branch. Therefore each selected commit represents a cherry.
Letās see how we can do it with TortoiseGit interface.
Cherry picking in TortoiseGit
First make sure that you have the correct branch checked out. In the example above, you would have to be in the feature branch. Right click on the folder where your repository lives and you will see a context menu with several options. Select Git Sync option as below.
You will see the following window. In the bottom left corner click Show log. (You can also access the log from the context menu: TortoiseGit -> Show log)
Now we will see a window showing the log of commits for the branch that we are in. In the top left corner click on the name of the branch (DEVPD-20744-ā¦ in the screenshot below) and another window will open.
The new window will contain a list of all your branches. Select one that you want to pick commits from and then search for a particular commit. The image below shows an already filtered list of commits, because I entered 20744 into the search bar. Those are the commits I want to cherry pick, so I selected them, right-clicked and chose āCherry Pick selected commitsā¦ā option.
If everything goes well you will see something like this. A green progress bar and a Done button that you can click to close the window.
Great! All the commits have now been applied to another branch and you should see them also in the log.
Edit conflicts
You might encounter some conflicts during cherry picking. Before you can continue, you ought to resolve them first. There are several options to choose from:
- Edit conflicts
- Resolved
- Resolve conflict using āCherry_pick_headā
- Resolve conflict using āHEADā
Sometimes you might need to review them first. If you click Edit conflict you will see a window like this. I donāt quite like this because I find the colours and 3 views a bit confusing.
When I need to review and compare the changes, I prefer to open Visual Studio Code and do it there. The green colour means Current change (HEAD) and the lines highlighted with the blue colour represent Incoming change (CHERRY_PICK_HEAD).
I can then Accept what I consider correct, save the file and in TortoiseGit I click Resolved and Continue.
Resolve conflict in submodule
What if you also use submodules? This is a special conflict case. If you use submodules in other branches, make sure that you first update and synchronize them before cherry picking.
If you take care of submodules first, then you can simply click Resolved and the proper changes that you pulled before will be used.
Please note that the Resolve command does not really resolve the conflict. It uses āgit addā to mark the file status as resolved to allow you to commit your changes
Summary
Cherry picking is a powerful tool in Git. If you work with many branches, chances are you will need to pick some commit from 1 branch and apply it to another branch. Notice we didnāt have to write a single command in terminal thanks to TortoiseGit and VS Code. At the beginning, you might find it confusing to click through many windows but once you figure it out itās a quite simple workflow.
When it comes to resolving conflicts, I prefer VS Code to TortoiseGit window, but otherwise itās straightforward and it takes only a few clicks. Be extra careful with submodules though.
Happy cherry picking! š