Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Difference Between Git Cherry-Pick and Git Revert? [closed]

I still find the behavior of git revert somewhat confusing. After significant pain and misunderstanding, I learnt that git revert negates a particular commit rather than reverting to that commit. I have not used git cherry-pick so far.

Can you elaborate on each of these two git commands? When and how do you prefer to use them?

like image 512
haziz Avatar asked Oct 26 '12 11:10

haziz


People also ask

What is git cherry-pick used for?

git cherry-pick is a powerful command that enables arbitrary Git commits to be picked by reference and appended to the current working HEAD. Cherry picking is the act of picking a commit from a branch and applying it to another. git cherry-pick can be useful for undoing changes.

What is the difference between git checkout and git cherry-pick?

Besides being used for different tasks, the most significant difference is that git-cherry-pick changes the history of a branch, while git checkout is being used only to jump to a specific point in time (a specific commit) in the branch's history, without changing it.

How do I revert back from cherry-pick?

A cherry-pick is basically a commit, so if you want to undo it, you just undo the commit. Stash your current changes so you can reapply them after resetting the commit.

What is the difference between cherry-pick and merge?

With the cherry-pick command, Git lets you incorporate selected individual commits from any branch into your current Git HEAD branch. When performing a git merge or git rebase , all the commits from a branch are combined.


1 Answers

git cherry-pick is like "Convert the specified commit into a patch and apply this patch here".

git revert is like "Convert the specified commit into a patch, 'invert' this patch (like in patch -R) and apply it here".

Both commands can lead to conflicts.

Cherry-pick is used on unmerged (not reachable by parent links from current commit) commits. Revert is typically used on merged commits.

Undoing a revert commit requires making another revert commit (leading to commit messages like "Revert of 'Revert of ...'"), not cherry-picking reverted commit (as it is considered already merged).

like image 100
Vi. Avatar answered Sep 24 '22 01:09

Vi.