Git别名给出了pathspec错误

时间:2013-09-30 23:43:29

标签: git version-control alias

我正在尝试使用别名来诊断问题,该别名允许用户签出文件的最新提交以便破坏他们最近的更改。我不想重新设置,只需将旧版本签出到新一轮提交中。

如果没有错误地传递文件名,那么将整个仓库分支到HEAD ^也很重要,因此sh的-u开关(如果$ 1没有填充东西则会阻塞)。

如果重要的话,我们都在同一个存储库中工作,而且我是唯一一个不是一个完整新手的用户,但我绝不是一个git master。

我们在Windows 7上使用MsysGit 1.8.4。

这是别名:

[alias]
    back1=!sh -uc 'git checkout HEAD^ $1 ' -

然而,这有时会产生错误,如

$ git back0 format_tests.sas
error: pathspec 'format_tests.sas' did not match any file(s) known to git.

但是如果我跑

$ git checkout HEAD^ format_tests.sas

直接从命令行,它工作正常。它似乎也可以在另一个git存储库上测试。

我觉得这可能是一个引用问题,但我现在还不知道。

我不使用下面介绍的方法的原因是,如果我在没有文件的情况下运行命令,我会得到类似下面的内容,这在我们的新手环境中是不行的(为什么我使用shell命令“ -u“首先切换”:

testgit.git/ (j=0,r=0)$ git back1
Warning: you are leaving 1 commit behind, not connected to
any of your branches:

  a77dfed hlksdj;

If you want to keep them by creating a new branch, this may be a good time
to do so with:

 git branch new_branch_name a77dfed

HEAD is now at 954f639... blakjsd

感谢您的关注和帮助!

1 个答案:

答案 0 :(得分:1)

你的别名不需要在shell中运行,你应该可以这样做:

[alias]
    back=checkout HEAD^ --

Git会自动附加别名'参数,这意味着:

$ git back afile bfile cfile

将变为:

$ git checkout HEAD^ -- afile bfile cfile

Bonus:注意使用双连字符“ - ”,这告诉checkout剩下的参数都是所有文件,否则它可能会错误地将文件解释为现有分支或其他引用。

要解决海报要求使用shell命令别名,需要注意的是子shell将其工作目录设置为存储库的根目录。以下是git help config所说的内容:

alias.*
    [...] Note that shell commands will be executed from the
    top-level directory of a repository, which may not necessarily be
    the current directory.  GIT_PREFIX is set as returned by 
    running git rev-parse --show-prefix from the original current directory.

好消息是您可以在命令中使用的GIT_PREFIX变量,如下所示:

back1=!sh -uc 'git checkout HEAD^ -- "$GIT_PREFIX/$1"' -