Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
There are many Git best practices, and Git tips are available for developers. And one of the cool Git tips is to use the cherry-pick command effectively. Cherry Pick is one of the most effective Git commands, and you can use it for copying commits from a branch and then pasting them to another.
The process is straightforward. Here, you must note that cherry-pick copies of the commits is a way so that the source branch still includes the original commits.
Some developers believe that cherry-pick in Git is not a particularly good practice. According to them, these commands might leave us with duplicate commits in different branches and thus mess up the history of Git altogether.
However, still, it’s not possible to deny that cherry-pick is extremely powerful as a Git command. If you succeed in understanding the actual working procedure of the command and use it with caution, you will indeed find it useful.
Cherry-picking is often used when there are some commits in the wrong branch. We can cherry-pick those commits and place them into the right branch. It’s that simple. There are times when we don’t need to merge an entire branch but require specific commits. In such situations, the required commits can be cherry-picked and placed into our branch.
Read on to know more about the cherry-pick in Git.
Different Applications of Cherry Pick
As cherry-pick is not consider as one of the best practices available for use, you must use it rarely, i.e. only when needed. Most of your merging jobs will be done effectively by the other commands available to you. Using those commands will also help you to avoid the occurrence of duplicate commits. This segment is, however, about scenarios that can only be managed by the use of cherry-picking.
For Team Collaboration
Team collaboration is essential. Assume a situation that requires two members of your team to work on different segments of a massive feature.
Let’s address the two individuals as Player 1 and Player 2. Suppose Player 1 has come up with a utility function that Player 2 needs. In such a situation, Player 2 may use the cherry-pick command to get the commit.
This would allow Player 1 to keep doing his business without reinventing the wheel.
To Restore Lost Commits and Undo Changes
There may be times when feature branches go stale and don’t get merged into the master branches.
There may be times when pull requests get closed before any merger happens. The biggest plus of Git is that it never loses such commits.
You can use commands such as it reflog’ and it log’ to find those commits and cherry-pick them.
Bug Hotfixes
Whenever a bug gets spotted, it’s crucial to come up with a suitable fix for the end-users. This should be done as fast as possible.
Let’s explain with an example. Suppose your QA team spots a pre-existing bug that you must get rid of quickly. Also, imagine a situation when you are dealing with a feature branch. If you want to complete the bug fix in the same feature branch as it has links with the module you are dealing with at the moment, the cherry-pick command will allow you to do so.
All you will have to do is making a clear commit addressing the hotfix. Things are now perfect for you to cherry-pick the commit to master.
Using Cherry Pick
Suppose there’s a scenario where you are dealing with a branch named feature, which has not yet been released for production. There’s another team that’s dealing with a different branch, which they are calling feature2. What will you do if you need a specific number of commits from the branch named feature 2?
Cherry-picking is the best option you have if you don’t want to make all changes in feature 2 a part of the branch you are dealing with.
One thing you must make sure before you begin to cherry-pick is that you are not a part of the branch from where you are looking to pick commits. Additionally, it would help if you also double-checked whether you own all the newest commits. One way of confirming these things is by hitting the git pull and updating the branch you are working on.
And the next thing you will have to do is checking the commits logs at the branch you are dealing with. Now, pick the commits (you may pick one or more than one depending on your needs) you are looking to apply to a different branch. Here also you will have to hit the git log on the branch you are working on at the moment. This will provide you with the desired output.
Your next job would be picking the required commit ids (the number may vary according to your requirements) and switch to the branch where you need to paste the selected commits. After switching to the branch where you are looking to paste the chosen commits, you will have to hot the command below:
git cherry-pick <commit-hash>
In case, you have plans of pasting multiple hashes, you will have to keep a space between every commit and then execute the command. This should look like the command below:
git cherry-pick <commit-hash1, commit-hash2, commit-hash3>
If you can complete the above steps as directed, you will see the branch getting updated and have the required commits.
Things you must remember
If you want to use a cherry-pick in Git effectively, you must keep certain things in mind.
When cherry-picking commits from private branches, you should use:
* git cherry-pick-x
When there’s a note to a commit, you cannot expect it to follow your cherry-pick command. In that case, you will have to use the following for bringing it over:
* git notes copy
Final Words
You should try to improve the Git workflow. And if you need to use you should use the cherry-pick feature.
However, still, you will have to remember that the Git community doesn’t encourage people to use this feature too often. So, it would help if you always looked to complete your job without using the cherry-pick command unless it’s necessary. You must remember that if you cherry-pick too frequently without following the order, your branch will not show those commits chronologically.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.