Oct 9, 2023 4 min read

Undo Last Git Commit: Free Tutorial

Undo Last Git Commit with our step-by-step tutorial. It is a distributed version control system for efficient code tracking.

Undo Last Git Commit
Table of Contents

Introduction

Before we begin talking about how to undo last git commit, let's briefly understand – What is Git ?

Git is a distributed version control system for efficient code tracking. It is a distributed version control system that allows developers to track changes in their project's codebase effectively. It helps multiple developers collaborate seamlessly by merging their work safely.

Git creates a detailed history of every modification made, providing a secure backup mechanism. It enhances productivity, simplifies teamwork, and enables easy experimentation. Mastering Git is essential for efficient software development.

In this tutorial, you will undo last git commit. We will also address a few FAQs on how to undo last git commit.

Advantages of Git

  1. Collaboration: Git enables seamless teamwork by allowing multiple developers to work on the same project simultaneously.
  2. Version Control: It conveniently tracks changes made to code, facilitating easy debugging and reverting to previous versions if needed.
  3. Flexibility: Git allows developers to work offline and switch between branches effortlessly, encouraging experimentation and innovation.
  4. Security: With Git, all project files are stored locally and backed up remotely, ensuring data integrity and recovery in case of loss.
  5. Productivity: By streamlining workflows, providing clear commit histories, and simplifying code merging, Git boosts developers' efficiency and speed.

Git Three-Tree Architecture

In Git, you'll be able to undo changes using the git reset command followed by the commit identifier.

git reset takes additional arguments that allow you to manage the command behavior. To understand clearly how reset works let’s speak about the Git’s three different trees. Three-tree architecture is the key concept of the Git management system. They're called trees because they represent collections of files.

Git manages and manipulates the following three trees:

  • The Working Directory - A directory, including all subdirectories and files on the local filesys­tem that's asso­ci­at­ed with the repository. It's often cited as a “working tree”. The working directory are something like a sandbox where you'll test the changes before committing them to the staging index.
  • The Index - This tree keeps track of recent or changed files that are added to the index with git add, to be included within the next commit. It's often cited as “staging area” or “staging index”.
  • The HEAD - A pointer to your last commit on the current branch.

    The git reset command has three arguments that correspond to the three trees:
  • --soft - Updates the HEAD pointer to the given commit. The Working directory and Index aren't changed.
  • --mixed - Updates the HEAD pointer and resets the Index to the required commit. The Working directory is left untouched. This is often the default operation mode of the reset command.
  • --hard - Updates the HEAD pointer and resets the Index and also the Working directory to the required commit. Be extra careful when using this selection as all local changes you haven’t committed are overwritten and lost.

Undoing the Last Commit

To undo the last commit without losing the changes you made to the local files and therefore the Index, invoke git reset with the --soft option followed by HEAD~1:

git reset --soft HEAD~1

HEAD~1 is a variable that points to the previous commit. The command above moves the present branch backward by one commit, effectively undoing your last commit. If you run the git status command, you’ll see that the changed files are listed as uncommitted changes.

To update the HEAD pointer to reset the Index, run git reset with --mixed or without an option:

git reset --mixed HEAD~1
git reset HEAD~1

The changed files are maintained, but unlike the previous example, now the changes aren't staged for commit.

If you don’t want to keep the changes you made to the files, invoke the git reset command with the --hard option:

git reset --hard HEAD~1

Before performing a hard reset, confirm you don’t need the changes anymore.

Undoing Multiple Commits


With git reset, you'll return to any previous commit.

For example, to move the present branch back three commits, you'd use:

git reset --hard HEAD~3

Since we are using --hard, the command above will remove the most recent three snapshots from the commit history.

Another way to move back to a selected commit is to pass the commit ID to the git reset command.

Use git log --oneline to seek out the commit IDs:

git log --oneline

The command will display a listing of all commits, including the ID and also the first line of the commit message:

Output

32921222 (HEAD -> master) Update changelog
7505724c adding new tests
750862ce new blog post
95a63417 sort configuration file
252032e4 Refactor User class
...

Once you recognize the ID of the commit you wish to reset to, just pass the ID to the git reset command:

git reset --hard 24b41564

FAQs to Undo Last Git Commit

Can I modify the last commit message?

Yes, you can modify the last commit message using the command git commit --amend -m "New commit message". This allows you to edit the last commit without creating a new one.

How can I discard the last commit entirely?

To discard the last commit completely, use git reset HEAD~1. This will remove the commit and all your changes, effectively returning your project to the state before the last commit.

What if I want to keep my changes but undo the commit?

In this case, you can use git reset HEAD~1 --soft. It will undo the last commit while keeping your changes, allowing you to make further modifications before committing again.

Can I undo multiple commits?

Yes, you can undo multiple commits by specifying the number of commits to undo. For example, git reset HEAD~3 will undo the last three commits.

How do I undo a commit that has already been pushed to a remote repository?

If a commit has been pushed, it is generally not recommended to directly undo it. Instead, you can create a new commit that reverses the changes using git revert <commit_id> and push the new commit.

Will undoing a commit also delete the changes locally?

When you undo a commit using git reset, it will remove the commit and changes from your local branch. However, you can still access the discarded changes through the reflog or other branch references.

Is it possible to undo a commit after a merge?

Yes, you can undo a commit after a merge. However, it is crucial to be cautious as it can affect the commit history and subsequent merges. It's best to consult with your team before making any changes.

Conclusion

To undo the last commit, use the git reset command. Don’t reset pushed commits because it may potentially cause plenty of problems for your colleagues.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.