Skip to Main Content






Git Basics

Common Pitfalls and Troubleshooting

Even with careful use, it's easy to run into issues when working with Git. Understanding how to revert changes and undo commits can help you recover from mistakes and keep your repository clean.

Understanding how to revert changes and undo commits is essential for maintaining a clean and stable codebase. These commands give you the flexibility to correct mistakes and refine your commits before sharing them with others, helping you avoid common pitfalls in version control.

Reverting Changes

  • Purpose:

Reverting changes allows you to undo the effects of a commit by creating a new commit that reverses the changes. This is a safe way to undo changes in a shared repository because it preserves the history.

  • Using git revert:

    • This command is used to reverse the changes introduced by a specific commit. It creates a new commit with the opposite effect of the target commit.

    • Usage:

      git revert <commit-hash>

    • Example:

If you realize a bug was introduced in commit abc123, you can revert it by running:

git revert xyz321

This will create a new commit that undoes the changes made by commit xyz321.

  • When to Use:

Use git revert when you want to undo a commit but still keep a record of the original changes. This is especially useful in collaborative environments where other people might have already pulled the changes.

Undoing Commits

  • Purpose: Sometimes you may want to undo commits that haven’t been pushed to the remote repository yet. This can be useful if you’ve committed something by mistake or need to amend your changes before sharing them.

  • Using git reset:

    • The git reset command allows you to undo changes by moving the current branch pointer to a previous commit. There are three main options for resetting: --soft, --mixed, and --hard.

    • Soft Reset (--soft):

      • Moves the branch pointer to a previous commit but leaves your working directory and index intact. This is useful if you want to re-commit your changes differently.

      • Usage:

        git reset --soft <commit-hash>

      • Example:

        To move back to a previous commit without losing changes:

        git reset --soft xyz321

    • Mixed Reset (--mixed):

      • Moves the branch pointer to a previous commit and resets the index, but leaves the working directory unchanged. This is the default option and is useful for undoing commits while keeping your changes unstaged.

      • Usage:

        git reset <commit-hash>

      • Example:

        To undo the last commit but keep the changes in your working directory:

        git reset HEAD~1

    • Hard Reset (--hard):

      • Moves the branch pointer to a previous commit and resets both the index and working directory to match that commit. This option permanently deletes any changes made after the reset point.

      • Usage:

        git reset --hard <commit-hash>

      • Example:

        To completely remove the last commit and all its changes:

        git reset --hard HEAD~1

    • Caution:

      • Use git reset --hard with caution, especially if you’ve already shared your commits with others, as it can lead to data loss.
  • Using git restore:

    • In newer versions of Git, git restore can be used to discard changes in the working directory or unstage files. This command is less destructive than git reset --hard.

    • Usage:

      git restore <file> git restore --staged <file>

    • Example:

      To discard changes to a specific file:

      git restore index.html

To unstage a file:

git restore --staged index.html