How to Discard Changes in Git (2024)

No matter how experienced you are as a programmer, not all of your code will always work at the first try. Luckily, Git allows you to discard and undo any of your changes, providing a safety net for your work.

In this short article, we'll look at some of the many ways how you can discard changes in Git.

Discarding Local Changes in a File

Changes that haven't been committed to the local repository are called "local" changes in Git. They exist in your Working Copy, but you haven't wrapped them in a commit, yet.

If you want to discard this type of changes, you can use the git restore command:

git restore index.html

This will undo all uncommitted local changes in the specified file. Please be careful because you cannot get these changes back once you've discarded them!

In case you are using the Tower Git client, you can discard local changes in a file simply from its contextual menu - or even discard only parts of your changes, while keeping the rest:

Although it's not possible in Git, Tower allows you to undo any wrongfully discarded changes with a simple shortcut: CMD+Z (or CTRL+Z on Windows)!

Discarding All Local Changes

If you want to undo all of your current changes, you can use the git restore command with the "." parameter (instead of specifying a file path):

$ git restore .

If, additionally, you have untracked (= new) files in your Working Copy and want to get rid of those, too, then the git clean command is your friend:

$ git clean -f

Again: please be careful with these commands! Once you've discarded your local changes, you won't be able to get them back!

Saving Changes on the Stash

Sometimes, you won't be 100% sure if you really don't need your local changes anymore. That's when - instead of discarding them - you can choose to save them temporarily:

$ git stash --include-untracked

Running this command will result in a clean Working Copy, but the changes are saved on Git's "Stash" so you can restore them at a later point if you need them:

$ git stash pop

The "pop" option will reapply the last saved state and, at the same time, delete and clean it from the Stash.

In case you are using the Tower Git client, saving to and restoring from the Stash can be performed right from the toolbar:

Learn More

How to Discard Changes in Git (2024)
Top Articles
Top-Down Analysis
Ironman money making guide
Splunk Stats Count By Hour
Unity Stuck Reload Script Assemblies
What Are the Best Cal State Schools? | BestColleges
Robot or human?
Puretalkusa.com/Amac
Sportsman Warehouse Cda
35105N Sap 5 50 W Nit
Www Thechristhospital Billpay
Evita Role Wsj Crossword Clue
Paketshops | PAKET.net
Ncaaf Reference
World Cup Soccer Wiki
DIN 41612 - FCI - PDF Catalogs | Technical Documentation
Colts seventh rotation of thin secondary raises concerns on roster evaluation
How Much Is Tj Maxx Starting Pay
Raleigh Craigs List
Classic Lotto Payout Calculator
Nwi Arrests Lake County
7543460065
Wizard Build Season 28
Bnsf.com/Workforce Hub
Carson Municipal Code
Copart Atlanta South Ga
Whitefish Bay Calendar
Effingham Bookings Florence Sc
Why Should We Hire You? - Professional Answers for 2024
Menards Eau Claire Weekly Ad
Happy Life 365, Kelly Weekers | 9789021569444 | Boeken | bol
Ezel Detailing
Parc Soleil Drowning
Contracts for May 28, 2020
Certain Red Dye Nyt Crossword
Engineering Beauties Chapter 1
How To Find Free Stuff On Craigslist San Diego | Tips, Popular Items, Safety Precautions | RoamBliss
Craigslist Panama City Beach Fl Pets
Hellraiser 3 Parents Guide
Relaxed Sneak Animations
Xxn Abbreviation List 2023
Www Mydocbill Rada
Rvtrader Com Florida
Tmka-19829
Bitchinbubba Face
Doordash Promo Code Generator
Powerspec G512
Sechrest Davis Funeral Home High Point Nc
Contico Tuff Box Replacement Locks
Doelpuntenteller Robert Mühren eindigt op 38: "Afsluiten in stijl toch?"
Unit 4 + 2 - Concrete and Clay: The Complete Recordings 1964-1969 - Album Review
Bob Wright Yukon Accident
Jesus Calling Oct 6
Latest Posts
Article information

Author: Velia Krajcik

Last Updated:

Views: 6097

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Velia Krajcik

Birthday: 1996-07-27

Address: 520 Balistreri Mount, South Armand, OR 60528

Phone: +466880739437

Job: Future Retail Associate

Hobby: Polo, Scouting, Worldbuilding, Cosplaying, Photography, Rowing, Nordic skating

Introduction: My name is Velia Krajcik, I am a handsome, clean, lucky, gleaming, magnificent, proud, glorious person who loves writing and wants to share my knowledge and understanding with you.