简体   繁体   English

git 从 bash 脚本和终端签出的行为不同

[英]git checkout from bash script and from terminal behaves differently

I am trying to do a regression test of my code using git and a bash script.我正在尝试使用 git 和 bash 脚本对我的代码进行回归测试。 So I have written a script to run a sample with the current version and with some previous version.所以我编写了一个脚本来运行当前版本和一些以前版本的示例。 The source files are located in "src" directory, and samples are in different "samples" directory.源文件位于“src”目录中,样本位于不同的“samples”目录中。 Both "src" and "samples" directories are tracked by git under same project. git 在同一项目下跟踪“src”和“samples”目录。

I want to roll back only "src" files and no touch files in "samples".我只想回滚“src”文件,而“samples”中没有触摸文件。 I have tried git reset and git checkout.我试过 git 重置和 git 结帐。

Problem: When I go to "src" in terminal and type:问题:当我 go 到终端中的“src”并输入:

git checkout -- *.F90

It does what is should - changes only Fortran files in this directory.它做了应该做的事情 - 仅更改此目录中的 Fortran 文件。

If I do the same command in a bash script, it changes everything: files "src" and in "samples", Like if it is run from the main directory.如果我在 bash 脚本中执行相同的命令,它会更改所有内容:文件“src”和“samples”,就像从主目录运行一样。 as "git checkout" or "git reset origin/master"..?作为“git checkout”或“git reset origin/master”..? How can it be?怎么会这样?

Somewhere I have found suggestion to use "env -i" in a script, but it did not work either.我在某个地方发现了在脚本中使用“env -i”的建议,但它也不起作用。

The whole script is about 500 lines, here is git related part:整个脚本大约500行,这里是git相关部分:

cd $dsrc                   # go to src directory
ls                         # debuging, just to see , that I am there
git stash
git checkout origin/master -- *.F90

with ls command I do see the source files, I am in the right place.使用 ls 命令我确实看到了源文件,我在正确的位置。

Any ideas?有任何想法吗?

From the Git glossary for "pathspec":来自“pathspec”的Git 词汇表

[...] in particular, * and ? [...] 特别是*? can match directory separators.可以匹配目录分隔符。

So *.F90 matches all .F90 files everywhere.所以*.F90匹配所有.F90文件。 To limit to the current directory, you could use要限制到当前目录,您可以使用

git checkout origin/master -- "$PWD"/*.F90

Notice that (as pointed out by torek in their comment) the manual refers to what Git does.请注意(正如torek在他们的评论中指出的那样)该手册指的是Git所做的。 If the shell can expand the glob first, it will – for example, if there are files in the current directory that do match *.F90 .如果shell可以先扩展 glob,它将 - 例如,如果当前目录中匹配*.F90的文件。

Only when there aren't any will Git get the unexpanded literal *.F90 and match the * with any prefix, including / .只有当没有任何前缀时 Git 才会获得未扩展的文字*.F90并将*与任何前缀匹配,包括/ To prevent expansion by the shell, you could quote the glob:为了防止 shell 的扩展,您可以引用 glob:

git checkout origin/master -- "$PWD/*.F90"

or use .或使用. in the pathspec:在路径规范中:

git checkout origin/master -- ./*.F90

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM