git cherry-pick and git show + git apply and git merge

Phanix
·
·
IPFS
·

If there are partial changes or new features, using git merge is sometimes not a good idea

The advantage of git merge is that a branch commit can be directly integrated into another branch, but if there are too many conflicts, sometimes the resolve conflict will go crazy, especially if the two branches have been separated for a long time.

Another situation is that if a "part" modification of a commit is to be applied to the current branch, then merge is not applicable. At this time, git cherry-pick is very suitable, especially adding -n (ie –no-commit) this command will apply all the changes of another commit, for example;

 $ git cherry-pick 951c5c77 -n
$ git status
On branch A
Your branch is up to date with 'origin/A'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

  modified: js/main.js
  modified: main.html

The parts not needed by cherry-pick can be done with git rest and git add.

Another way is to use git show with git apply. The former can display the changes made by a commit. If the file name is added, only the changes of the file will be listed. E.g:

 $ git show 951c5c77 main.html
commit 951c5c77be28f0a8c3f21f406895aab187003473 (origin/A, A)
Author: P <p@test.com>
Date: Thu Mar 28 14:58:43 2019 +0800

    some commit message

diff --git a/main.html b/main.html
index 8f5c4ac..c4f97f3 100644
--- a/main.html
+++ b/main.html
@@ -84,11 +84,11 @@
                                <div class="carousel-inner" role="listbox">
                                        
                                </div>
- <a class="left carousel-control" href="#glasses-selection" role="button" data-slide="prev">
+ <a class="left carousel-control" href="#glasses-selection" role="button" data-slide="prev" id="preitems">
                                        <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                                        <span class="sr-only">Previous</span>
                                </a>
- <a class="right carousel-control" href="#glasses-selection" role="button" data-slide="next">
+ <a class="right carousel-control" href="#glasses-selection" role="button" data-slide="next" id="nextitems">
                                        <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                                        <span class="sr-only">Next</span>
                                </a>

List the different parts, and then git apply to update the past.

 $ git show 951c5c77 main.html | git apply

However, with cherry-pick or git show + git apply, no commit relation similar to git merge will be established, that is, the association may be known only by the commit message.

Original link: Phanix's Blog

CC BY-NC-ND 2.0

Like my work? Don't forget to support and clap, let me know that you are with me on the road of creation. Keep this enthusiasm together!