Back to Git & Dev Tools
2026-01-119 min read

Reset, checkout, and revert (Git & Dev Tools)

Learn Reset, checkout, and revert (Git & Dev Tools) step by step with clear examples and exercises.

Why This Matters

In this extensive guide, we delve into essential Git commands for developers: resetting, checking out, and reverting. These commands are crucial for managing your codebase effectively, especially during debugging and collaboration.

Understanding these Git operations is vital for several reasons:

  1. Debugging: If you make a mistake in your local repository, you can reset or revert to a previous state to fix it without losing progress.
  2. Collaboration: When working on a team, you may need to switch between branches, merge changes, or discard unwanted commits. These commands help manage these tasks efficiently.
  3. Version control: Git keeps track of every change made in your project, allowing you to revert to any previous state if needed.
  4. Real-world scenarios: Debugging and collaboration are common occurrences in software development, making these commands indispensable for professional developers.

Prerequisites

Before diving into the core concepts, ensure you have a basic understanding of:

  1. Git installation and setup
  2. Creating and managing local repositories
  3. Basic Git commands like add, commit, and push
  4. Understanding branches and merging changes
  5. Familiarity with Git's branching strategies, such as feature branches and pull requests
  6. Knowledge of common Git workflows, such as Gitflow or Github Flow
  7. Understanding the concept of staging area (index) and working directory
  8. Familiarity with Git tags for marking specific commits
  9. Basic understanding of merge conflicts and how to resolve them
  10. Knowledge of Git aliases for simplifying common commands

Core Concept

Resetting

Resetting a Git repository allows you to move the current HEAD (the latest commit) to a different commit or even create a new one. There are three types of reset commands: soft, mixed, and hard.

Soft Reset (git reset --soft )

A soft reset keeps all changes in the working directory and staging area but moves the HEAD to the specified commit. This command is useful when you want to undo a specific commit without losing local changes.

$ git reset --soft <commit-hash>

Mixed Reset (git reset )

A mixed reset moves the HEAD and discards changes in the staging area but keeps local modifications in the working directory. This command is useful when you want to undo a commit and stage some changes for a new commit.

$ git reset <commit-hash>

Hard Reset (git reset --hard )

A hard reset moves the HEAD, discards changes in both the staging area and working directory. This command is useful when you want to completely undo a commit and revert to the previous state.

$ git reset --hard <commit-hash>

Limitations of Reset Commands

  1. Permanent changes: Once you perform a hard or mixed reset, the discarded changes are lost forever unless they were previously committed.
  2. Remote tracking branches: Hard resets can cause issues when working with remote repositories, as they may overwrite commits that haven't been pushed yet. It is recommended to use git push --force after a hard reset if you want to update the remote repository.
  3. Protected branches: Some Git hosting services, like GitHub, protect certain branches (e.g., master or main) from accidental hard resets. In such cases, you'll need to force-push the changes with git push --force.
  4. Unintended consequences: Hard and mixed resets can lead to unintended consequences if used incorrectly, such as losing local modifications or discarding commits that were intended to be kept. Always double-check your commands before executing them.

Checking Out

Checking out allows you to switch between branches or specific commits in your repository.

Switching Branches (git checkout )

Switching branches brings the HEAD of the specified branch into your working directory, allowing you to work on that branch's changes.

$ git checkout <branch-name>

Creating a New Branch (git checkout -b )

Creating and checking out a new branch at the same time allows you to start working on a new feature or bug fix without affecting the main branch.

$ git checkout -b <new-branch-name>

Switching Between Commits (git checkout )

You can also use git checkout to switch between specific commits, even if they are not part of any branch. This command is useful for cherry-picking changes from one commit and applying them to another branch or new commit.

$ git checkout <commit-hash>

Creating a Branch at a Specific Commit (git checkout -b )

Creating a new branch at a specific commit allows you to start working on changes made in that commit without affecting other branches.

$ git checkout -b <new-branch-name> <commit-hash>

Reverting

Reverting allows you to undo specific changes in a commit by creating a new commit that undoes those changes. This command is useful when you want to revert a commit without affecting the rest of the commit history.

Reverting a Commit (git revert )

Reverting a commit creates a new commit that undoes the changes made in the specified commit. This command is useful when you want to revert a commit that introduces a bug or unwanted changes.

$ git revert <commit-hash>

Interactive Rebasing (git rebase -i )

Interactive rebasing allows you to selectively rewrite the commit history of a branch, squash commits, or edit commit messages. This command is useful for cleaning up your Git history and making it more maintainable.

$ git rebase -i <branch>

Worked Example

Suppose you have a repository with three commits: A, B, and C, and you are currently on commit B. If you want to undo the changes made in commit B while keeping your local modifications, you can use a soft reset as follows:

$ git checkout <commit-hash-of-A> # Go to commit A
$ git reset --soft <commit-hash-of-B> # Move the HEAD to commit B and keep local changes

Now, you can continue working with the local changes from commit B but have the HEAD pointing to commit A. If you want to complete the revert process, you can create a new commit that undoes the changes made in commit B:

$ git add . # Stage all changes for the new commit
$ git commit --amend # Create a new commit that undoes the changes made in commit B

Common Mistakes

  1. Forgotten arguments: Forgetting the `, `, or other required arguments will result in errors. Always double-check your commands before executing them.
  2. Mixed reset with uncommitted changes: If you perform a mixed reset with uncommitted changes, those changes will be discarded. Make sure to commit any local modifications before running a mixed or hard reset.
  3. Hard reset with remote tracking branches: A hard reset on a branch that is being tracked by a remote repository can result in data loss if you don't have the necessary protection enabled. Be cautious when using hard resets with remote branches.
  4. Reverting without a commit message: When reverting a commit, always include a meaningful commit message to explain why the changes were reverted.
  5. Reverting multiple commits: If you want to revert multiple commits, use an interactive rebase instead of repeatedly running git revert.
  6. Conflicts during merging: When merging branches, conflicts may arise due to overlapping changes. Use the git merge --no-commit command to manually resolve these conflicts before completing the merge.
  7. Mixed and hard resets with detached HEAD: Performing a mixed or hard reset can result in a detached HEAD state. To return to a regular branch, use the git checkout command.
  8. Forgotten branches: If you create a new branch but forget to push it to the remote repository, you may lose your changes if you delete the local branch or perform a hard reset. Always push your branches after creating them.
  9. Incorrect use of git merge and git rebase: Using git merge when you should have used git rebase, or vice versa, can lead to confusing and difficult-to-resolve merge conflicts. Familiarize yourself with both commands and choose the appropriate one based on your needs.
  10. Ignoring Git hooks: Git hooks are scripts that run automatically during certain Git events, such as commit or push. Ignoring these hooks can lead to issues with your workflow, such as failing to enforce coding standards or perform automated tests. Make sure to set up and configure appropriate Git hooks for your project.
  11. Unintended consequences of git reset: Be aware that using git reset can have unintended consequences, such as losing local modifications or discarding commits that were intended to be kept. Always double-check your commands before executing them.
  12. Not understanding the difference between hard and soft resets: Hard resets discard both staged and unstaged changes, while soft resets keep all changes in the working directory and staging area. Be sure to choose the appropriate reset command based on your needs.
  13. Not committing before merging: Always commit your local modifications before merging branches to avoid losing any changes during the merge process.
  14. Not cleaning up after interactive rebasing: After an interactive rebase, make sure to force-push the updated branch to the remote repository with git push --force.
  15. Not using Git stash for temporary storage: If you have uncommitted changes that you want to work on later but don't want to commit yet, use Git stash instead of resetting or checking out a different branch.

Practice Questions

  1. You are working on a feature branch and want to revert the last commit. What command should you run?
  • git revert HEAD
  • git reset --hard HEAD~1
  • git reset --soft HEAD~1
  • git checkout HEAD~1
  1. You have made some changes that are not yet committed, and you want to switch to a different branch. What should you do before switching branches?
  • Commit your changes first
  • Discard your changes before switching branches
  • Switch branches regardless of uncommitted changes
  • None of the above
  1. You have accidentally hard reset your local repository and lost some commits. How can you recover those lost commits?
  • Use Git reflog to find the lost commit hash and checkout the commit
  • Use Git stash to save your uncommitted changes and then use git reset --hard to go back to the last good commit
  • Push your local repository to a remote repository, create a new branch from the remote repository, and merge it with your local branch
  • None of the above
  1. You have created a new feature branch but forgot to push it to the remote repository. What happens if you delete the local branch?
  • The remote branch will also be deleted
  • The remote branch will remain intact
  • The local changes will be lost forever
  • None of the above
  1. You are working on a feature branch and want to switch to another branch, but you don't want to lose your uncommitted changes. What should you do?
  • Commit your changes first and then switch branches
  • Use Git stash to save your uncommitted changes before switching branches
  • Switch branches regardless of uncommitted changes
  • None of the above

FAQ

What is the difference between git reset --soft, git reset --mixed, and git reset --hard?

  • These commands allow you to move the HEAD to a different commit or create a new one, with varying levels of discarding changes in the working directory and staging area. Soft resets keep all changes, mixed resets discard changes in the staging area but keep local modifications, and hard resets discard both staged and unstaged changes.

What is the purpose of git checkout?

  • git checkout allows you to switch between branches or specific commits in your repository, as well as create new branches. It brings the HEAD of the specified branch into your working directory, allowing you to work on that branch's changes.

What is the purpose of git revert?

  • git revert allows you to undo specific changes in a commit by creating a new commit that undoes those changes. This command is useful when you want to revert a commit that introduces a bug or unwanted changes without affecting the rest of the commit history.

4

Reset, checkout, and revert (Git & Dev Tools) | Git & Dev Tools | XQA Learn