git别名在错误的目录中操作 [英] git aliases operate in the wrong directory

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

问题描述

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

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

证明这一点的最简单方法是使用一个像这样的 git 别名:

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

[alias]
    pwd = !pwd

所以git pwd 只是运行bash 命令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...?

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

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 配置别名

注意shell命令会从顶级目录执行的存储库,它可能不一定是当前目录.GIT_PREFIX 设置为通过运行 git rev-parse --show-prefix 返回从原来的当前目录

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天全站免登陆