Using Git in the Real Life for Beginners 5

Wed, Jun 16, 2021 3-minute read

In real life, you are working on devleop branch, and devloped 3 commits on this branch commit A, B, C. But your team only want feature C; Or you just want to fix a commit in the middle of branch B.

6.Picking some features

So you cannot just use git merge xx, because those 3 features would be added to master branch. So now you need “cheey pick”.

Cherry-pick

step:

  • checkout -b develop
  • git log feature // find feature C’s SHA1
  • git cherry-pick C’s SHA1 // this numberf -> commit d6677889989089080079907907978u0

git cherry-pick d667788 // you don’t need copy the whole number

git cherry-pick [commit number]

git cherry-pick 4d2951 e4cdff9,

git cherry-pick [start-commit-number]..[end-commit-number] // want pick commit A, C

git cherry-pick [start-commit-number]^..[end-commit-number] // want pick commit (A, C], start from B to C

// 切换到branch2分支
$ git checkout branch2
Switched to branch 'branch2'
$ 
$ 
// 查看最近三次提交
$ git log --oneline -3
23d9422 [Description]:branch2 commit 3
2555c6e [Description]:branch2 commit 2
b82ba0f [Description]:branch2 commit 1

// 切换到branch1分支
$ git checkout branch1
Switched to branch 'branch1'

// 查看最近三次提交
$ git log --oneline -3
20fe2f9 commit second
c51adbe commit first
ae2bd14 commit 3th

Now I want to merge the first commit from branch2 into branch1

$ git cherry-pick 2555c6e
error: could not apply 2555c6e... [Description]:branch2 commit 2
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

solve the coflict

$ git commit 
[branch1 790f431] [Description]:branch2 commit 2
 Date: Fri Jul 13 18:36:44 2018 +0800
 1 file changed, 1 insertion(+)
 create mode 100644 only-for-branch2.txt

continue with cherry-pick, now it successfull combine together.

$ git log --oneline -3
790f431 [Description]:branch2 commit 2
20fe2f9 commit second
c51adbe commit first

If you don’t want to commit directly, check the “options”.

git cherry-pick [<options>] <commit-ish>...

common options:

    --continue  // continue the next operation
    --quit  // quite
    --abort  // abort this operation
    --n,    // do no git commit automatically
    --e        // edit commit message

git cherry-pick -n

  • without -n
$ git cherry-pick 23d9422
[branch1 2c67715] [Description]:branch2 commit 3
 Date: Fri Jul 13 18:37:05 2018 +0800
 1 file changed, 1 insertion(+)
$

Check the log

$ git log --oneline -3
2c67715 [Description]:branch2 commit 3
f8bc5db [Description]:branch2 commit 2
20fe2f9 commit second

  • git cherry-pick -n
// 回退上次提交,再此进行cherry-pick
$ git reset --hard HEAD~
HEAD is now at f8bc5db [Description]:branch2 commit 2
$ git cherry-pick -n 23d9422
$ git status
On branch branch1
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   only-for-branch2.txt

$ 
  • git cherry-pick -e

now I want to use cherry-pick -e to modify commit message to combine it

$ git cherry-pick -e 23d9422

  1 [Description]:branch2 commit 3
  2 //
  3 // It looks like you may be committing a cherry-pick.
  4 // If this is not correct, please remove the file
  5 //       .git/CHERRY_PICK_HEAD
  6 //  and try again.

$ git cherry-pick 23d9422
error: could not apply 23d9422... [Description]:branch2 commit 3
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'


git cherry-pick ..branch2 or git cherry-pick ^HEAD branch2

               C<---D<---E  branch2
              /
master   A<---B  
              \
               F<---G<---H  branch3
                         |
                         HEAD


               C<---D<---E  branch2
              /
master   A<---B  
              \
               F<---G<---H<---C'<---D'<---E'  branch3
                                          |
                                         HEAD

Ok, It’s time for Git 6


Git4