
When managing code with Git, you often work across multiple branches. Sometimes, you need a specific change from one branch but don't want to merge the entire branch's history. Git's cherry-pick command allows you to select and apply a specific commit from one branch onto another. This is different from merging or rebasing, which integrate entire branches. With cherry-pick, you precisely select only the commits you need.
This article explains how to use git cherry-pick to apply single or multiple commits, handle conflicts, and undo changes when necessary.
Cherry-picking is ideal when you need to transfer a single, specific change—such as a hotfix or a small feature—from one branch to another without merging all the other changes from that branch.
<commit-hash>: The unique identifier (SHA-1 hash) of the commit you want to apply.Identify the commit you want to cherry-pick, check out the branch that contains the commit.
Use git log to find the commit's hash.
Output:
Copy the hash of the commit you need, for example, a1b2c3d.
Switch to the target branch where you want to apply the commit.
Cherry-pick the commit using its hash.
The command above applies the changes from commit a1b2c3d to the main branch, creating a new commit with the same content and message, but a different hash.
In addition to applying a single commit, you can cherry-pick a sequence of commits or select multiple specific commits. This is useful when you need to transfer a set of changes without merging an entire branch.
To apply a series of consecutive commits from another branch, use the .. notation.
<start-commit-hash>^: The ^ symbol tells Git to start from the commit before the specified start commit.<end-commit-hash>: The last commit in the range to be applied.To apply the last two commits from feature-branch to main:
The command above applies commits starting from d4e5f6a to a1b2c3d to the main branch in the same order.
To apply multiple, non-consecutive commits, list their hashes in the order you want them applied.
To apply only the specific commits from a branch:
This command applies the changes from commits g7h8i9j and a1b2c3d to the current branch, in the specified order, while skipping the other commit.
A conflict occurs if the changes in the cherry-picked commit overlap with changes in your current branch. Git will pause the process and ask you to resolve the conflict manually.
When a conflict occurs, git status will show the conflicted files.
Output:
Open the conflicted file in a text editor. Look for the conflict markers (<<<<<<<, =======, >>>>>>>), edit the file to keep the correct code, and remove the markers.
After resolving the conflict, stage the modified file.
Continue the cherry-pick process.
If you prefer to cancel the cherry-pick and return to the previous state, run:
If you make a mistake during cherry-picking, you can undo the changes in two ways depending on whether the commit has been pushed to a remote repository.
If you have not pushed the commit to a remote repository, you can remove it from your local history using git reset.
This above command discards the most recent commit and any uncommitted changes in your working directory. Use it with caution.
If you have already pushed the cherry-picked commit, avoid rewriting history. Instead, use git revert to create a new commit that reverses the changes.
This command creates a new commit that reverts the changes introduced by the specified cherry-picked commit, preserving your history and avoiding conflicts with collaborators.
In this article, you learned how to use git cherry-pick to apply specific commits from one branch to another. You now know how to pick single or multiple commits, resolve conflicts, and undo a cherry-pick. This command gives you precise control over your repository's history, making it an essential tool for managing complex workflows like hotfixes and selective feature integration.
0 Comments
Be the first to comment and share your perspective with the community.