How to Revert Individual Files and Folders to Old Versions in Git

How to Revert Individual Files and Folders to Old Versions in Git

Git is a powerful tool for tracking each version of your codebase, and it’s often necessary to look back in time and recover old versions of files. Git can revert whole commits, or reset the entire repository, but it can also roll back changes to a single file or folder.

Reverting vs. Resetting

Usually, when you “revert” a commit, Git applies a new commit applying the opposite changes, effectively cancelling it out. This is useful if you make a mistake and need to “delete” that commit, though it’s still recorded in the history.

Resetting the repository is a little different. You can only revert one commit at a time, but if you do a git reset, Git will completely change the state of the repo back to when that commit was made. This is done for lots of reasons, usually to delete commits or fix branch history.

Both of these operations work on the entire repository, but you can also use similar commands to perform the same actions on individual files or folders. For example, using git reset on a single file will set that file back to how it was when the commit was made. This is useful if you just want to pick out an old version of the file from your Git history.

Looking at Old Versions in Git

The low-tech solution to setting a file back to how it used to be is pretty simple—Github and most other Git servers keep track of your file history, and you can simple click on a commit and click “Browse Files” to view a snapshot of your repository from back in time. You can then download the file, or copy the text over.

How to Revert Individual Files and Folders to Old Versions in Git

This is especially useful if you’re working with a large code files, and want to look at old versions of functions you wrote. You probably don’t want to revert the entire thing in that case, just the single function. You can copy the code from that function without touching the Git CLI.

Resetting a File to an Old Version in Git

In this test repository, we made a commit that edited the README and added a new file. We want to revert the changes made to the README, but we don’t want to reset the entire repo back to the initial commit.

How to Revert Individual Files and Folders to Old Versions in Git

The solution is to reset just the README by checking out an old version of that file. Git’s checkout command does a lot of things, like switching branches, but it’s basically used for downloading files given a commit or branch ID.

To reset a file back to an old version, you’ll need to find the commit ID from when you want to reset to. You can use git log for this, scoped to a single file to view only the changes done to that file:

git log

How to Revert Individual Files and Folders to Old Versions in Git

Copy the ID for the commit, and then run git checkout with the ID and file path:

git checkout 22710694b25d7ce5297559851beb7d3e4de811bb

This will change the file back, but it won’t commit the changes just yet. You’re free to edit it and commit when you’re ready.

How to Revert Individual Files and Folders to Old Versions in Git

In this example, git checkout staged the changes for the next commit. If you don’t want to commit them, you’re free to discard the changes. This can be useful to download old versions of files temporarily without the use of Github.

Reverting Changes to Single Files

Similarly, if you want to revert the changes in a single commit, you can do so with git revert. There’s no way to apply it to a single file however, but you can simply discard the changes if the commit affects other files.

Use the --no-commit flag to allow editing of the “revert commit” that Git automatically creates.

git revert de8564b131ca6a15a7e7c73f5ef156b119cc0b93

This will allow you to change the files before finalizing the revert. If there are unwanted changes staged, you can remove them through your client, or with an empty git checkout.

git checkout -- file

How to Revert Individual Files and Folders to Old Versions in Git

News Related


Redmi Pad with 10.61-Inch Display, Helio G99 SoC Launched: Price in India, Specifications, Offers

The Redmi Pad competes against a bunch of tablets priced under Rs 20,000 in India, including the likes of the Realme Pad, OPPO Pad Air, Moto Tab G62, etc. Read more »

How to Login to Windows 11 Without Internet Connection

There are times when we need to use our computers in remote areas where WiFi or Ethernet Internet connections is not available, or when you are in Airplane mode. If ... Read more »

Acer Predator Helios 300 PH317-56 Price in Malaysia & Specs

The Acer Predator Helios 300 PH317-56 is powered by an Intel® Core™ i7-12700H processor, NVIDIA® GeForce RTX™ 3060 6GB GDDR6 (MGP : 140W) graphics, 16GB DDR5 4800Mhz (Upgradable to 32GB ... Read more »

Mobile Games Based On SpongeBob And Narcos Are Coming To Netflix Gaming

SpongeBob: Get Cooking is available on Netflix's mobile platform now, with base-building strategy game Narcos: Cartel Wars Unlimited also on the way. Read more »

New Maruti Suzuki Jimny 5 Door SUV Spied Near Khardung La

New range of Maruti SUVs will take on the collective might of Hyundai, Kia and Mahindra New Maruti Jimny 5 Door SUV With SUV segment registering strong growth, Maruti can ... Read more »

PNB Introduces WhatsApp Banking for Customers and Non-Customers; Know How to Activate

PNB Introduces WhatsApp Banking for Customers and Non-Customers; Know How to Activate PNB WhatsApp Banking: State-owned Punjab National Bank (PNB) on Monday said it has introduced banking services through WhatsApp ... Read more »

How to adjust the Taskbar in Windows 10 is addictive with play

13 ways to tweak the Taskbar in Windows 10 bring a lot of fun in the process of tweaking your computer’s interface. Join GhienCongListen to discover one of the default ... Read more »

Xiaomi 12T And 12T Pro Round Up: Expected Price In India, How To Watch Launch Event Online, Specifications, More

Xiaomi 12T series is launching globally today, and here's our round up of the phone's expected price, features and more. Read more »

Infinix 43Y1 Smart TV and INBook X2 Laptop To Launch in India Soon: Check Details

The Infinix InBook X2 will come in three colour options. Read more »

YouTube Might Want You to Be a Premium User to Watch 4K Videos

YouTube might be planning a new change, which might not be liked by many. The video-streaming platform is reportedly reserving 4K videos for Premium users, thus, persuading people to upgrade ... Read more »

Dragon Quest The Adventure Of Dai: A Hero's Bond Announces Free Anniversary Bonuses

The Dragon Quest mobile game is giving away a bunch of freebies to celebrate its first anniversary. Read more »

Acer Nitro 5 AN515-46 Price in Malaysia & Specs

The Acer Nitro 5 AN515-46 is powered by an AMD Ryzen™ 5 6600H processor, NVIDIA® GeForce RTX™ 3050 4GB GDDR6 (95W) graphics, 8GB DDR5 4800Mhz (1 extra RAM Slot) and ... Read more »

Twitter Edit Tweet Option Now Available for Some Users

Twitter edit button is live now, here's how to use it. Read more »

Death Stranding 2 Rumors Emerge Again, Reports Game Is Condenamed "Ocean"

New rumors seem to confirm that Death Stranding 2 is definitely in the works for Sony, under the codename "Ocean." Read more »

Ford Ranger 2023 review: XLT V6 - GVM test

The new Ranger looks fresh and modern. (Image: Mark Oastler) The V6 XLT has a 2285kg kerb weight. (Image: Mark Oastler) The load tub’s floor is 1547mm long and 1584mm ... Read more »

The 10 best photo scanners in 2022

Take a look at our picks for the best photo scanners out there, including ones for home and work. There are also ones that fit into all sorts of budgets. ... Read more »

Google Pixel 6a is Currently Available on Flipkart at Rs 28,528 with Bank Offer Making it One of the Most Consistently Discount Phones

The phone was launched for Rs 43,999 back in July. Read more »

Suda51 Says No More Heroes Could Get A Fourth Game

Travis could come back if there was enough demand from fans, the creator said. Read more »

The airasia Super App now lets users book tickets for buses, trains and ferries

airasia Super App today announced an expansion to its booking capability as the platform now allows users to book tickets for buses, trains and ferries seamlessly via its latest offering, ... Read more »

How to fix the taskbar not automatically hiding on Windows

Windows gives you a choice auto hide taskbar when not in use. However, if the feature doesn’t work, here’s how to fix it. Instructions to fix the taskbar error does ... Read more »

Redmi Note 11 SE and Redmi Watch 2 Lite Combo at Rs 13,999 is a Deal You Can Consider

As part of this combo deal, buyers can save Rs 4,499 with about a 25% discount. Read more »

JioBook Budget Laptop with Snapdragon 665 Processor, 2GB LPDDR4x RAM Launched in India: Price, Specifications

The JioBook laptop is expected to go on sale ahead of the festive season. Read more »

New TerraMaster NASs, the F2-223 and F4-223, add TRAID for SOHO and SMB users

TerraMaster’s new NASs, the F2-223 and F4-223, are two and four-bay storage arrays that include TerraMaster RAID (TRAID), and the latest TOS 5. Both are powered by an Intel Celeron ... Read more »

Spider-Man Supreme Is Coming To Marvel Contest Of Champions

The new Spider-Man variant will face off against villain Spot in Contest of Champions' new event quest Arachnophobia. Read more »

How to ensure the world’s largest pumped-hydro dam isn’t a disaster for Queensland’s environment

To quit coal and move to renewables, we need large-scale energy storage. That’s where pumped hydro comes in. Queensland’s ambitious new plan involves shifting from a coal-dominated electricity grid to ... Read more »

Reno 9 series tipped to pack the older Snapdragon 778G SoC

Details about the Oppo Reno 9 series’ chipset have surfaced online before the official launch, courtesy of a reliable tipster. Oppo brought its Reno 8 series smartphones into the Indian market ... Read more »

Digi Postpaid Infinite plans are back, free iPad from Postpaid Family Unlimited plan and many more

In another round of Juara Internet Untuk Semua by Digi, this new campaign is available for all new and current Digi customers to enjoy exciting offers. From free devices to ... Read more »

ICAI CA Foundation 2022: Last date to register for December exam TODAY at Here's how to apply

ICAI CA Foundation 2022: Last date to register for December exam TODAY at Here’s how to apply ICAI CA Foundation 2022: Institute of Chartered Accountants of India, ICAI CA ... Read more »

Ngern Tid Lor helps fix bad credit score and build good credit to create a sustainable financial life

Ngern Tid Lor Public Company Limited, or TIDLOR, led by Piyasak Ukritnukun, Managing Director, stated, “Since our establishment, Ngern Tid Lor has provided financial services for lower income customers with ... Read more »

Liverpool vs. Rangers: Preview, date, time, live stream and how to watch Champions League match in Canada

Liverpool vs. Rangers: Preview, date, time, live stream and how to watch Champions League match in Canada It’s been a trying start to the season for Liverpool. Sitting ninth in ... Read more »

Samsung Galaxy A54 Tipped to Come With a 50MP Camera Setup

The Samsung Galaxy A53 features a 64MP primary camera. Read more »

Porsche Macan review

Overview What is it? The Macan is the most popular Porsche in the UK. It’s an SUV with a desirable badge, how could it not be a success? The thing ... Read more »

Eintracht Frankfurt vs. Tottenham Hotspur live stream info, TV channel: How to watch Champions League on TV, stream online

Who will lift the Champions League trophy this season? The Champions League is back in action on Tuesday, only on Paramount+. Who’s Playing Tottenham Hotspur @ Eintracht Frankfurt What to Know ... Read more »

How to plant and grow amaryllis and what do you do with the bulbs after they bloom

amaryllis bulbs light spot best results strong compact growth what to do tips garden bloom Getting our amaryllis to reflower can be tricky, and there seems to be lots of ... Read more »

How to Disable Touch Screen in Windows 10/11

Sometimes, you may find the need to disable the Touch Screen on your Laptop computer. You can find below different methods to disable Touch Screen in Windows 10/11. Disable Touch ... Read more »

New Marvel Snap Season Symbiote Invasion Goes Live, Adds Miles Morales & Variants for Spider-Woman & Carnage

It’s a brand new month, which means it’s time for a new season in Marvel Snap. Previously, we had Sword and Shield, which added Nick Fury into the mix. Fury ... Read more »

Ed Sheeran to Bring Stadium Show to Soldier Field in 2023

Multi-platinum recording artist Ed Sheeran will bring his massive world tour to the city of Chicago next summer, performing a show at Soldier Field in July. Sheeran, best-known for hit ... Read more »

Pixel 7 series design renders, Pixel Watch user manual leaked

The highly anticipated Google Pixel 7 series smartphones have popped up on the internet again in the form of leaked design renders. Google is prepping to host a launch event ... Read more »

WIKO 10 Malaysia release: coming soon on 7 Oct with a free pair of headphones, priced at RM699

Today, Wiko Malaysia just announced a new WIKO 10 phone. The phone will come in three colours – Klein Blue, Silver and Black and will be available from the 7th of ... Read more »

Honda Launches Buy Now, Pay In 2023 Offer – For Amaze, City Buyers

Honda ‘Drive in 2022, Pay in 2023’ scheme has been launched in partnership with Kotak Mahindra Prime Limited Image – Yash9w With consumer sentiments soaring during festive season, auto companies ... Read more »
Breaking thailand news, thai news, thailand news Verified News Story Network