The git reset --hard
means git reset --hard HEAD
where HEAD is a ref (reference) to the currently checked out commit or the last commit in your current branch, i.e. don't change the branch but get rid of all local changes so it will remove all the staged and unstaged changes. The git reset
command provides a lot of options which is explained here very well:
Can you explain what "git reset" does in plain english?
Whereas Cherry picking means to choose a commit from one branch and apply it onto another. It can be done if you eg. made a mistake and committed a change into wrong branch, but do not want to merge the whole branch. You can just eg. revert the commit and cherry-pick it on another branch.
If in case you push to a wrong branch or you have issues with your branch which is showing a lot of commits from other users (when multiple users are working) due to old git history on your system you can delete the current branch keeping your commit hashes safe and then cherry-pick the commits to a new branch which will be updated from master. (This case happens sometimes and cherry-pick
is an option when rebase
or merge
is not solving the issue)
The quote taken from this answer:
Source: https://stackoverflow.com/a/30218784/4207394
Using git cherry-pick The command git cherry-pick commit applies the
changes introduced by the named commit on the current branch. It will
introduce a new, distinct commit. Strictly speaking, using git
cherry-pick doesn’t alter the existing history within a repository;
instead, it adds to the history. As with other Git operations that
introduce changes via the process of applying a diff, you may need to
resolve conflicts to fully apply the changes from the given commit .
The command git cherry-pick is typically used to introduce particular
commits from one branch within a repository onto a different branch. A
common use is to forward- or back-port commits from a maintenance
branch to a development branch.
So whenever you want to reset a commit or your changes you can use git reset
and event if you want to reset the commit and keep the changes safe of that commit you can use git reset
with --soft
option. And if you want to copy a commit from some other branch to some other then you can use git cherry-pick
. The git reset
has many options available for example:
To reset to the current HEAD
:
git reset --hard HEAD
To reset one commit down and removing the changes:
git reset --hard HEAD~1
this 1 can be n to remove any number of commits. And also to preserve the changes of the commits you can use --soft
instead of --hard
.
For cherry-pick
the syntax is:
git cherry-pick <commit-hash>