是否有返回当前项目名称的 git 命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8190392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a git command that returns the current project name?
提问by brock
Does git have a built-in command for showing the name of the current remote project? Right now I'm using this:
git 是否有内置命令用于显示当前远程项目的名称?现在我正在使用这个:
git remote -v | head -n1 | awk '{print }' | sed 's/.*\///' | sed 's/\.git//'
...but it seems like there would be a built-in equivalent.
...但似乎会有一个内置的等价物。
采纳答案by Jacob Groundwater
It looks like your script is pulling the last part of the remote URL and using that as the project name. This works when using a single remote site, such as http://bitbucket.orgbut your system will not work universally across all users of that repository.
看起来您的脚本正在提取远程 URL 的最后一部分并将其用作项目名称。这在使用单个远程站点(例如http://bitbucket.org)时有效,但您的系统不会在该存储库的所有用户之间普遍运行。
Users generally all have different remote repositories, in fact on many projects you will have multiple remotes. Git does not care where a repository comes from when fetching and merging. The merge algorithm will even accept branches with no common ancestor.
用户通常都有不同的远程存储库,实际上在许多项目中您将拥有多个远程存储库。在获取和合并时,Git 并不关心存储库的来源。合并算法甚至会接受没有共同祖先的分支。
The only real solution is to create a text file in the root of each repository that contains the project name. This solution will work across all users, regardless of how they setup their remotes.
唯一真正的解决方案是在每个存储库的根目录中创建一个包含项目名称的文本文件。该解决方案适用于所有用户,无论他们如何设置遥控器。
回答by sbz
I was looking for same information in order to customize my shell prompt, so I decided to give a try and ended up with this command which output just the name of the project:
我正在寻找相同的信息来自定义我的 shell 提示,所以我决定尝试一下,最终得到了这个命令,它只输出项目的名称:
$ git config --local remote.origin.url|sed -n 's#.*/\([^.]*\)\.git##p'
It should works in any case if your remote origin url is SSH, HTTPS with DNS or IP based.
如果您的远程源 url 是基于 DNS 或 IP 的 SSH、HTTPS,它应该在任何情况下都可以工作。
- SSH based
- dns : ssh://[email protected]:user/project.git
- ip : ssh://[email protected]:user/project.git
- HTTPS based
- 基于 SSH
- dns : ssh://[email protected]:user/project.git
- ip:ssh://[email protected]:user/project.git
- 基于HTTPS
If you don't have remote configured, only a local repository and your top level folder is the name of the project you can use git rev-parse and basename inside your git tree (not reliable solution). It will output the project name:
如果您没有远程配置,只有一个本地存储库和您的顶级文件夹是项目的名称,您可以在 git 树中使用 git rev-parse 和 basename (不可靠的解决方案)。它将输出项目名称:
TOP=$(git rev-parse --show-toplevel); echo ${TOP##*/}
NB: GH doesn't allow you to clone using IP directly on HTTPS because certificate chain validation. It was just to illustrate the use cases.
注意:由于证书链验证,GH 不允许您直接在 HTTPS 上使用 IP 进行克隆。这只是为了说明用例。
回答by mugiseyebrows
For remote folder name it'd be better
对于远程文件夹名称,它会更好
git remote -v | head -n1 | awk '{print }' | sed -e 's,.*:\(.*/\)\?,,' -e 's/\.git$//'
because it could be [email protected]:repo_name - without any slash.
因为它可能是 [email protected]:repo_name - 没有任何斜线。
回答by jsxt
The command git remote -v
can not be assumed as reliable because your repository can work with more than one remote repositories. For example, your project is your-project
and you have added another-project
. After the command you are expecting to see the name of your project but you'll see the name of another project:
git remote -v
不能假定该命令是可靠的,因为您的存储库可以与多个远程存储库一起使用。例如,您的项目是your-project
并且您已添加another-project
. 在命令之后,您希望看到您的项目名称,但您将看到另一个项目的名称:
$ git remote -v | head -n1
ABC https://git.team1.ourcompany.com/another-project.git (fetch)
ABC https://git.team1.ourcompany.com/another-project.git (push)
origin https://git.ourcompany.com/your-project.git (fetch)
origin https://git.ourcompany.com/your-project.git (push)
What I could suggest is to check your repository's configuration, for example:
我可以建议的是检查您的存储库的配置,例如:
$ git config --local remote.origin.url
https://git.ourcompany.com/your-project.git
In the first approach this is the more reliable but doesn't give 100% insurance.
在第一种方法中,这种方法更可靠,但不能提供 100% 的保险。
回答by Jim Mitchener
There is no such thing as a project name in Git. You are simply getting the name of the folder the repository is located in remotely. Git has no built-in way of computing this as it has absolutely no use for it.
Git 中没有项目名称这样的东西。您只是在远程获取存储库所在文件夹的名称。Git 没有内置的计算方式,因为它完全没有用。
回答by Linus Sw?las
I would like to point out the same answer @Mike mentioned in the comments to your question, the $GIT_DIR/description
file. Some other software use this for the nameof the repository, such as the post-receive-email hook script. (Which actually does sed -ne '1p' "$GIT_DIR/description"
) and thus simply use the first line from that file as the name of the repository.
我想指出@Mike 在对您的问题的评论中提到的相同答案,即$GIT_DIR/description
文件。其他一些软件使用它作为存储库的名称,例如post-receive-email hook script。(实际上确实如此sed -ne '1p' "$GIT_DIR/description"
),因此只需使用该文件的第一行作为存储库的名称。
回答by A.H.
Chained head
awk
and sed
calls like this
像这样链接head
awk
和sed
调用
git remote -v | head -n1 | awk '{print }' | sed 's/.*\///' | sed 's/\.git//'
can be combined into one sed
call like this:
可以组合成一个这样的sed
调用:
git remote -v | sed -rn '1s#.*/(.*)\.git.*##p'
回答by Maoz Zadok
basename $(git config remote.origin.url |sed "s/\.git$//")
or:
或者:
git config remote.origin.url |sed 's#.*\/\(.*\)\.git##'
回答by manojlds
The remote url or the folder in which the git repo is kept can be anything as far as Git is concerned. So you do not have a built-in way for checking this. Git cannot identify what the name of the project is. You have to ( or the owner of the project). Usually, this will be in the form of a README or some similar file checked into the repository that gives the name of the project.
就 Git 而言,远程 url 或保存 git repo 的文件夹可以是任何东西。所以你没有一个内置的方法来检查这个。Git 无法识别项目的名称。您必须(或项目的所有者)。通常,这将以 README 或某些类似文件的形式签入存储库,并提供项目名称。
回答by Adam Dymitruk
What you are doing is fine. I would not trust that someone won't change the name in a readme file. Use the URL as you are doing. Decide on a convention so that origin always refers to the central repo where the urls will contain the identity.
你在做什么很好。我不相信有人不会更改自述文件中的名称。按照您的操作使用 URL。确定一个约定,以便 origin 始终指代 url 将包含身份的中央存储库。