Before the article
Before I do that, I would like to ask you a question. How long after you were exposed to Git, did you know about this command?
My answer is that after a long time, it’s really a sad story. It is true that laziness is the root of all evil.
What can cherry-pick do?
Cherry, Chinese translation is cherry, Chinese translation is to pick. Git cherry-pick means to find the word “cherry” in your project file and ask the blogger to exchange it for cherries.
The above is joking, writing a blog, what, serious point!
Git cherry-pick allows you to select one or more existing committed changes to be introduced into the current content using the git cherry-pick command.
So, when do you get such an unusual command?
Let’s say you’re working on a project with a feature branch called Develop. Feature has three submissions, namely A, B and C. The develop branch only wants to add C, and the merge operation cannot be satisfied because it will merge all three commits directly. I want to merge only C, not A and B. Cherry pick!
Specific practices:
- Switch to the Develop branch.
- through
git log feature
, find the SHA1 value of C. - through
Git cherry-pick <C SHA1>
Merge C’s changes into the current content branch Develop. - If there are no conflicts, the process is complete. If there is a conflict, follow the normal conflict resolution procedure.
cherry-pick VS merge, Ready? GO!
Merge and cherry-pick merge and cherry-pick merge merge and cherry-pick merge merge and cherry-pick merge
Git merge: Merges two commit histories. Git cherry-pick: merge the submitted content.
It’s important to be clear that commit means change!
In this example, the content submitted in C is compared with the modification made in B, which may be to create a file or modify a word. So C content is an addition to a file, and a modification of a word.
Commit history with commit C as the end point. The actual content is all changes before commit C and C.
The object of the cherry-pick operation is commit. The object of merge is the Commit History.
So, when you use it, you have to know what you want.
The blogger invites you to participate in a game of cherry picking
All talk, no practice, now write a small demo test.
- Create an empty folder GitDemo,
git init
Initialization. - Create a random file, complete the initial commit, and create the master branch.
- Create and switch the Develop branch to create each commit, one file for each commit, for easy testing.
Specific commands are as follows:
// Switch to GitDemo and initialize Gitcd. Git add. Git commit -m'Create cherry-pick file, first commit'Git checkout -b develop touch cherry1. TXT git add. Git commit -m"Create cherry File 1"Git add. Git commit -m"Create cherry file 2"Git add. Git commit -m"Create Cherry file 3"
Copy the code
Above, the test scenario is constructed. Git develop commit history
Now, take a look. What size cherry do you like best? Pick whichever one you like. I like no. 3, seen from above 3 SHA1 value is 9 e2d49b7c6d868c4cac4c5198d6661837eca813b, before using a few is enough.
Git checkout master git cherry-pick 9e2d49bCopy the code
Select successful, through the ls command, see the success of adding cherry no.3.txt.
Cherry pick game success!
In addition, it should be noted that cherry # 3 picked to master is actually not the real # 3, but a copy of # 3. The SHA1 value of the two is different, so it can be confirmed that these are two commits.
Learn more about cherry-pick
After understanding the nature of the cherry-pick operation, look at the other commands and there is no pressure. All commands have a detailed look at the official documentation, here I give a few more commonly used:
git cherry-pick <commits>
Copy the code
Select multiple submissions to merge, separated by Spaces. Git cherry-pick 4d2951 e4cdff9, for example, can be used to pick numbers 1 and 3 in one go.
git cherry-pick <start-commit>.. <end-commit>Copy the code
Select multiple commit merges within a range, but this syntax corresponds to the operation distinction of open left close right, and does not include start-commit. Note that there must be a sequential relationship between two commits, and the former must precede the latter.
git cherry-pick <start-commit>^.. <end-commit>Copy the code
This is the same thing as the above, except that with the ^ sign, it’s now a closed interval, start-commit.
git cherry-pick <branch name>
Copy the code
Pick the submission at the top of branch. Git cherry-pick develop
Git cherry-pick --continue git cherry-pick --quit git cherry-pick --abort // Abort this operationCopy the code
–continue to proceed to the next operation, –quit to end the cherry-pick operation. Abort returns to the pre-cherry pick state, including those that have been successful in multiple commits.
The tail piece
Understanding the nature of commit is critical for this command. Well, see you in the next blog