Git 别名给出路径规范错误



我正在尝试诊断别名的问题,该别名允许用户签出文件的最新提交,以便阻止其最近的更改。 我不想硬重置,只需将旧版本签出到新一轮提交中即可。

同样重要的是,如果文件名没有错误地传入,则不要将整个存储库分支到 HEAD^,因此 sh 的 -u 开关(如果 $1 没有填充某些东西,则阻塞(。

如果这很重要,我们都在同一个仓库中工作,我是唯一一个不是完全新手的用户,但我绝不是 git 大师。

我们在 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 存储库上测试良好。

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

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

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

感谢您的关注和帮助!

你的别名不需要通过 shell 运行,你应该能够做到这一点:

[alias]
    back=checkout HEAD^ --

Git 将自动附加别名的参数,这意味着:

$ git back afile bfile cfile

会变成这样:

$ git checkout HEAD^ -- afile bfile cfile

奖励:注意使用双连字符"--",这告诉结帐剩余的参数都是文件,否则它可能会错误地将文件解释为现有分支或其他引用。

为了满足发布者使用 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"' -

相关内容

最新更新