Photo by Jim Wilson on Unsplash
Git Reset, Revert, and Cherry-Pick: Tools for Efficient Branch Management
Essentials for Safeguarding and Maintaining Your Sprint Cycles
At work, we follow the agile methodology and complete tasks in two-week sprints. Our team uses Git flow branching to manage the codebase, with developers assigned feature tickets and creating branches to work on their respective tasks. Once complete, features are merged into the development branch and then into release branches created at the end of each sprint.
To ensure the quality of new releases, I often use Git's reset, revert, and cherry-pick commands to remove or relocate features efficiently upon request.
Remove a feature from the dev branch.
Using git reset to remove commits.
Suppose we need to remove an unstable feature, f2
, from the development branch. This feature has three commits, f2/1
, f2/2
, and f2/3
, which need to be removed. We can use the git reset
command to remove these commits since they are located at the end of the branch.
git reset --hard c3
git reset --hard c3
moves the HEAD
pointer to commit c3
, effectively removing feature commits f2/1
, f2/2
, and f2/3
from the branch history. Caution is advised as this command will also erase any changes made in these commits. The branch must be updated on the remote repository with force push after resetting it locally.
Using git revert to remove commits.
The git revert
command is a useful tool when you need to remove old commits from a branch with new commits from other features. For example, let's say we want to remove the feature f2
with commits f2/1
, f2/2
, and f2/3
from the branch, but other features have new commits added to the same branch.
A revert operation will take the specified commit, inverse the changes from that commit, and create a new "revert commit". The ref pointers are then updated to point at the new revert commit, making it the tip of the branch.
git revert f2/1
git revert f2/2
git revert f2/3
Executing these 3 git revert commands will create 3 new revert commits, namely f2/r1
, f2/r2
, and f2/r3
at the end of the branch.
The git reset
is powerful but risky because it rewrites history. The git revert
, on the other hand, creates a new commit that undoes the changes, leaving the original changes intact in the history.
Cherry-picking specific features from the dev to the release branch.
If the sprint is still in progress but the team requires certain development features to be added to the staging/release branch, the git cherry-pick
command can be utilized.
To move the feature f2
to the staging branch, you can begin by checking out the staging branch, and then using the git cherry-pick command to apply the 3 commits from f2
onto the staging branch.
git checkout stage
git cherry-pick f2/1
git cherry-pick f2/2
git cherry-pick f2/3
When using the git cherry-pick
command to move a feature from the development branch to the release branch, three new cherry-picked commits will be added to the release branch. These commits will be exact copies of the original commits from the development branch.
These three git tools have been invaluable in my daily work. However, it's important to use them with care and understanding. I hope that this information has been helpful to you as well.
Thanks for taking the time to read this article! Don't forget to share this article with your colleagues if you think they could benefit from it too!