In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "Why to use Git cherry-pick command", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Why to use Git cherry-pick command" this article.
Avoid repetitive work
If you can copy the same commit directly to another branch, there is no need to remake the same changes in different branches. Note that the selected submission creates a new submission with a new hash in another branch, so if you see a different submission hash, don't be confused.
If you want to know what a submitted hash is and how it is generated, here is a description that can help you. The commit hash is a string generated by the SHA-1 algorithm. The SHA-1 algorithm takes an input and then outputs a unique hash of 40 characters. If you are using a POSIX system, try running this command on your terminal:
$echo-n "commit" | openssl sha1
This will output a unique hash value of 40 characters, 4015b57a143aec5156fd1444a017a32137a3fd0f. This hash represents the string commit.
The SHA-1 hash value generated by Git at the time of submission represents more than just a string. It represents:
Sha1 (meta data commit message committer commit date author authoring date Hash of the entire tree object)
This explains why any minor changes you make to the code will result in a unique commit hash. Even a small change will be found. This is because Git has integrity.
Undo / undo lost changes
Selection is convenient when you want to revert to the working version. When multiple developers are working on the same code base, changes are likely to be lost and the latest version is transferred to an older or non-working version. At this point, the selection submitted to the working version can be a savior.
How does it work?
Suppose there are two branches: feature1 and feature2, and you want to apply the commit in feature1 to feature2.
On the feature1 branch, run the git log command to copy the submit hash you want to select. You can see a series of submissions similar to the following code example. The alphanumeric code after commit is the submission hash you need to copy. For convenience, you can choose to copy the first six characters (966cf3 in this case).
Commit 966cf3d08b09a2da3f2f58c0818baa37184c9778 (HEAD-> master) Author: manaswinidas Date: Mon Mar 8 09:20:21 2021 + 1300 add instructions
Then switch to the feature2 branch and run git cherry-pick on the hash you just got from the log:
$git checkout feature2 $git cherry-pick 966cf3.
If the branch does not exist, use git checkout-b feature2 to create it.
There's a problem here. You may encounter the following situation:
$git cherry-pick 966cf3 On branch feature2 You are currently cherry-picking commit 966cf3d. Nothing to commit, working tree clean The previous cherry-pick is now empty, possibly due to conflict resolution. If you wish to commit it anyway, use: git commit-- allow-empty Otherwise, please use 'git reset'
Don't panic. Just run git commit-- allow-empty as recommended:
Git commit-- allow-empty [feature2 afb6fcb] add instructions Date: Mon Mar 8 09:20:21 2021 + 1300
This will open your default editor and allow you to edit the submission information. If you have nothing to add, you can save the existing information.
In this way, you have completed your first selection. As mentioned above, if you run git log on the branch feature2, you will see a different commit hash. Here is an example:
Commit afb6fcb87083c8f41089cad58deb97a5380cb2c2 (HEAD-> feature2) Author: manaswinidas Date: Mon Mar 8 09:20:21 2021 + 1300 add instructions
Don't be confused by different submission hashes. This is just a submission that distinguishes feature1 from feature2.
Select multiple submissions
But what if you want to select multiple submissions? You can use:
Git cherry-pick...
Please note that you do not have to use the entire submitted hash value, you can use the first five to six characters.
Similarly, this is also very cumbersome. What if the submission you want to select is a series of consecutive submissions? This method is too laborious. Don't worry, there's an easier way.
Suppose you have two branches:
Feature1 includes the submissions you want to copy (from earlier commitA to commitB).
Feature2 is the branch to which you want to transfer the submission from feature1.
Then:
Enter git checkout
.
Gets the hash values of commitA and commitB.
Enter git checkout
.
Enter git cherry-pick
^.. (note that this includes commitA and commitB).
If you encounter a merge conflict, resolve it as usual, and then type git cherry-pick-- continue to resume the selection process.
Important selection options
Here are some useful options in the Git documentation that you can use in the cherry-pick command.
-e,-- edit: with this option, git cherry-pick allows you to edit the submission information before submitting it.
-s,-- signoff: add a Signed-off by line at the end of the submission message. For more information, see the signoff option in git-commit (1).
-S [
],-- pgg-sign [=]: these are submissions of GPG signatures. The keyid parameter is optional and defaults to the submitter identity; if specified, it must be embedded in the option without spaces.
-- ff: if the current HEAD is the same as the parent submission of the selected submission, the submission will be fast-forwarded.
Here are some other subcommands for subsequent operations besides-- continue:
-- quit: you can forget what is currently in progress. This can be used to clear the status of subsequent operations after selection or revocation fails.
-- abort: cancels the operation and returns to the state before the operation sequence.
Here are some examples of selection:
Git cherry-pick master: apply the change introduced by the commit at the top of the master branch and create a new commit that contains the change.
Git cherry-pick master~4 master~2': applies the changes brought about by the fifth and third latest commits pointed to by master`, and creates two new commits based on these changes.
You don't know what to do? You don't have to remember all the orders. You can type git cherry-pick-help on your terminal at any time to see more options or help.
The above is all the contents of the article "Why use Git's cherry-pick command". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.