How can I determine the URL that a local Git repository was originally cloned from?
几天前我从Github搞了一个项目。从那以后,我发现Github上有几个叉子,我忘了记下原来是哪一个。我怎样才能确定我拉的是哪个叉子?
如果只需要远程URL,或者引用完整性已被破坏:
1 | git config --get remote.origin.url |
如果需要完整的输出或引用完整性,请执行以下操作:
1 | git remote show origin |
使用
1 2 3 4 5 6 | C:\Users\jaredpar\VsVim> git remote show origin * remote origin Fetch URL: [email protected]:jaredpar/VsVim.git Push URL: [email protected]:jaredpar/VsVim.git HEAD branch: master Remote branches: |
如果要使用脚本中的值,则将使用此答案中列出的第一个命令。
如果您希望将其用于脚本目的,则只能使用
1 | git config --get remote.origin.url |
你可以试试:
1 | git remote -v |
它将打印所有遥控器的获取/推送URL。
要得到答案:
1 | git ls-remote --get-url [REMOTE] |
这比读取配置更好;请参阅
--get-url
Expand the URL of the given remote repository taking into account
any"url. config setting (See.insteadOf" git-config(1) ) and
exit without talking to the remote.
正如@jefromi所指出的,这个选项是在v1.7.5中添加的,直到v1.7.12.2(2012-09)才被记录下来。
使用Git2.7(2015年1月5日发布),您可以使用
1 | git remote get-url origin |
(埃多克斯的漂亮吊坠1〔15〕)
见Ben Boeckel(
remote: add get-url subcommand
Expanding
insteadOf is a part ofls-remote --url and there is no way
to expandpushInsteadOf as well.
Add aget-url subcommand to be able to query both as well as a way to get all configured URLs.
1 | get-url: |
Retrieves the URLs for a remote.
Configurations forinsteadOf andpushInsteadOf are expanded here.
By default, only the first URL is listed.
- With '
--push ', push URLs are queried rather than fetch URLs.- With '
--all ', all URLs for the remote will be listed.
在Git2.7之前,您有:
1 2 3 | git config --get remote.[REMOTE].url git ls-remote --get-url [REMOTE] git remote show [REMOTE] |
总而言之,至少有四种方法:
(以下是针对官方Linux存储库进行的尝试)
最少信息:
1 2 | $ git config --get remote.origin.url https://github.com/torvalds/linux.git |
和
1 2 | $ git ls-remote --get-url https://github.com/torvalds/linux.git |
更多信息:
1 2 3 | $ git remote -v origin https://github.com/torvalds/linux.git (fetch) origin https://github.com/torvalds/linux.git (push) |
更多信息:
1 2 3 4 5 6 7 8 9 10 11 | $ 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) |
简短回答:
1 | $ git remote show -n origin |
或者,纯快速脚本的替代方案:
1 | $ git config --get remote.origin.url |
一些信息:
我最后得到的是:
http://www.kernel.org/pub//software/scm/git/docs/git-remote.html
您可以将
如果您需要纯速度,请使用:
1 | $ git config --get remote.origin.url |
感谢@jefromi指出这一点。
我想你可以在
《Git的URL将Git里面的配置文件。这两个值corresponds
Mac和Linux使用下面的commands:
1 2 | cd project_dir cat .git/config | grep url | awk '{print $3}' |
打开Windows下面的文件在任何文本编辑器和找到的
1 | project_dir/.git/config |
注:本会的工作,即使你是脱机或远程的Git服务器已经由下。
我不记得所有的两个参数的git commands,所以只要把安在
1 2 | [alias] url = ls-remote --get-url |
在《reloading终端,你可以有完全的类型:
1 | > git url |
这是一个少享受我的竞相使用酮:
1 2 3 4 5 | [alias] cd = checkout ls = branch lsr = branch --remote lst = describe --tags |
在basically使用。
1 | git remote get-url origin |
信息工程的Git Bash命令控制台或命令控制台命令在Windows。这是说,与信息工程学院的Git版本2。x。
上游的偏远地区可能不被称为"起源地",所以这里有一个变化:
1 2 3 4 | remote=$(git config --get branch.master.remote) url=$(git config --get remote.$remote.url) basename=$(basename"$url" .git) echo $basename |
或:
1 | basename $(git config --get remote.$(git config --get branch.master.remote).url) .git |
对于更有用的变量,有:
1 | $ git config -l |
补充其他答案:如果由于某种原因遥控器发生了更改,因此无法反映原始来源,则reflog中的第一个条目(即命令
例如
1 2 3 | $ git reflog | tail -n 1 f34be46 HEAD@{0}: clone: from https://github.com/git/git $ |
(请记住,回流可能会被清除,因此这不一定有效。)
对于我,这是更容易吗?(不typlng)的方式:
1 2 3 | $ git remote -v origin https://github.com/torvalds/linux.git (fetch) origin https://github.com/torvalds/linux.git (push) |
事实上,我有一个称为
1 2 | git remote -v git status |
你可以添加你的配置文件有两个:
获取
对于
1 | git ls-remote --get-url origin | cut -f 2 -d @ | cut -f 1 -d"/" |
对于
1 | git ls-remote --get-url origin | cut -f 2 -d @ | cut -f 1 -d":" |
一种简单的方法是打开
1 | cat .git/config |
编辑:
使用
1 | cat <path2project>/.git/config | grep url |
如果您经常需要这个命令,您可以使用MacOS在您的
1 | alias giturl='cat ./.git/config | grep url' |
所以您只需要调用git根文件夹中的
如果您像这样扩展这个别名
1 | alias giturl='cat .git/config | grep -i url | cut -d'=' -f 2' |
你只得到一个没有前面的普通URL
"url="
在里面
url=http://example.com/repo.git
您可以在使用中获得更多的可能性:
例子
在Mac上,您可以调用
或者使用Linux上的chrome浏览器打开
打印任意命名的远程获取URL:
1 | git remote -v | grep fetch | awk '{print $2}' |
如果你不知道这个名字的远程对上游的一个分支,你可以看,一线城市的IP inspecting上游分行,支行名称是建在流。使用这样的
1 | git rev-parse --symbolic-full-name --abbrev-ref @{upstream} |
这个节目,是上游分支,分支的电流源。这两个parsed可以把这样的远程名称:
1 | git rev-parse --symbolic-full-name --abbrev-ref @{upstream} | cut -d / -f 1 |
现在把这两个
1 2 | git ls-remote --get-url \ $(git rev-parse --symbolic-full-name --abbrev-ref @{upstream} | cut -d / -f 1) |
现在,它应该是noted,这是不一样的necessarily远程数据库源码,这是cloned虔诚。然而,在许多案例信息就足够了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #!/bin/bash git-remote-url() { local rmt=$1; shift || { printf"Usage: git-remote-url [REMOTE] ">&2; return 1; } local url if ! git config --get remote.${rmt}.url &>/dev/null; then printf"%s ""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 ""$url" } |
使用:
1 2 3 4 5 6 7 8 | # 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 [email protected]:your-username/your-repository.git cd your-repository git-remote-url origin |
输出:
1 | https://github.com/your-username/your-repository |