git别名在错误的目录下运行 [英] git aliases operate in the wrong directory

查看:106
本文介绍了git别名在错误的目录下运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总结:目前通过git别名运行的命令的工作目录是错误的。



最简单的演示方法是使用git别名: p>

  [alias] 
pwd =!pwd

所以 git pwd 只是运行bash命令 pwd 。人们会认为这两个命令的输出是一样的。现在,我们来试试一下这个例子:

  $ cd〜
$ pwd&& git pwd
/ home / limpchimp
/ home / limpchimp#great!
$ mkdir foo&& cd foo&&& git init
/home/limpchimp/foo/.git/
$ pwd&&&&& git pwd
/ home / limpchimp / foo
/ home / limpchimp / foo#great!
$ mkdir bar&& cd bar
$ pwd&&& git pwd
/ home / limpchimp / foo / bar
/ home / limpchimp / foo#uuhhhhhhhhh ...?

似乎git正在将当前工作目录更改为第一个具有 .git 文件夹(如果存在)。这是非常有问题的;它正在拧紧我写的某些脚本,这些脚本旨在在特定目录中运行,并使我无法使用某些东西作为git别名。有没有解决的办法?如何修复?

解决方案

这是根据设计在文档中明确指定的,如果需要,还有解决方法。 p>

Git配置别名


请注意,shell命令将从存储库的顶级目录
执行,这可能不一定是当前目录。
GIT_PREFIX设置为从原始当前目录运行git rev-parse --show-prefix
返回的值



Summary: the current working directory of commands run through git aliases is wrong.

The easiest way to demonstrate this is to have a git alias like so:

[alias]
    pwd = !pwd

So git pwd is just running the bash command pwd. One would think that the two commands' outputs would be the same. Now, let's try this out a few times:

$ cd ~
$ pwd && git pwd
/home/limpchimp
/home/limpchimp    # great!
$ mkdir foo && cd foo && git init
Initialized empty Git repository in /home/limpchimp/foo/.git/
$ pwd && git pwd
/home/limpchimp/foo
/home/limpchimp/foo  # great!
$ mkdir bar && cd bar
$ pwd && git pwd
/home/limpchimp/foo/bar
/home/limpchimp/foo   # uuhhhhhhhh...?

It seems that git is changing the current working directory to be the first parent directory that has a .git folder (if one exists). This is very problematic; it's screwing up certain scripts that I've written, which are meant to operate in a specific directory, and making me unable to use certain things as git aliases. Is there a way around this? How can I fix it?

解决方案

This is clearly specified in the documentation as by design along with a workaround if needed.

Git 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别名在错误的目录下运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆