Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Undo Last Git Commit - DesignLinux

designlinux 0 Comments

Sometimes, when working with Git, you may want to undo the latest commit. A commit is a snapshot of a Git repository at a given time. Git has a reference variable called HEAD that points to the latest commit in the current working branch. To undo a commit, all you need to do is point the HEAD variable to the previous snapshot.

This guide explains how to undo the last Git commit.

It is not recommended to undo a commit is already pushed to a shared repository. If you only want to change the commit message, check out this article .

Git Three-Tree Architecture #

In Git you can undo changes using the git reset command followed by the commit identifier.

git reset takes additional arguments that allow you to control the command behavior. To better understand how reset works let’s talk about the Git’s three different trees. Three-tree architecture is the key concept of the Git management system. They are 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 is asso­ci­at­ed with the repository. It is often referred to as a “working tree”. The working directory is something like a sandbox where you can test the changes before committing them to the staging index.
  • The Index – This tree keeps track of new or changed files that have been added to the index with git add, to be included in the next commit. It is often referred to 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 are not changed.
  • --mixed – Updates the HEAD pointer and resets the Index to the specified commit. The Working directory is left untouched. This is the default operation mode of the reset command.
  • --hard – Updates the HEAD pointer and resets the Index and the Working directory to the specified commit. Be extra careful when using this option as all local changes you haven’t committed will be overwritten and lost.

Undoing the Last Commit #

To undo the last commit without losing the changes you made to the local files and 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 current 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~1git reset HEAD~1

The changed files are kept up, but unlike the previous example, now the changes are not 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, make sure you don’t need the changes anymore.

Undoing Multiple Commits #

With git reset, you can return to any previous commit.

For example, to move the current branch back three commits, you would use:

git reset --hard HEAD~3

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

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

Use git log --oneline to find the commit IDs:

git log --oneline

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

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

Once you know the ID of the commit you want to reset to, just pass the ID to the git reset command:

git reset --hard 95a63417

Conclusion #

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

If you hit a problem or have feedback, leave a comment below.

git

Related

Tags: git

How to Create Bash Aliases

Prev Post

Setting Up Hadoop Pre-requisites and Security Hardening – Part 2

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 502
  • 1,055,274

DesignLinux.com © All rights reserved

Go to mobile version