Sitemap

Git Undoing Things

3 min readDec 5, 2023

--

Press enter or click to view image in full size
Git GitHub Version Control Source Code Management Distributed Version Control Repository Branches Commits Merge Pull Request Push Forking Collaboration Code Review Issue Tracking GitFlow Git Commands GitHub Pages Continuous Integration Continuous Delivery DevOps Code Hosting Source Code Collaboration Code Hosting Platform Version Control System Distributed System Git Hooks Gitignore Codebase Git Clients Commit History Code Sharing Codebase Management Repository Management Open Source Git GUI Git
Git Undoing Things

Git offers various commands to handle changes in a repository, with `checkout`, `revert`, and `reset` being three key ones. This overview outlines the distinctions among these commands for undoing changes in a Git repository.

GIT Checkout (Safe)

The git checkout command in Git has various uses, and one of them is to navigate between different commits, effectively allowing you to "undo" commits in a sense.

  • Navigate to a Specific Commit:
git checkout <xxxx>
  • Create a New Branch from a Commit:

You can create a new branch from a specific commit using git checkout -b.

git checkout -b dev <xxxx>
  • Return to the Latest Commit:

To return to the latest commit, switch back to the branch.

git checkout main

GIT Revert (be aware)

— Create Revert Commit: The `git revert` command is used to create a new commit that undoes the changes made in a previous commit.
— Safe for Shared Branches: Since `revert` creates a new commit, it’s considered safer for use in shared branches because it doesn’t rewrite history.

git revet xxxx

— Doesn’t Remove Commits: It doesn’t remove the original commit; instead, it adds a new commit that undoes the changes.

Get Soulaimane YAHYA’s stories in your inbox

Join Medium for free to get updates from this writer.

If you want to avoid the text editor prompt for the commit message, you can use the -m option:

git revert -m 1 --no-edit xxxx

When you perform a merge in Git, it creates a new commit known as a merge commit. A merge commit typically has two or more parents, representing the commits from the branches being merged. The git revert command is used to create a new commit that undoes the changes introduced by a previous commit. However, when dealing with merge commits, you need to specify which parent(s) you want to revert.

In this example, -m 1 specifies the mainline (parent) for a merge commit, and --no-edit tells Git not to open the text editor. The revert will be performed with the default commit message, indicating that it is a revert of a specific commit.

  • Revert Multiple Commits:

To revert multiple consecutive commits, you can specify a commit range. For example, to revert commits from abc123 to def456:

git revert abc123^..def456

GIT Reset (Danger)

— Move HEAD and Optionally Modify Staging Area: The `git reset` command is used to move the `HEAD` pointer to a specific commit, effectively “rewinding” the branch to that commit. It can also optionally modify the staging area and working directory.

git reset xxxx [--soft --mixed --hard]

` — soft`: Only moves the `HEAD` pointer, leaving changes staged.
` — mixed`: Moves the `HEAD` pointer and unstages changes, but leaves changes in the working directory.
` — hard`: Moves the `HEAD` pointer, resets the staging area, and discards changes in the working directory.

In summary

- Use `git checkout` for switching between commits in the working directory.
- Use `git revert` to create a new commit that undoes changes from a previous commit.
- Use `git reset` to move the `HEAD` pointer and optionally modify the staging area and working directory, with different modes for different behaviors.

--

--