Advanced Git Commands – Reset, Revert, Rebase and Cherry-Pick
Imagine your team is developing a banking application. You have committed a change to the loan repayment module, but later discover that the change introduced an incorrect calculation. Another developer has already started working on the same feature, and your branch contains several commits that need to be reorganised.
Simply deleting files or creating another commit may not be the best solution. Git provides advanced commands that allow you to undo changes, recover from mistakes, reorganise commit history, and selectively move changes between branches.
Four commands are especially important for developers working with real-world repositories:
- git reset – Move the current branch pointer and optionally change the staging area and working directory.
- git revert – Create a new commit that reverses the changes introduced by an earlier commit.
- git rebase – Reapply commits on top of another base commit to reorganise history.
- git cherry-pick – Apply the changes introduced by selected commits onto the current branch.
1. Understanding the Problem: Why Do We Need Advanced Git Commands?
In a typical development workflow, developers create commits, work on feature branches, merge changes, and collaborate through pull requests. However, development does not always go as planned.
Consider these situations:
- You committed code but have not pushed it. You want to remove the last commit while keeping your changes.
- You pushed an incorrect commit to a shared branch and need to undo it safely.
- Your feature branch is behind the latest development branch and needs a cleaner commit history.
- A bug fix exists in another branch, but you need only that particular commit.
Each situation requires a different operation. Choosing the wrong command can discard work or rewrite history that other developers are already using.
2. Understanding Git Commit History
Before learning the commands, understand how Git represents project history. A Git commit is a snapshot of the project and contains metadata such as its author, timestamp, message, and parent commit reference.
Git branches are movable references to commits. The special reference HEAD identifies the commit currently checked out. In a normal branch checkout, HEAD points to the current branch, and that branch points to a commit.
Diagram 1: A simplified Git commit history with the current branch and HEAD at the latest commit.
For the examples in this article, assume the repository contains a banking application and the current branch is feature/loan-calculation.
Example: Inspect the current repository history
git status
git log --oneline --graph --decorate --all
Sample output
* d4e5f6a (HEAD -> feature/loan-calculation) Update repayment calculation * c3d4e5f Add loan validation * b2c3d4e Create loan service * a1b2c3d Initial project setup
The output shows the latest commit, its parent commits, and the branch currently checked out.
3. Git Reset – Move the Branch Pointer
git reset is used to move the current branch pointer to another commit. Depending on the selected mode, it can also update the staging area and working directory.
It is commonly used when you want to undo local commits, unstage files, or remove recent changes from your current branch.
3.1 Understanding the Three Reset Modes
Git provides three commonly used reset modes:
| Mode | Branch / HEAD | Staging area | Working directory |
|---|---|---|---|
| --soft | Moves to target commit | Preserved | Preserved |
| --mixed | Moves to target commit | Reset to target commit | Preserved |
| --hard | Moves to target commit | Reset to target commit | Updated to target commit; tracked local changes can be discarded |
3.2 Git Reset – Soft
Suppose you committed three files together, but now you want to modify the commit message or adjust the contents before creating the commit again.
Use --soft to move the branch pointer backwards while keeping the changes staged.
Command
git reset --soft HEAD~1
Check the status
git status
Expected output example
On branch feature/loan-calculation Changes to be committed: modified: LoanService.cs modified: RepaymentCalculator.cs
The previous commit is no longer the branch tip, but its changes remain staged and ready for another commit.
git reset --soft when you want to combine recent commits, adjust a commit, or recommit already staged changes without losing them.3.3 Git Reset – Mixed
--mixed is the default reset mode. It moves the branch pointer and resets the staging area, but keeps the changes in the working directory.
Command
git reset --mixed HEAD~1
# Equivalent command
git reset HEAD~1
Expected output example
Unstaged changes after reset: M LoanService.cs M RepaymentCalculator.cs
The files are still modified, but they are no longer staged.
3.4 Git Reset – Hard
--hard moves the branch pointer and resets both the staging area and working directory to match the target commit.
Command
git reset --hard HEAD~1
Expected output example
HEAD is now at c3d4e5f Add loan validation
The current branch now points to the previous commit. Tracked changes introduced after that commit are removed from the working directory.
git reset --hard can permanently discard uncommitted tracked-file changes. It is not a routine cleanup command. Check git status and save any important work before running it.3.5 Understanding HEAD~1 and HEAD~n
Git provides relative commit references to identify earlier commits.
HEAD # Current commit
HEAD~1 # First parent of current commit
HEAD~2 # First parent, two generations back
HEAD~3 # First parent, three generations back
For a normal linear history, HEAD~2 refers to the commit two steps behind the current commit. For histories containing merges, remember that ~n follows first-parent links.
4. Git Revert – Undo Changes Without Rewriting History
Imagine that an incorrect repayment calculation has already been pushed to the shared development branch. Other developers have pulled the commit and are building their work on top of it.
Using reset to remove that published commit would rewrite the shared branch history. This can create problems for everyone who has already downloaded the original history.
In this situation, git revert is generally the safer choice.
4.1 How Git Revert Works
Git revert creates a new commit that applies the inverse of the changes introduced by an earlier commit. The original commit remains in the repository history.
Command
git revert d4e5f6a
Expected output example
[development 7a8b9c0] Revert "Update repayment calculation" 2 files changed, 8 insertions(+), 12 deletions(-)
Git creates a new commit that reverses the selected commit's changes. The exact output depends on the repository and the changes being reverted.
4.2 Visualising Git Revert
Diagram 2: Revert preserves the original commit and adds a new commit that reverses its changes.
4.3 Revert Multiple Commits
You can revert more than one commit, but the order matters because later commits may depend on earlier changes.
# Revert a specific commit
git revert a1b2c3d
# Revert several commits individually
git revert a1b2c3d b2c3d4e
# Revert without immediately creating a commit
git revert --no-commit a1b2c3d
With --no-commit, Git applies the inverse changes to your working tree and index without creating the revert commit immediately. You can inspect the combined result and commit it when ready.
4.4 Reverting a Merge Commit
Reverting a merge commit requires additional care because a merge commit has more than one parent. Git needs to know which parent should be treated as the mainline.
git revert -m 1 <merge-commit-hash>
The -m 1 option selects the first parent as the mainline. It does not mean "revert one commit". Confirm the parent order and intended result before using this command.
5. Git Rebase – Reorganise Commit History
Suppose you are working on a feature branch while the development branch receives new commits. You want your feature branch to include the latest development changes and maintain a clean, linear history.
git rebase moves or reapplies commits from your current branch on top of another base commit.
5.1 Understanding Rebase with an Example
Assume the development branch has received two new commits while your feature branch contains two independent commits.
Diagram 3: Conceptual view of feature commits being reapplied on top of the updated base. A second feature commit would follow F1' in the resulting history.
Before rebase, the feature branch has its own line of development. After rebase, Git creates new commits containing the feature changes on top of the updated base.
5.2 Rebase Command
First, update your local development branch and then rebase the feature branch onto it.
# Switch to development
git switch development
# Fetch the latest remote changes
git fetch origin
# Update local development branch
git pull --ff-only origin development
# Switch back to feature branch
git switch feature/loan-calculation
# Rebase feature commits onto development
git rebase development
Expected output example
Successfully rebased and updated refs/heads/feature/loan-calculation.
The feature commits are now based on the latest local development branch.
5.3 What Happens Internally During Rebase?
- Git identifies the common ancestor between the current branch and the target base.
- Git identifies the commits on the current branch that need to be replayed.
- Git checks out the target base commit.
- Git reapplies the selected changes one by one as new commits.
- Git updates the current branch to point to the newly created commit history.
5.4 Interactive Rebase
Interactive rebase is useful when you want to clean up recent local commits before sharing your branch. You can reorder commits, edit commit messages, combine commits, or remove commits from the rewritten history.
git rebase -i HEAD~3
Git opens an editor with a list of the selected commits. A simplified example is:
pick a1b2c3d Add loan entity
pick b2c3d4e Add repayment validation
pick c3d4e5f Fix validation condition
Common interactive rebase instructions:
| Instruction | Purpose |
|---|---|
pick |
Keep the commit as it is. |
reword |
Keep the changes but edit the commit message. |
edit |
Pause to amend or modify the commit. |
squash |
Combine the commit with the previous commit and edit the combined message. |
fixup |
Combine the commit with the previous commit, discarding its message. |
drop |
Remove the commit from the rebased history. |
For example, to combine the last two commits into one, you can mark the second commit as squash or fixup.
6. Git Cherry-Pick – Apply a Specific Commit
Imagine a developer has fixed a critical interest calculation bug in the development branch. Your release branch also needs that fix, but it is not ready to receive all the other development changes.
You can use git cherry-pick to apply the changes introduced by the required commit to your current branch.
6.1 Basic Cherry-Pick
First, identify the commit hash that contains the required fix.
git log --oneline development
Suppose the required commit is e5f6a7b. Switch to the release branch and cherry-pick it.
git switch release/2026.11
git cherry-pick e5f6a7b
Expected output example
[release/2026.11 9a8b7c6] Fix interest calculation rounding 1 file changed, 5 insertions(+), 2 deletions(-)
Git applies the selected changes and creates a new commit on the current branch. The new commit has a different hash from the original commit.
6.2 Cherry-Pick Multiple Commits
# Apply two selected commits
git cherry-pick a1b2c3d c3d4e5f
# Apply a continuous range of commits
git cherry-pick a1b2c3d^..c3d4e5f
The range syntax above includes the commit a1b2c3d and the commit c3d4e5f, along with commits between them in the selected history.
6.3 Cherry-Pick Without Automatically Creating a Commit
Sometimes you want to apply the changes but review them before committing.
git cherry-pick --no-commit e5f6a7b
Git applies the changes to your index and working tree without creating the commit immediately. You can review and stage the final result before committing.
6.4 Visualising Cherry-Pick
Diagram 4: Cherry-pick applies the selected change to another branch as a new commit.
7. Handling Conflicts in Advanced Git Operations
Reset, revert, rebase, and cherry-pick can all involve situations where Git cannot apply changes automatically. This happens when the operation encounters conflicting edits or incompatible changes.
For example, one developer changes the repayment calculation in a feature branch, while another developer modifies the same lines in the development branch.
7.1 Identify the Conflict
During a rebase or cherry-pick, Git may stop and report that a file has conflicts.
CONFLICT (content): Merge conflict in RepaymentCalculator.cs error: could not apply e5f6a7b... Update repayment calculation
Open the affected file. Git marks the conflicting sections using special markers:
<<<<<<< HEAD
decimal interest = principal * rate / 100;
=======
decimal interest = principal * rate / 1200;
>>>>>>> e5f6a7b
The section above the separator represents the current side, and the section below represents the incoming change. The correct resolution depends on the business requirement; do not blindly choose one side.
7.2 Resolve and Continue a Rebase
# Edit the conflicting file and remove conflict markers
# Stage the resolved file
git add RepaymentCalculator.cs
# Continue the rebase
git rebase --continue
To stop the rebase and return to the state before the rebase began:
git rebase --abort
7.3 Resolve a Cherry-Pick Conflict
# Resolve the conflicting file
git add RepaymentCalculator.cs
# Continue cherry-picking
git cherry-pick --continue
To cancel the cherry-pick operation:
git cherry-pick --abort
For a revert conflict, resolve and stage the affected files, then complete the revert using the appropriate commit command or continue command indicated by Git. You can also cancel the operation with git revert --abort.
8. Git Reset vs Revert vs Rebase vs Cherry-Pick
These commands may look similar because they all affect Git history or project changes. However, they solve different problems.
| Command | Main purpose | Creates new commit? | Rewrites existing branch history? |
|---|---|---|---|
git reset |
Move branch pointer and optionally reset index and working tree | No | Yes, when moving a branch backwards or elsewhere |
git revert |
Undo the effect of an earlier commit | Yes, by default | No; it preserves existing commits |
git rebase |
Reapply commits onto a new base | Creates new commit objects for replayed commits | Yes |
git cherry-pick |
Apply selected commit changes to the current branch | Yes, by default | Does not rewrite the source branch |
8.1 Which Command Should You Choose?
| Scenario | Recommended command | Reason |
|---|---|---|
| Remove the latest local commit but keep changes staged | git reset --soft |
Moves branch pointer and preserves staged changes. |
| Unstage files but keep modifications | git reset |
Resets the index without discarding working-tree changes. |
| Undo a commit already shared with the team | git revert |
Adds a new commit instead of rewriting shared history. |
| Update a feature branch with a cleaner linear history | git rebase |
Replays feature commits on top of the latest base. |
| Move one bug fix to a release branch | git cherry-pick |
Applies only the selected commit's changes. |
| Combine several local commits before a pull request | git rebase -i |
Allows commits to be squashed or reorganised. |
9. Recovering from Mistakes Using Git Reflog
What happens if you accidentally run git reset --hard and move your branch pointer to the wrong commit?
In many cases, Git's reflog can help you locate the previous position of HEAD and recover a commit that is no longer reachable from the current branch tip.
9.1 Inspect the Reflog
git reflog
Sample output
d4e5f6a HEAD@{0}: reset: moving to HEAD~1
7a8b9c0 HEAD@{1}: commit: Update repayment calculation
c3d4e5f HEAD@{2}: commit: Add loan validation
b2c3d4e HEAD@{3}: commit: Create loan service
Here, the earlier commit 7a8b9c0 is still visible in the local reflog.
9.2 Restore the Previous Commit Safely
Before resetting again, consider creating a recovery branch from the commit you want to preserve.
# Create a recovery branch at the previous commit
git branch recovery/loan-calculation 7a8b9c0
# Switch to the recovery branch
git switch recovery/loan-calculation
10. Common Mistakes and Best Practices
10.1 Common Mistakes
- Using hard reset on shared branches: This can remove commits from the branch history other developers are using.
- Rebasing published commits without coordination: Rewriting shared history can create divergent histories and collaboration problems.
- Cherry-picking without checking dependencies: A selected commit may depend on changes from earlier commits that are not present on the target branch.
- Reverting a merge without understanding the mainline: Selecting the wrong parent can produce an unexpected inverse change.
- Ignoring conflicts: Removing conflict markers without validating the resulting business logic can introduce defects.
- Assuming reflog is a backup: Reflog is useful for local recovery, but it should not replace remote backups or disciplined branching.
10.2 Recommended Best Practices
- Run
git statusbefore destructive or history-changing operations. - Use
git log --oneline --graph --decorate --allto inspect commit history. - Prefer revert for undoing changes already published to a shared branch.
- Use interactive rebase to clean up local commits before sharing a feature branch.
- Do not rewrite commits that other developers depend on unless the team has agreed on the workflow.
- Review cherry-picked changes for missing dependencies and environment-specific assumptions.
- Run unit tests and relevant integration tests after resolving conflicts.
- Create a backup branch before performing risky history changes when the original work must be preserved.
11. Real-World Team Workflow
Consider a team working on a banking application with three branches:
development– Shared integration branch.feature/loan-calculation– Feature development branch.release/2026.11– Release preparation branch.
A practical workflow might look like this:
Fetch the latest changes and rebase your local feature commits onto the updated development branch, if your team uses a rebase-based workflow.
git fetch origin
git switch development
git pull --ff-only origin development
git switch feature/loan-calculation
git rebase development
Use interactive rebase to combine temporary commits and improve commit messages before opening a pull request.
git rebase -i HEAD~3
If an incorrect change has already reached the shared development branch, create a revert commit instead of resetting the shared branch.
git switch development
git pull --ff-only origin development
git revert <incorrect-commit-hash>
git push origin development
Identify the specific bug-fix commit and cherry-pick it into the release branch after checking its dependencies.
git switch release/2026.11
git cherry-pick <bug-fix-commit-hash>
git push origin release/2026.11
12. Interview Preparation
Q1. What is the difference between git reset and git revert?
Answer: Reset moves the current branch pointer and may also change the index and working tree. Revert creates a new commit that reverses the changes introduced by an earlier commit. Revert is generally preferred for undoing published changes on shared branches.
Q2. What is the difference between git reset --soft and git reset --mixed?
Answer: Both move the branch pointer. Soft reset keeps changes staged, whereas mixed reset resets the staging area but keeps the modifications in the working directory.
Q3. What happens when you execute git reset --hard HEAD~1?
Answer: Git moves the current branch pointer to the previous commit and updates the index and working directory to match that commit. Uncommitted tracked-file changes can be discarded.
Q4. Why is git revert safer than git reset for shared branches?
Answer: Revert preserves the existing commit history and adds a new commit that reverses an earlier change. Reset can move the branch pointer and remove commits from the visible branch history, potentially disrupting collaborators.
Q5. What is the difference between merge and rebase?
Answer: Merge combines branch histories and normally creates a merge commit when a fast-forward is not possible. Rebase reapplies commits on a new base and creates new commit objects, generally producing a linear history.
Q6. Does rebase change commit hashes?
Answer: Yes. Replayed commits are new commit objects with different parent references and therefore different commit hashes. Their author and committer metadata may also differ.
Q7. What is interactive rebase?
Answer: Interactive rebase allows developers to reorder, reword, edit, squash, fix up, or drop commits before the resulting history is finalised.
Q8. What is cherry-pick used for?
Answer: Cherry-pick applies the changes introduced by selected commits onto the current branch. It is useful when a specific fix needs to be moved to another branch without merging all changes from the source branch.
Q9. Can cherry-pick create conflicts?
Answer: Yes. If the changes cannot be applied cleanly, Git pauses the operation. Resolve the conflicts, stage the files, and run git cherry-pick --continue, or cancel using git cherry-pick --abort.
Q10. What is Git reflog?
Answer: Reflog records local updates to references such as HEAD and branch tips. It can help locate commits after operations such as reset or rebase, provided the relevant reflog entries and objects are still available.
Q11. What does git rebase --abort do?
Answer: It stops an in-progress rebase and restores the branch to its state before the rebase started.
Q12. Can you cherry-pick a merge commit?
Answer: Yes, but a merge commit has multiple parents. You generally need to specify the mainline parent using the -m option, after understanding which parent should be treated as the mainline.
13. Hands-On Practice
Use a local test repository to practise these commands. Avoid experimenting with destructive reset operations in a production repository.
Exercise 1 – Practise Soft, Mixed and Hard Reset
Objective: Understand how each reset mode affects the branch pointer, staging area, and working directory.
mkdir git-advanced-practice
cd git-advanced-practice
git init
echo "Initial loan service" > loan.txt
git add loan.txt
git commit -m "Initial commit"
echo "Add loan validation" >> loan.txt
git add loan.txt
git commit -m "Add loan validation"
echo "Add repayment logic" >> loan.txt
git add loan.txt
git commit -m "Add repayment logic"
git log --oneline
Now practise the following commands one at a time, inspecting git status after each operation. Reset the repository to a known state between experiments.
git reset --soft HEAD~1
git status
# Restore the test branch to its saved state before
# trying the next mode, if necessary.
git reset --mixed HEAD~1
git status
For hard reset, ensure there are no uncommitted changes you need to keep.
Exercise 2 – Practise Revert
Objective: Undo a commit while preserving history.
git log --oneline
git revert HEAD
git log --oneline
Observe that Git creates a new commit rather than removing the original commit.
Exercise 3 – Practise Rebase
Objective: Reapply feature commits on top of an updated base branch.
git switch -c feature/repayment
echo "Repayment feature" > repayment.txt
git add repayment.txt
git commit -m "Add repayment feature"
git switch master
echo "Update documentation" > README.md
git add README.md
git commit -m "Update documentation"
git switch feature/repayment
git rebase master
If your initial branch is named main rather than master, replace the branch name in the commands.
Exercise 4 – Practise Cherry-Pick
Objective: Apply one commit from another branch.
git switch -c feature/bug-fix
echo "Fix interest calculation" > interest-fix.txt
git add interest-fix.txt
git commit -m "Fix interest calculation"
# Copy the commit hash from git log
git log --oneline
# Switch to the target branch
git switch master
# Apply only the selected commit
git cherry-pick <commit-hash>
Compare the commit histories of both branches and observe that the target branch receives a new commit containing the selected change.
14. Conclusion
Advanced Git commands are essential tools for developers who work with multiple branches, shared repositories, production releases, and collaborative development workflows.
The most important thing is not memorising commands. It is understanding what each command changes and how that affects the repository and other developers.
- Use reset to move the branch pointer and manage local staged or unstaged changes.
- Use revert to safely reverse published changes while preserving history.
- Use rebase to replay commits on a new base and organise commit history.
- Use cherry-pick to apply selected commits to another branch.
- Use reflog to investigate and recover from many local reference mistakes.
With regular practice, these commands become part of your everyday development workflow and help you manage complex Git histories with confidence.
15. Official Documentation and Further Learning
- Git Documentation – git reset
- Git Documentation – git revert
- Git Documentation – git rebase
- Git Documentation – git cherry-pick
- Git Documentation – git reflog
- Pro Git Book – Official Free Git Book
Upgradix by Shrash Studio – Learn Technology. Build Your Future.