how to

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

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

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 README.md

how to

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

git checkout 22710694b25d7ce5297559851beb7d3e4de811bb README.md

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

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

TECH NEWS RELATED

Sony Teases Possible TV Shows And Movies Based On From Software Games

PlayStation's Hermen Hulst discusses why Sony invested more money into the company behind Elden Ring and Bloodborne.

View more: Sony Teases Possible TV Shows And Movies Based On From Software Games

Goodbye, Lightning port? EU votes to make USB-C the charging standard by the end of 2024

What just happened? The European Parliament has voted to pass EU legislation that will see USB-C become the universal charging format across numerous consumer electronic devices, including iPhones, by the end of 2024. The European Commission’s more than decade-long fight for a universal charging solution appeared to be almost ...

View more: Goodbye, Lightning port? EU votes to make USB-C the charging standard by the end of 2024

TOPPING E30 II DAC + L30 II Amplifier Desktop Stack Review

Introduction The beginning of this year saw our first TOPPING product review in the form of a two-in-one stack consisting of the E50 digital-to-analog converter (DAC) and and the L50 linear headphone amplifier. Priced under $500, and offering free RCA interconnects if purchased as a combo from the likes of ...

View more: TOPPING E30 II DAC + L30 II Amplifier Desktop Stack Review

Why are Workstation GPUs expensive? Are they better and faster?

Many of you must have noticed at some point before how GPUs designed for workstations are more expensive than traditional GPUs for gaming and general consumer use. Workstation GPUs such as the likes of the AMD FirePro and the NVIDIA Quadro are some of the best in the business, but ...

View more: Why are Workstation GPUs expensive? Are they better and faster?

How to Try to open the coffin in the Genshin Impact The Secret of Al-Ahmar World Quest

Screenshot by Pro Game Guides The Secret of Al-Ahmar is the third part of the Golden Slumber World Questline, where you, Tirzad, and the mercenaries explore The Mausoleum of King Deshret. You’ll encounter plenty of puzzles in the dizzying, maze-like interior—the first of which involves finding a way to ...

View more: How to Try to open the coffin in the Genshin Impact The Secret of Al-Ahmar World Quest

Fortnite v22.10 Update Adds a Formidable Ranged Weapon & Unvaults a Fan-Favorite

Epic Games has released the first-ever major update of the Fortnite Chapter 3 Season 4 called v22.10 for its battle royale mode. The latest patch sees the addition of a new weapon called Cobra DMR, and players have been given a chance once again to swing with the Grapple Gloves ...

View more: Fortnite v22.10 Update Adds a Formidable Ranged Weapon & Unvaults a Fan-Favorite

Samsung Has a Plan to End the Chip Shortage

BigBlueStudio/Shutterstock.com The ongoing shortage of semiconductors has caused ripple effects throughout the technology world, affecting the availability of phones, tablets, cars, and many other electronics. The situation has improved slightly in recent months, and Samsung has announced plans to expand chip manufacturing. Samsung might be best known for selling ...

View more: Samsung Has a Plan to End the Chip Shortage

How to Connect a Laptop to a Monitor

diy13/Shutterstock.com Most people choose laptops for their portability. However, you can get a lot more utility out of a laptop by plugging it into a larger display while at home or in the office. Here are a few tips to get you started. Plug It In The first thing ...

View more: How to Connect a Laptop to a Monitor

HyperX Cloud Stinger 2 headset review

‘Deadstream’ Review (Shudder)

Apple Music passes 100 million song milestone

How to make Pumpkin Soup in Disney Dreamlight Valley

Slime Rancher 2 keeps crashing or freezing on PC

Valorant Patch 5.07 Delivers Huge Fracture Overhaul & Changes to Agent Flashes

JBL Tune Flex review: earbuds that you can wear two ways

New Nintendo Direct Coming Soon, but This One Is Not About Games

Bayonetta 3 Artist Mari Shimazaki Reveals Her Gorgeous Art for Bayonetta & Viola

Bungo Stray Dogs Anime Gets New Action-Packed Season 4 Key Art

Valorant PBE Flash Changes Likely to Shift the Meta Differently Across Ranked & Pro Play [UPDATED]

How to make Cherry Pie in Disney Dreamlight Valley

OTHER TECH NEWS

Top Car News Car News