How To Undo A Change In Particular File In Git

1. NEW WAY / MY CASE

  • no simple way with a single command, got to go one-by-one; need a commit SHA + filepath
  • it is using restore - not reset, nor revert as a way to UNDO things in git
  • git restore source=<commit> <filepath>
  • if you don’t know the commit you want to come back to, but the commit with which the change was made, use <commit>~1
  • ~1 was my fix, so the command looked e.g. (I'm a powershell/win guy)
<!-- GIT FULL COMMAND -->
▶ git restore --source fee6ebe5~1 "C:\Users\Admin\Documents\workspace\SNOW\support-kb\20 Collector\20.04-AWS-custom-domain-deployment-with-DNS.md"
<!-- POSH THROWAWAY FUNCTION -->
function gr($sha,$file) {git restore --source $sha~1 $file}
<!-- CALL THE THROWAWAY FUNCTION -->
gr a43e63b7 "C:\Users\Admin\Documents\workspace\SNOW\support-kb\23 Loaders\23.01-Out-of-Memory-(OOM)-Error-Resolution.md"

2. OLD WAY

★ Answer from https://stackoverflow.com/questions/31281679/how-to-undo-local-changes-to-a-specific-file

git checkout -- filename.txt
  • In 2020, Git introduced a new command git restore in version 2.23.0. Therefore, if you have git version 2.23.0+, you can simply git restore filename.txt — which does the same thing as git checkout — filename.txt. The docs for this command do note that it is currently experimental.

3. LINKS

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavol Kutaj

Pavol Kutaj

Infrastructure Support Engineer/Technical Writer (Snowplow Analytics) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com