Tim*_*Tim 3782 git github git-remote
几天前我从GitHub上撤了一个项目.我已经发现GitHub上有几个叉子,我忽略了我最初采用的那个.如何确定我拉出的哪个叉子?
Jar*_*Par 5344
如果只想要远程URL,或者参照完整性已被破坏:
git config --get remote.origin.url
Run Code Online (Sandbox Code Playgroud)
如果您需要完整输出或参照完整性完好无损:
git remote show origin
Run Code Online (Sandbox Code Playgroud)
当使用git clone
(来自GitHub或任何源存储库)时,克隆源的默认名称是"origin".使用git remote show
将显示有关此远程名称的信息.前几行应显示:
C:\Users\jaredpar\VsVim> git remote show origin
* remote origin
Fetch URL: git@github.com:jaredpar/VsVim.git
Push URL: git@github.com:jaredpar/VsVim.git
HEAD branch: master
Remote branches:
Run Code Online (Sandbox Code Playgroud)
如果要使用脚本中的值,可以使用此答案中列出的第一个命令.
Cas*_*bel 600
如果您希望将此用于脚本编写,则只能获取URL
git config --get remote.origin.url
Run Code Online (Sandbox Code Playgroud)
Mon*_*aro 522
你可以试试:
git remote -v
Run Code Online (Sandbox Code Playgroud)
它将打印所有遥控器的提取/推送URL.
Car*_*ter 244
得到答案:
git ls-remote --get-url [REMOTE]
Run Code Online (Sandbox Code Playgroud)
这比阅读配置更好; 请参阅手册页git-ls-remote
:
--get-URL
扩展给定远程存储库的URL,同时考虑任何
"url.<base>.insteadOf"
配置设置(请参阅git-config(1)
)并退出而不与远程通信.
正如@Jefromi所指出的,这个选项在v1.7.5中添加,直到v1.7.12.2(2012-09)才记录.
Von*_*onC 104
使用Git 2.7(2015年1月5日发布),您可以使用git remote
以下方法获得更加一致的解决方案:
git remote get-url origin
Run Code Online (Sandbox Code Playgroud)
(漂亮的吊坠git remote set-url origin <newurl>
)
见Ben Boeckel()提交96f78d3(2015年9月16日).(由Junio C Hamano合并- -在提交e437cbd,2015年10月5日):mathstuf
gitster
remote:添加get-url子命令
扩展
insteadOf
是其中的一部分,ls-remote --url
也没有办法扩展pushInsteadOf
.
添加get-url
子命令以便能够查询这两个子命令以及获取所有已配置URL的方法.
get-url:
Run Code Online (Sandbox Code Playgroud)
检索远程的URL.这里的
配置insteadOf
和pushInsteadOf
扩展.
默认情况下,仅列出第一个URL.
- 使用'
--push
',查询推送URL而不是提取URL.- 使用'
--all
',将列出远程的所有URL.
在git 2.7之前,你有:
git config --get remote.[REMOTE].url
git ls-remote --get-url [REMOTE]
git remote show [REMOTE]
Run Code Online (Sandbox Code Playgroud)
nop*_*ole 56
总而言之,至少有四种方式:
(以下是针对官方Linux存储库的尝试)
最少信息:
$ git config --get remote.origin.url
https://github.com/torvalds/linux.git
Run Code Online (Sandbox Code Playgroud)
和
$ git ls-remote --get-url
https://github.com/torvalds/linux.git
Run Code Online (Sandbox Code Playgroud)
更多信息:
$ git remote -v
origin https://github.com/torvalds/linux.git (fetch)
origin https://github.com/torvalds/linux.git (push)
Run Code Online (Sandbox Code Playgroud)
更多信息:
$ git remote show origin
* remote origin
Fetch URL: https://github.com/torvalds/linux.git
Push URL: https://github.com/torvalds/linux.git
HEAD branch: master
Remote branch:
master tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)
Run Code Online (Sandbox Code Playgroud)
Cas*_*sey 21
简短回答:
$ git remote show -n origin
Run Code Online (Sandbox Code Playgroud)
或者,纯粹快速脚本的替代方案:
$ git config --get remote.origin.url
Run Code Online (Sandbox Code Playgroud)
一些信息:
$ git remote -v
将打印所有遥控器(不是你想要的).你想要原产地吗?$ git remote show origin
更好,仅显示origin
但需要太长时间(在git版本1.8.1.msysgit.1上测试).我最终得到:$ git remote show -n origin
,这似乎是最快的.使用-n
它不会获取远程头(AKA分支).你不需要那种类型的信息,对吗?
http://www.kernel.org/pub//software/scm/git/docs/git-remote.html
您可以应用于| grep -i fetch
所有三个版本以仅显示提取URL.
如果您需要纯粹的速度,那么使用:
$ git config --get remote.origin.url
Run Code Online (Sandbox Code Playgroud)
感谢@Jefromi指出这一点.
Wit*_*rba 18
我更喜欢这个,因为它更容易记住:
git config -l
Run Code Online (Sandbox Code Playgroud)
它将列出所有有用的信息,例如:
user.name=Your Name
user.email=your.name@notexisting.com
core.autocrlf=input
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://github.com/mapstruct/mapstruct-examples
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
Run Code Online (Sandbox Code Playgroud)
Cod*_*nox 16
对我来说,这是更简单的方法(无需输入):
$ git remote -v
origin https://github.com/torvalds/linux.git (fetch)
origin https://github.com/torvalds/linux.git (push)
Run Code Online (Sandbox Code Playgroud)
实际上,我已经将其放入了一个可以执行的alias
调用s
中:
git remote -v
git status
Run Code Online (Sandbox Code Playgroud)
您可以使用以下方法添加到您的个人资料:
alias s='git remote -v && git status'
Har*_*nan 14
Git URL将在Git配置文件中.该值对应于键url
:
cd project_dir
cat .git/config | grep url | awk '{print $3}'
Run Code Online (Sandbox Code Playgroud)
注意:即使您处于脱机状态或远程git服务器已关闭,这也将起作用.
Wha*_*ool 14
我永远不会记住Git命令的所有参数,所以我只是在~/.gitconfig
文件中添加了一个对我来说更有意义的别名,所以我记得它,并且它导致更少的输入:
[alias]
url = ls-remote --get-url
Run Code Online (Sandbox Code Playgroud)
重新加载终端后,您只需键入:
> git url
Run Code Online (Sandbox Code Playgroud)
以下是我经常使用的一些内容:
[alias]
cd = checkout
ls = branch
lsr = branch --remote
lst = describe --tags
Run Code Online (Sandbox Code Playgroud)
may*_*ʎɐɯ 12
我主要使用:
git remote get-url origin
Run Code Online (Sandbox Code Playgroud)
它适用于Windows中的Git Bash命令控制台或CMD命令控制台.也就是说,它适用于Git版本2.x.
上游的遥控器可能不被称为"原点",所以这里有一个变化:
remote=$(git config --get branch.master.remote)
url=$(git config --get remote.$remote.url)
basename=$(basename "$url" .git)
echo $basename
Run Code Online (Sandbox Code Playgroud)
要么:
basename $(git config --get remote.$(git config --get branch.master.remote).url) .git
Run Code Online (Sandbox Code Playgroud)
对于更有用的变量,有:
$ git config -l
Run Code Online (Sandbox Code Playgroud)
origin
对于ssh://
存储库:
git ls-remote --get-url origin | cut -f 2 -d @ | cut -f 1 -d "/"
Run Code Online (Sandbox Code Playgroud)
对于git://
存储库:
git ls-remote --get-url origin | cut -f 2 -d @ | cut -f 1 -d ":"
Run Code Online (Sandbox Code Playgroud)
为了补充其他答案:如果遥控器由于某种原因被更改,因此不能反映原始原点,则reflog中的第一个条目(即命令显示的最后一个条目git reflog
)应指示最初克隆repo的位置从.
例如
$ git reflog | tail -n 1
f34be46 HEAD@{0}: clone: from https://github.com/git/git
$
Run Code Online (Sandbox Code Playgroud)
(请记住,可能会清除reflog,因此无法保证其正常工作.)
您使用 SSH 克隆克隆了您的存储库。
git config --get remote.origin.url
git@gitlab.com:company/product/production.git
Run Code Online (Sandbox Code Playgroud)
但是你想获取 http url 以在浏览器中打开它或共享它:
git config --get remote.origin.url | sed -e 's/:/\//g'| sed -e 's/ssh\/\/\///g'| sed -e 's/git@/https:\/\//g'
https://gitlab.com/company/product/production.git
Run Code Online (Sandbox Code Playgroud)
GitHub 或 GitLab 无关紧要。
随着git remote show origin
你必须在项目目录.但是,如果您想从其他任何地方确定URL,您可以使用:
cat <path2project>/.git/config | grep url
Run Code Online (Sandbox Code Playgroud)
如果您经常需要此命令,则可以在MacOS 中.bashrc
或.bash_profile
使用MacOS 定义别名.
alias giturl='cat ./.git/config | grep url'
Run Code Online (Sandbox Code Playgroud)
因此,您只需要调用giturl
Git根文件夹即可获取其URL.
如果你像这样扩展这个别名
alias giturl='cat .git/config | grep -i url | cut -d'=' -f 2'
Run Code Online (Sandbox Code Playgroud)
你只得到没有前面的普通URL
"URL ="
在
你有更多的使用可能性:
例
在Mac上,您可以调用open $(giturl)
在标准浏览器中打开URL.
或者chrome $(giturl)
在Linux上使用Chrome浏览器打开它.
一种简单的方法是打开.git/config
文件:
cat .git/config
Run Code Online (Sandbox Code Playgroud)
编辑:
vim .git/config
要么
nano .git/config
打印任意命名的远程提取URL:
git remote -v | grep fetch | awk '{print $2}'
Run Code Online (Sandbox Code Playgroud)
#!/bin/bash
git-remote-url() {
local rmt=$1; shift || { printf "Usage: git-remote-url [REMOTE]\n" >&2; return 1; }
local url
if ! git config --get remote.${rmt}.url &>/dev/null; then
printf "%s\n" "Error: not a valid remote name" && return 1
# Verify remote using 'git remote -v' command
fi
url=`git config --get remote.${rmt}.url`
# Parse remote if local clone used SSH checkout
[[ "$url" == git@* ]] \
&& { url="https://github.com/${url##*:}" >&2; }; \
{ url="${url%%.git}" >&2; };
printf "%s\n" "$url"
}
Run Code Online (Sandbox Code Playgroud)
用法:
# Either launch a new terminal and copy `git-remote-url` into the current shell process,
# or create a shell script and add it to the PATH to enable command invocation with bash.
# Create a local clone of your repo with SSH, or HTTPS
git clone git@github.com:your-username/your-repository.git
cd your-repository
git-remote-url origin
Run Code Online (Sandbox Code Playgroud)
输出:
https://github.com/your-username/your-repository
Run Code Online (Sandbox Code Playgroud)
仅获取远程 URL:
git config --get remote.origin.url
Run Code Online (Sandbox Code Playgroud)
为了获得有关特定遥控器的更多详细信息,请使用
git remote show [remote-name] command
Run Code Online (Sandbox Code Playgroud)
要查看远程 URL:
git remote show origin
Run Code Online (Sandbox Code Playgroud)
要查看 .git 文件夹放置的位置:
git config --get remote.origin.url
Run Code Online (Sandbox Code Playgroud)
如果您不知道分支的上游远程名称,则可以通过检查当前分支所基于的上游分支名称来首先查找。git rev-parse
像这样使用:
git rev-parse --symbolic-full-name --abbrev-ref @{upstream}
Run Code Online (Sandbox Code Playgroud)
这表明上游分支是当前分支的源。可以这样解析以获取远程名称:
git rev-parse --symbolic-full-name --abbrev-ref @{upstream} | cut -d / -f 1
Run Code Online (Sandbox Code Playgroud)
现在,将其传递给git ls-remote
您,您将获得作为当前分支源的上游远程站点的URL:
git ls-remote --get-url \
$(git rev-parse --symbolic-full-name --abbrev-ref @{upstream} | cut -d / -f 1)
Run Code Online (Sandbox Code Playgroud)
现在应该注意,这不必与从其克隆的源远程存储库相同。但是,在许多情况下,这就足够了。