Skip to main content

git reset, git revert, and git cherry-pick are three Git commands used for different purposes related to managing your version control history. Here's a brief overview of each command:

git reset
git revert
git cherry-pick

 

Snapshot

git revert is to roll back to a previous version of the repo you're working on
git reset simply wipe all changes made since the last commit.  You don't want to commit the changes you have locally, however, you want to reinstate the conditions as they were when the commit was made
git cherry-pick as its name states, it's about selection. That is copy selected commits so the original commit is still present in the source branch

 

git reset

git reset is used to reset the current branch to a specific commit or state.  It is often used to undo commits or move the branch pointer to a different commit.
There are different reset options:

  • git reset --soft: Moves the branch pointer to a specific commit while keeping your changes in the staging area. This is useful for amending the previous commit.
  • git reset --mixed (default): Moves the branch pointer to a specific commit and unstages changes. Your changes are retained in your working directory but not in the staging area.
  • git reset --hard: Moves the branch pointer to a specific commit and discards all changes made after that commit. This effectively removes those commits from the branch's history.

As an example, resetting to the last commit form your local

git reset --hard

 

git revert

git revert is used to create a new commit that undoes the changes made in a previous commit.  It's a safe way to reverse changes without altering the commit history. Reverting creates a new commit that negates the changes made in a specific commit.  Useful when you want to maintain a clean commit history and avoid destructive history rewriting.

 

git cherry-pick

git cherry-pick allows you to apply a specific commit from one branch to another.  Useful when you want to take changes from one branch and apply them to another branch.  Cherry-picking can be handy for applying specific bug fixes or features to different branches.

 

In summary

Use git reset to manipulate your branch's history by moving the branch pointer to a specific commit.
Use git revert to create a new commit that undoes the changes introduced in a specific commit while preserving the commit history.
Use git cherry-pick to apply specific commits from one branch to another, helping you incorporate changes selectively.

The choice of command depends on your specific use case and the desired outcome for your Git repository's history.

Related articles

Andrew Fletcher17 Feb 2024
Update a git feature branch to the latest branch (master)
In our projects, team members frequently generate new Git branches linked to the same Jira number, resulting in cluttered and disorganised structures that can pose challenges for both current and future developers. For instance:Jira: ABC-123Ticket work: Reformat headersOver time, these branches...
Andrew Fletcher29 Sep 2023
Adding a Git repo to a server
git clone git@bitbucket.org:{username}/{repo}.gitAnd I was unceremoniously delivered the following errorfatal: could not create work tree dir '{project}': Permission deniedMy initial thought was the error due to server permission... being sudo.  This was tested by running the commandsudo git...
Andrew Fletcher26 Sep 2023
Git changing local and remote branch name
Something I haven't had to do in a while is to change the name of a Git branch both local and remote. Steps to renaming a branchRename your local branch:If you are on the branch you want to rename:git branch -m new-nameWhereas, if you're on a different branch:git branch -m old-name...