1

I am working on a project with a shell script file (let's say a.sh) in it.

I clone the repo on Windows and accidentally run a.sh. Then, when I run git status in git bash, I got the following information

git status
on branch master
Changes not staged for commit:
(use "git add <file> ..." to update what will be committed)
(use "git restore <file> ..." to discard changes in working directory)
    modified: a.sh

Obviously, I do not want this a.sh to be changed, and I follow the hint with

git restore a.sh

However, when I run git status again, I still got the same information that a.sh was modified. I also tried another way by stage and commit this file with git commit -am "restore sh", then try to reset this file by git restore head^ a.sh. The result is still the same.

So I was wondering if there are any methods to tackle this problem?

FYI, according to my colleagues, a.sh is used for killing idle python thread. When I git diff this file, it shows that

old mode 100755
new mode 100644

I am not so familiar with shell script, so I am not sure if this information will be helpful.

  • Something seems to be changing the shell scripts permissions. – evolutionxbox Apr 06 '20 at 13:04
  • I've never used `git restore`. I would have used `git checkout a.sh`, which is what `git` used to recommend. – chepner Apr 06 '20 at 13:06
  • see https://stackoverflow.com/questions/1580596/how-do-i-make-git-ignore-file-mode-chmod-changes – kofemann Apr 06 '20 at 13:07
  • @chepner https://git-scm.com/docs/git-restore is new, and replaces part of `git checkout` – evolutionxbox Apr 06 '20 at 13:10
  • @evolutionxbox Given that it is experimental and doesn't appear to provide any advantage over `checkout` in this case, I would stick with `checkout`. – chepner Apr 06 '20 at 13:11
  • It's experimental? The git docs don't say that it is. – evolutionxbox Apr 06 '20 at 13:13
  • @chepner I actually tried `git checkout a.sh`, but it still doesn't work out. – Yuejiang_Li Apr 06 '20 at 13:21
  • @evolutionxbox The link you posted did. Maybe it's not considered experimental in a version of `git` that actually recommends its use. – chepner Apr 06 '20 at 13:29
  • @chepner smdh. It totally does. How did I miss that? – evolutionxbox Apr 06 '20 at 13:30
  • @kofemann Thanks for the link, it worked! – Yuejiang_Li Apr 06 '20 at 14:20
  • Does this answer your question? [How do I make Git ignore file mode (chmod) changes?](https://stackoverflow.com/questions/1580596/how-do-i-make-git-ignore-file-mode-chmod-changes) – kofemann Apr 06 '20 at 15:01
  • @chepner (and others): I'm not a developer-of-Git, and they are indeed all marked "experimental", but at this point, it's pretty likely that they'll remain. The basic observation is that the existing `git checkout` has safe modes (check out branch name) and unsafe modes (check out file name, overwrites and discards work-in-progress) and that you can't necessarily tell which mode any given `git checkout` might use. So the command got split into the safe `git switch` and the unsafe `git restore`. Since then, `git restore` has acquired options that let it do a few things `git checkout` can't. – torek Apr 06 '20 at 16:09
  • 1
    This is not precisely a duplicate, but see https://stackoverflow.com/q/21691202/1256452 – torek Apr 06 '20 at 16:13
  • Try `git checkout -- a.sh` to discard changes to a.sh – liuwenzhuang Apr 07 '20 at 08:13

1 Answers1

0

Welcome to Stackoverflow.

Looks like you have not pushed any of your changes to "master" (remote).

So, you can do:

git reset --hard origin/master

This will undo whatever you have done at origin (i.e. you computer) to match your last "pushed" version on remote (i.e. before you started any of this).

Good Luck.

Mamun
  • 2,322
  • 4
  • 27
  • 41