12

I have changes in many types of file like .tsx .scss and .scss.d.ts, and have committed and pushed to my branch.

Is there any way I can reset only extension .scss.d.ts with master ?

Keep the changes in .tsx and .scss only reset .scss.d.ts with master.

Thanks

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
ankitd
  • 1,967
  • 3
  • 26
  • 44
  • 1
    Possible duplicate of [How to revert uncommitted changes to files of a certain type in git](https://stackoverflow.com/questions/14864655/how-to-revert-uncommitted-changes-to-files-of-a-certain-type-in-git) – Chris Maes Oct 16 '19 at 09:17

2 Answers2

17

You could first output a list of the paths with

git ls-files master -- *.scss.d.ts

then that list can be send to the checkout command* to restore each of them to their state on master

git checkout master -- $(git ls-files master -- *.scss.d.ts)

* Note that since recent git versions, you also have git restore to the same effect

git restore --source=master '*.scss.d.ts'
Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • am able to get liest but second command does not work for me. – ankitd Oct 16 '19 at 09:43
  • yeah files have been committed and pushed to branch. :( – ankitd Oct 16 '19 at 09:53
  • @antikd "files have been committed and pushed to branch." You seem to imply that's a bad thing. Why? Also, what "does not work"? Do you get an error? An unexpected state for your files? – Romain Valeri Oct 16 '19 at 14:32
  • `git checkout master -- $(git ls-files -- *.scss.d.ts)` gives these error error: pathspec 'pathtofile.scss.d.ts' did not match any file(s) known to git. – ankitd Oct 17 '19 at 05:32
  • This error is coming for 8 files and all of them does exist. – ankitd Oct 17 '19 at 05:33
  • Aww.... sorry, I made a stupid copy-pasting mistake when reformatting my answer, but of course `master` has to be referenced in the call to `ls-files` too (`git checkout master -- $(git ls-files master -- *.scss.d.ts)`), I edited. – Romain Valeri Oct 17 '19 at 06:33
  • FYI am running this on git bash on windows 10. – ankitd Oct 17 '19 at 06:45
  • @ankitd Same environment here, I tried it on a repo and it worked. I wonder what could be different in your case... – Romain Valeri Oct 17 '19 at 09:25
  • While this works, it's overly complex and probably not what most people are looking for. Sorry, but there are better ways to do this. – MTS Feb 16 '22 at 00:43
  • FYI the `checkout branch --` version does a hard reset whereas the `restore` only does a soft reset. – Basti Jan 25 '23 at 14:48
13
git reset master -- "*.scss.d.ts"
JBallin
  • 8,481
  • 4
  • 46
  • 51
  • This is the correct answer. – MTS Feb 16 '22 at 00:43
  • On `git version 2.32.0 (Apple Git-132)`, simply **`git reset "*.scss.d.ts"`** seems to work as well. – MTS Feb 16 '22 at 00:46
  • @MTS No, I'm afraid this is a confusion. OP wanted to *get rid* of changes, not keep and unstage them. This command does **not** achieve the goal, it only *unstages* files with a particular extension. See my solution for a maybe slightly more complex, but working solution. – Romain Valeri Feb 16 '22 at 07:56
  • @RomainValeri where does OP say “rid”? All I see is “reset”. – JBallin Feb 17 '22 at 00:10
  • @MTS that will reset to HEAD, which may be different from master (which is what OP requested). – JBallin Feb 17 '22 at 00:13
  • @MTS First, it sounded more like the colloquial use of "reset", not the specific git meaning, but then the mention of "keep the changes in ..." convinced me it clearly wasn't "resetting understood as unstaging" from OP. We'd need them to clarify here, I guess. – Romain Valeri Feb 17 '22 at 08:06