Hello everyone, I am 17.
Today I will talk to you about the usage of git check-pick, git patch and git stash.
git cherry-pick
Why use cherry-pick?
Cherry-pick can be considered for scenarios that are not suitable for merge.
Imagine the following scenarios
-
Only want to sync partial commits of the branch. The two branches are completely independent features and are not suitable for merge.
-
Don't want to synchronize branches prematurely.
Here are a few examples. dev is the branch, and A and B are commits.
git cherry-pick dev 将 dev 分支的最近一次提交,转移到当前分支。
git cherry-pick A 可以转移有权访问的任意分支的任意提交。
git cherry-pick A B 一次可以同步多个提交
转移从 A 到 B 的所有提交,不包含 提交 A。提交 A 必须早于提交 B,否则命令将失败,但不会报错。
git cherry-pick A..B
包含提交 A
git cherry-pick A^..B
If there is no conflict, a new commit will be formed in the current branch, and the content of the commit is exactly the same as the message, except that the hash (commit id) value is different.
If there is a conflict, the method of resolving the conflict has already been mentioned in git checkout
that section, the solution is the same, use it at the end git cherry-pick --contine
, if you want to undo itgit cherry-pic --abort
git patch
Why use patch?
It is not suitable for merge or cherry-pick scenarios, you can consider patch.
Imagine the following scenarios
- Two different git libraries, a certain piece of code needs to be synchronized.
- Some changes affect all developers, but you want to make this change, to verify something. You need another development cooperation, and you need to synchronize this modification to him. Direct copy is a way, but if there are many modifications, it is easy to make mistakes, so it is more appropriate to use patch.
Although check-pick can also synchronize different libraries, in practice, it is not convenient to synchronize online because of permissions or security issues.
patch scheme
There are two options for pach, diff and format-patch.
Diff only retains the difference between the file re-A state and B state, but does not retain information such as commit record messages. Diff can generate a single patch with multiple commits. git apply
Apply the patch with .
format-patch completely retains the completion information of each commit, and each commit generates a patch file. git am
Apply the patch with .
Checks are done with git apply --check. View is git appplay -stat
diff generates patch, apply applies patch
make patch
git diff >fix.patch
git diff 38d8e02 >fix.patch 相当于
git diff 38d8e02 HEAD >fix.patch
In short, the results of diff can be used to make patches.
apply patch
git apply --check fix.patch
git apply fix.patch
format-patch make patch, am apply patch
git format-patch -2 用最近的两次提交制作 patch
git format-patch commitId 某次提交以后的所有patch,不包括本次提交
git format-patch --root commitId 从第一次提交到指定提交的所有 patch
git format-patch -o patch -2 输出 patch 文件到 patch 文件夹
format-patch
The patch made is one file per submission, arranged in positive order.
0001-第一次提交.patch
0002-第二次提交.patch
application submission
git apply --check *.patch
git am *.patch
git stash
The English original meaning of stash is storage. git stash
The function of is to save the content of the current workspace. Unlike committing to the staging area, git stash
stashing content is not affected by branch switching.
Application Scenario
- After developing for a while, I found that the branch was wrong. The best copywriting at this time is
git stash save
to cut to the new branchgit stash pop
. - Halfway through the development, there is an urgent bug to fix, and submitting at this time will result in an invalid submission record. You can first
git stash save
switch branches to fix bugs, and then switch backgit stash pop
.
Note: Files that have not been added will not be stashed. If you want to stash these files together, you can add
-u
the parameter , which is--include-untracked
short for , git stash -u.