如何确定本地Git存储库最初从中克隆的URL?
几天前,我从GitHub中提取了一个项目。后来我发现GitHub上有几个叉子,我忘了记下我最初使用的叉子。我如何确定我拉了哪些分叉?如果您只需要远程URL,或者您的URL未连接到可以到达远程回购的网络:如何确定本地Git存储库最初从中克隆的URL?,git,github,git-remote,Git,Github,Git Remote,几天前,我从GitHub中提取了一个项目。后来我发现GitHub上有几个叉子,我忘了记下我最初使用的叉子。我如何确定我拉了哪些分叉?如果您只需要远程URL,或者您的URL未连接到可以到达远程回购的网络: git config --get remote.origin.url 如果您需要完整输出,并且您所在的网络可以到达源站所在的远程回购: git remote show origin 使用git clone(从GitHub或任何源存储库获取)时,克隆源的默认名称为“origin”。使用git
git config --get remote.origin.url
如果您需要完整输出,并且您所在的网络可以到达源站所在的远程回购:
git remote show origin
使用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:
如果您想使用脚本中的值,可以使用此答案中列出的第一个命令。我想您可以在
.git/config
和remote[“origin”]
下找到它,如果您没有对其进行操作。如果您想使用此命令编写脚本,您只能使用
git config --get remote.origin.url
您可以尝试:
git remote -v
它将打印所有远程设备的获取/推送URL。打印任意命名的远程获取URL:
git remote -v | grep fetch | awk '{print $2}'
简短答复:
$ git remote show -n origin
或者,纯快速脚本的替代方案:
$ git config --get remote.origin.url
一些信息:
$git remote-v
将打印所有遥控器(不是您想要的)。你想要原产地,对吗$git remote show origin
更好,只显示origin
,但时间太长(在git版本1.8.1.msysgit.1上测试)$git remote show-n origin
,这似乎是最快的。使用-n
它将不会获取远程头(也称为分支)。你不需要那种类型的信息,对吧
您可以对所有三个版本应用| grep-i fetch
,以仅显示fetch URL
如果您需要纯速度,请使用:
$ git config --get remote.origin.url
感谢您指出这一点。上游的遥控器可能不被称为“原点”,因此这里有一个变体:
remote=$(git config --get branch.master.remote)
url=$(git config --get remote.$remote.url)
basename=$(basename "$url" .git)
echo $basename
或:
对于更有用的变量,有:
$ git config -l
要得到答案:
git ls-remote --get-url [REMOTE]
这比读取配置要好;请参阅:
——获取url
展开给定远程存储库的URL,并考虑
任何“url..而不是”
config设置(请参见git config(1)
)和
在不与遥控器通话的情况下退出
正如@Jefromi所指出的,该选项是在中添加的,直到(2012-09年)才被记录在案。总而言之,至少有四种方法:
(以下内容已在官方Linux存储库中试用)
最少信息:
及
更多信息:
更多信息:
要获取origin的IP地址/主机名
对于ssh://
存储库:
git ls-remote --get-url origin | cut -f 2 -d @ | cut -f 1 -d "/"
git ls-remote --get-url origin | cut -f 2 -d @ | cut -f 1 -d ":"
对于git://
存储库:
git ls-remote --get-url origin | cut -f 2 -d @ | cut -f 1 -d "/"
git ls-remote --get-url origin | cut -f 2 -d @ | cut -f 1 -d ":"
使用Git 2.7(2015年1月5日发布),您可以使用Git-remote
,获得更加一致的解决方案:
git remote get-url origin
(git远程设置url来源的漂亮挂件)
参见(2015年9月16日)作者。(于2015年10月5日合并): 远程:添加获取url子命令 扩展
而不是
是ls remote--url的一部分,没有办法
要展开请按而不是。
添加一个get url
子命令,以便能够同时查询这两个url以及获取所有已配置url的方法
检索远程服务器的URL。
此处扩展了代替
和推代替
的配置。
默认情况下,仅列出第一个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]
补充其他答案:如果遥控器因某种原因已被更改,因此未反映原始来源,则reflog中的第一个条目(即命令git reflog
显示的最后一个条目)应指示repo最初从何处克隆
e、 g
(请记住,reflog可能会被清除,因此这不能保证工作。)使用git remote show origin
时,您必须位于projects目录中。但是如果您想从其他任何地方确定URL
您可以使用:
cat <path2project>/.git/config | grep url
因此,您只需要在Git根文件夹中调用giturl
,就可以简单地获得它的URL
如果你像这样扩展这个别名
alias giturl='cat .git/config | grep -i url | cut -d'=' -f 2'
您只获得普通URL,而没有前面的
“url=”
在
网址=
您可以在其使用中获得更多的可能性:
示例
在Mac上,您可以调用open$(giturl)
在标准浏览器中打开URL
或者chrome$(giturl)
在Linux上用chrome浏览器打开它。一个简单的方法是打开.git/config
文件:
cat .git/config
要编辑:
vim.git/config
或
nano.git/config
如果您不知道分支的上游远程服务器的名称,可以首先通过检查当前分支所基于的上游分支名称来查找该名称。像这样使用git rev parse
:
git rev-parse --symbolic-full-name --abbrev-ref @{upstream}
git rev-parse --symbolic-full-name --abbrev-ref @{upstream} | cut -d / -f 1
这表明上游分支是当前分支的源。可以对其进行解析以获得如下远程名称:
git rev-parse --symbolic-full-name --abbrev-ref @{upstream}
git rev-parse --symbolic-full-name --abbrev-ref @{upstream} | cut -d / -f 1
现在,将其传输到git ls remote
,您将获得上游远程服务器的URL,该远程服务器是当前分支的源:
git ls-remote --get-url \
$(git rev-parse --symbolic-full-name --abbrev-ref @{upstream} | cut -d / -f 1)
现在应该注意的是,这不一定与从中克隆的源远程存储库相同。但在许多情况下,这就足够了。Git URL将位于Git配置文件中。该值对应于键url
project_dir/.git/config
对于Mac和Linux,请使用以下命令:
cd project_dir
cat .git/config | grep url | awk '{print $3}'
对于Windows,在任何文本编辑器中打开下面的文件,并找到keyurl
的值
project_dir/.git/config
git ls-remote --get-url \
$(git rev-parse --symbolic-full-name --abbrev-ref @{upstream} | cut -d / -f 1)
cd project_dir
cat .git/config | grep url | awk '{print $3}'
project_dir/.git/config
git remote get-url origin
#!/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"
}
# 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
https://github.com/your-username/your-repository
[alias]
url = ls-remote --get-url
> git url
[alias]
cd = checkout
ls = branch
lsr = branch --remote
lst = describe --tags
$ git remote -v
origin https://github.com/torvalds/linux.git (fetch)
origin https://github.com/torvalds/linux.git (push)
git remote -v
git status
git config --list
git config -l
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
git config --get remote.origin.url
git@gitlab.com:company/product/production.git
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
git config --get remote.origin.url
git remote show [remote-name] command
git remote show origin
git config --get remote.origin.url