How To Undo A Change In Particular File In Git
2 min readJul 8, 2022
The aim of this page📝 is to help checking how could I undo changes to multiple files from various pull requests ideally with a single command.
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
- notreset
, norrevert
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"
- the file will be restored to that version → continue with the git-business as usual (depends on your workflow)
2. OLD WAY
★ Answer from https://stackoverflow.com/questions/31281679/how-to-undo-local-changes-to-a-specific-file ★
- You want
git checkout
to get git's version of the file from master.
git checkout -- filename.txt
- In general, when you want to perform a git operation on a single file, use
-- filename
. - 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.