這是一張有關標題為 Cherry Pick in Git 的圖片

Cherry Pick in Git

Learn how to use cherry-pick to selectively apply existing commits.

What is Cherry-Picking?

Cherry-pick is a method to select specific commits from one branch and apply them to another. For instance, when two developers are working independently on branches A and B, and an important bug fix is committed to branch A, branch B can apply this bug fix through cherry-picking.

Unlike rebasing branch B onto branch A, cherry-picking aims to apply individual commits without merging or reorganizing the entire branch.

Cherry-Picking Existing Commits

Using VS Code

Cherry-picking is relatively simple in Git. Right-click on the desired commit in the current branch and select Cherry Pick....

Current Branch Status Right Click → Cherry Pick Confirm Cherry Pick Cherry Pick Completed

Using Command Line

Check the current branch status and note the commit IDs:

Branch Status

If the current branch’s latest commit is 52a310a7, and you only want to cherry-pick 2f01e37, enter the command git cherry-pick 2f01e37:

1
2
3
4
5
git cherry-pick 2f01e37

[CP 75dc0b3] 修正 file.txt
 Date: Thu Jun 20 13:19:09 2024 +0800
 1 file changed, 1 insertion(+), 1 deletion(-)

Similarly, to cherry-pick a range of commits, remember the start and end commit IDs, and enter:

1
git cherry-pick start^..end

For example, to cherry-pick commits between 5d59 and b03a (inclusive):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
git cherry-pick 5d59^..b03a

[CP 6b21176] 新增 b.txt
 Date: Thu Jun 20 13:15:02 2024 +0800
 1 file changed, 1 insertion(+)
 create mode 100644 b.txt
[CP 0b37faa] 新增 c.txt
 Date: Thu Jun 20 13:15:08 2024 +0800
 1 file changed, 1 insertion(+)
 create mode 100644 c.txt

After executing the above, the final branch status is:

Final Branch Status

If conflicts arise, you must resolve them manually or abort the cherry-pick process using git cherry-pick --abort.

Cherry-Picking a Specific File

In Git, the cherry-pick command can only apply entire commits, not specific file changes. However, if you want to apply changes from a specific file in a commit or branch, you can use the checkout command to achieve this.

1
2
3
4
# checkout a specific file from a branch
git checkout BRANCH_NAME FILE_PATH
# or checkout a specific file from a commit
git checkout COMMIT_SHA FILE_PATH

As shown in the image below, the current branch CP is at commit ID 52a310a7. We can use the checkout command to retrieve the file.txt from the commit ID d616aa83 (WITH_ANOTHER_FIX_ALL):

WITH_ANOTHER_FIX_ALL contains multiple file changes

1
2
3
git checkout d616 file.txt

Updated 1 path from 8ddf008

The file will be in the staging area, and you can commit it using VS Code:

Commit after entering message

The final branch status is shown in the image below, where the commit ID 223cf241 with the message File.txt_fixed includes both the “Fix file.txt” and “WITH_ANOTHER_FIX_ALL” changes. This is a common technique in simplifying and merging commits, allowing for more effective management and integration of changes within the codebase by consolidating changes from different commits into one file.

Final Branch Status

Conclusion

Using the cherry-pick command, we can copy commits from one branch to another. However, frequent cherry-picking can complicate the branch structure, making it harder to maintain.

Using rebase or merge operations can simplify branch complexity. Particularly, in cases with multiple file changes, merges are more likely to encounter conflicts, which can be challenging for newcomers to resolve.

References

  1. Git - git-cherry-pick Documentation
Theme Stack designed by Jimmy