Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is Git selected and how to use it?

2025-03-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what is the selection of Git and how to use it". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

What is selection?

Using the cherry-pick command, Git allows you to merge individual submissions from any branch into your current Git HEAD branch.

When git merge or git rebase is executed, all commits of a branch are merged. The cherry-pick command allows you to select individual submissions for consolidation.

Benefits of selection

The following situation may make it easier for you to understand the selection function.

Imagine that you are implementing new features for the upcoming weekly sprint. When your code is ready, you will push it to the remote branch, ready for testing.

However, the customer is not satisfied with all the changes and requires you to present only some changes. Because the customer has not approved all the changes to the next release, git rebase will not have the expected results. Why is this? Because git rebase or git merge will include every adjustment from the last sprint.

Selection is the answer! Because it only focuses on the changes added in the submission, the selection only brings in the approved changes and does not add other submissions.

There are several other reasons you can use selection:

This is essential for bug fixes because the bug appears in the corresponding commit in the development branch.

You can avoid unnecessary work by using git cherry-pick instead of using other options such as git diff to apply specific changes.

It is a useful tool if the whole branch cannot be combined because the versions of different Git branches are not compatible.

Use the cherry-pick command

In the simplest form of the cherry-pick command, you only need to use the SHA identifier to indicate the submission you want to integrate into the current HEAD branch.

To get the submitted hash, you can use the git log command:

$git log-oneline

When you know the hash value submitted, you can use the cherry-pick command.

The syntax is:

$git cherry-pick

For example:

$git cherry-pick 65be1e5

This will merge the specified changes into the currently checked out branch.

If you want to make further changes, you can also ask Git to add the submitted changes to your working copy.

The syntax is:

$git cherry-pick-no-commit

For example:

$git cherry-pick 65be1e5-no-commit

If you want to select multiple submissions at the same time, separate their submission hashes with spaces:

$git cherry-pick hash2 hash4

When selecting a submission, you cannot use the git pull command because it can get the submission from one warehouse and automatically merge it into another warehouse. Cherry-pick is a tool that specializes in not doing this; on the other hand, you can use git fetch, which can get submissions but not apply them. There is no doubt that git pull is convenient, but it is not accurate.

Try it yourself.

To try this process, start the terminal and generate a sample project:

$mkdir fruit.git$ cd fruit.git$ git init.

Create some data and submit:

$echo "Kiwifruit" > fruit.txt$ git add fruit.txt$ git commit-m 'First commit'

Now, represent a remote developer by creating a replica of the project:

$mkdir ~ / fruit.fork$ cd! $$echo "Strawberry" > > fruit.txt$ git add fruit.txt$ git commit-m'Added a fruit "

This is a valid submission. Now, create a bad submission that represents something you don't want to merge into your project:

$echo "Rhubarb" > > fruit.txt$ git add fruit.txt$ git commit-m'Added a vegetable that tastes like a fruit "

Go back to your warehouse and get the submitted content from your imaginary developer:

$cd ~ / fruit.git$ git remote add dev ~ / fruit.fork$ git fetch devremote: Counting objects: 6, done.remote: Compressing objects: 100% (2reused 2), done.remote: Total 6 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (6 delta 6), done...$ git log-oneline dev/mastere858ab2 Added a vegetable that tastes like a fruit0664292 Added a fruitb56e0f8 First commit

You have obtained the submitted content from your imaginary developer, but you have not incorporated it into your version library. You want to accept the second submission, but you don't want to accept the third, so use cherry-pick.

$git cherry-pick 0664292

The second submission is now in your warehouse:

$cat fruit.txtKiwifruitStrawberry

Push your changes to the remote server, and this is done!

Reasons to avoid using selection

In the developer community, selection is usually discouraged. The main reason is that it causes repeated submissions, and you lose the ability to track your submission history.

If you select a large number of submissions out of order, they will be recorded in your branch, which may lead to unsatisfactory results in the Git branch.

Selection is a powerful command that can lead to problems if you do not correctly understand what may happen. However, when you screw up and submit to the wrong branch, it may save your life (at least your day's work).

This is the end of the content of "what Git is selected and how to use it". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report