Skip to main content
ubuntuask.com

Back to all posts

How to Undo the Last Git Commit?

Published on
4 min read
How to Undo the Last Git Commit? image

Best Git Tools to Buy in December 2025

1 Learning Git: A Hands-On and Visual Guide to the Basics of Git

Learning Git: A Hands-On and Visual Guide to the Basics of Git

BUY & SAVE
$34.92 $45.99
Save 24%
Learning Git: A Hands-On and Visual Guide to the Basics of Git
2 FASTPRO Pink Tool Set, 220-Piece Lady's Home Repairing Tool Kit with 12-Inch Wide Mouth Open Storage Tool Bag

FASTPRO Pink Tool Set, 220-Piece Lady's Home Repairing Tool Kit with 12-Inch Wide Mouth Open Storage Tool Bag

  • COMPLETE TOOLKIT FOR ALL YOUR DIY AND HOUSEHOLD NEEDS!
  • DURABLE FORGED STEEL PLIERS FOR STRENGTH AND EASY USE.
  • STYLISH PINK DESIGN MAKES IT A PERFECT GIFT CHOICE!
BUY & SAVE
$49.99 $66.99
Save 25%
FASTPRO Pink Tool Set, 220-Piece Lady's Home Repairing Tool Kit with 12-Inch Wide Mouth Open Storage Tool Bag
3 Version Control with Git: Powerful Tools and Techniques for Collaborative Software Development

Version Control with Git: Powerful Tools and Techniques for Collaborative Software Development

BUY & SAVE
$43.23 $65.99
Save 34%
Version Control with Git: Powerful Tools and Techniques for Collaborative Software Development
4 CARTMAN 39Piece Tool Set General Household Hand Tool Kit with Plastic Toolbox Storage Case Pink

CARTMAN 39Piece Tool Set General Household Hand Tool Kit with Plastic Toolbox Storage Case Pink

  • COMPLETE TOOL SET FOR ALL YOUR SMALL REPAIR AND DIY NEEDS!
  • DURABLE CONSTRUCTION RESISTS CORROSION FOR LONG-LASTING USE.
  • CONVENIENT STORAGE CASE ENSURES TOOLS STAY ORGANIZED ON-THE-GO!
BUY & SAVE
$22.99 $24.99
Save 8%
CARTMAN 39Piece Tool Set General Household Hand Tool Kit with Plastic Toolbox Storage Case Pink
5 Apollo Tools 135 Piece Household Pink Hand Tools Set with Pivoting Dual-Angle 3.6 V Lithium-Ion Cordless Screwdriver - DT0773N1

Apollo Tools 135 Piece Household Pink Hand Tools Set with Pivoting Dual-Angle 3.6 V Lithium-Ion Cordless Screwdriver - DT0773N1

  • EFFORTLESS DIY WITH UPGRADED 3.6V RECHARGEABLE SCREWDRIVER.
  • ORGANIZED TOOLS IN A HEAVY-DUTY, PORTABLE TOOLBOX FOR CONVENIENCE.
  • SUPPORT BREAST CANCER RESEARCH WITH EVERY PURCHASE-$1 DONATION!
BUY & SAVE
$34.99
Apollo Tools 135 Piece Household Pink Hand Tools Set with Pivoting Dual-Angle 3.6 V Lithium-Ion Cordless Screwdriver - DT0773N1
6 Stalwart - 75-HT1007 Household Hand Tools, Tool Set - 6 Piece by , Set Includes – Hammer, Screwdriver Set, Pliers (Tool Kit for the Home, Office, or Car) Black

Stalwart - 75-HT1007 Household Hand Tools, Tool Set - 6 Piece by , Set Includes – Hammer, Screwdriver Set, Pliers (Tool Kit for the Home, Office, or Car) Black

  • ALL-IN-ONE KIT FOR MINOR REPAIRS AND DIY PROJECTS AT HOME.
  • COMPACT DESIGN EASILY FITS IN ANY SPACE FOR CONVENIENT STORAGE.
  • DURABLE TOOLS ENSURE PRECISION FOR ALL YOUR REPAIR NEEDS.
BUY & SAVE
$8.12 $9.56
Save 15%
Stalwart - 75-HT1007 Household Hand Tools, Tool Set - 6 Piece by , Set Includes – Hammer, Screwdriver Set, Pliers (Tool Kit for the Home, Office, or Car) Black
7 Household Tool Box - 7-Piece Handheld Tool Kit With Hammer, Phillips Screwdrivers, Long-Nose Pliers, Tweezers, and Tape Measure by Stalwart

Household Tool Box - 7-Piece Handheld Tool Kit With Hammer, Phillips Screwdrivers, Long-Nose Pliers, Tweezers, and Tape Measure by Stalwart

  • COMPLETE 7-PIECE TOOLKIT FOR ALL YOUR DIY AND REPAIR NEEDS.

  • COMPACT CARRYING CASE: EASILY FITS IN HOME, CAR, OR GARAGE.

  • DURABLE METAL TOOLS ENSURE PRECISION FOR EVERY MAINTENANCE TASK.

BUY & SAVE
$10.15 $13.12
Save 23%
Household Tool Box - 7-Piece Handheld Tool Kit With Hammer, Phillips Screwdrivers, Long-Nose Pliers, Tweezers, and Tape Measure by Stalwart
8 Professional Git

Professional Git

BUY & SAVE
$24.79 $52.00
Save 52%
Professional Git
9 Head First Git: A Learner's Guide to Understanding Git from the Inside Out

Head First Git: A Learner's Guide to Understanding Git from the Inside Out

BUY & SAVE
$50.99 $79.99
Save 36%
Head First Git: A Learner's Guide to Understanding Git from the Inside Out
10 Version Control with Git: Powerful tools and techniques for collaborative software development

Version Control with Git: Powerful tools and techniques for collaborative software development

  • AFFORDABLE PRICES ON QUALITY USED BOOKS FOR SAVVY SHOPPERS.
  • ENVIRONMENTALLY FRIENDLY: REDUCE WASTE BY CHOOSING USED.
  • UNIQUE FINDS: DISCOVER RARE TITLES NOT AVAILABLE IN STORES.
BUY & SAVE
$42.54 $44.99
Save 5%
Version Control with Git: Powerful tools and techniques for collaborative software development
+
ONE MORE?

To undo the last Git commit, you can use the "git revert" or "git reset" command. Here are the steps:

  1. To undo the last commit while preserving the changes as uncommitted working directory changes, use the "git revert" command followed by the commit hash or HEAD~1: git revert HEAD This creates a new commit that undoes the previous commit's changes.
  2. To completely remove the last commit and any changes associated with it, you can use the "git reset" command: git reset HEAD~1 This moves the HEAD pointer to the previous commit, effectively discarding the last commit and its changes. If you want to remove the commit and all changes from your working directory, you can use the "--hard" flag: git reset --hard HEAD~1 Use this with caution as it irreversibly removes any local changes.

Remember to use these commands selectively and be cautious when manipulating commit history, especially if you have already pushed your changes to a shared repository.

How to revert a specific file from the last Git commit?

To revert a specific file from the last Git commit, you can follow these steps:

  1. Make sure you are in the root directory of your Git repository using the command line.
  2. Use the git log --oneline command to view the list of commits and find the commit hash of the previous commit that you want to revert.
  3. Run the command git checkout -- to checkout and revert the specific file from the previous commit. Replace with the commit hash you found in the previous step, and with the path to the file you want to revert. For example, if you want to revert the file example.txt from the previous commit with the hash abcdef, the command would be: git checkout abcdef -- example.txt
  4. After executing the command, the specified file will be reverted to its state in the previous commit.
  5. Finally, you can verify the changes by running git status to see that the file is reverted and then create a new commit to keep track of the changes made after reverting the specific file if needed.

Note: Reverting a file will permanently remove any changes made to it in the previous commit and replace it with the state from the commit you specified.

What is the safest method to undo the last Git commit?

The safest method to undo the last Git commit is to utilize the git revert command. Unlike git reset, which discards the commits and history, git revert creates a new commit that undoes the changes introduced by the previous commit.

To undo the last Git commit using git revert, you can follow these steps:

  1. Open your command line or terminal.
  2. Navigate to the Git repository where you want to undo the commit.
  3. Execute the following command:

git revert HEAD

  1. This will open your default text editor to create a commit message for the revert. Save and exit the commit message file to confirm the revert.
  2. Git will now create a new commit that undoes the changes made in the previous commit.

By using git revert, you keep the commit history intact, making it a safer method since it doesn't alter the repository's history.

What is the command to uncommit the most recent Git commit?

To uncommit the most recent Git commit, you can use the command:

git reset HEAD~1

This command will move the HEAD pointer and the branch pointer back one commit, effectively removing the last commit from history. Although the changes will still be present in your working directory, they will not be included in any commit.

Alternatively, if you want to remove the commit entirely and discard the changes, you can use:

git reset --hard HEAD~1

This command not only moves the pointers back one commit but also discards all changes made in that commit.