关于github:如何确定最初克隆本地Git存储库的URL?

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

使用git clone时(从Github或任何相关的源存储库),克隆源的默认名称是"origin"。使用git remote show将显示有关此远程名称的信息。前几行应该显示:

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]

这比读取配置更好;请参阅git-ls-remote的手册页:

--get-url

Expand the URL of the given remote repository taking into account
any "url..insteadOf" config setting (See 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日发布),您可以使用git remote获得更为一致的解决方案:

1
git remote get-url origin

(埃多克斯的漂亮吊坠1〔15〕)

见Ben Boeckel(mathstuf的Commit 96F78D3(2015年9月16日)。(由Junio C Hamano--gitster于2015年10月5日在Commit E437CBD合并)

remote: add get-url subcommand

Expanding insteadOf is a part of ls-remote --url and there is no way
to expand pushInsteadOf as well.
Add a get-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 for insteadOf and pushInsteadOf 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

一些信息:

  • $ git remote -v将打印所有遥控器(不是您想要的)。你想要原点对吗?
  • $ git remote show origin好得多,只显示origin但时间太长(在git版本1.8.1.msysgit.1上测试)。
  • 我最后得到的是:$ git remote show -n origin,这似乎是最快的。使用-n,它不会获取远程头(即分支)。你不需要那种信息,对吧?

    http://www.kernel.org/pub//software/scm/git/docs/git-remote.html

    您可以将| grep -i fetch应用于所有三个版本,以仅显示fetch URL。

    如果您需要纯速度,请使用:

    1
    $ git config --get remote.origin.url

    感谢@jefromi指出这一点。


    我想你可以在.git/configremote["origin"]下找到它,如果你不操纵它的话。


    《Git的URL将Git里面的配置文件。这两个值corresponds url是关键。

    Mac和Linux使用下面的commands:

    1
    2
     cd project_dir
     cat .git/config | grep url | awk '{print $3}'

    打开Windows下面的文件在任何文本编辑器和找到的url密钥值。

    1
    project_dir/.git/config

    注:本会的工作,即使你是脱机或远程的Git服务器已经由下。


    我不记得所有的两个参数的git commands,所以只要把安在~/.gitconfig别名文件,使得更多的了解我,所以我记得它,和它的结果,在不typlng:

    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中的第一个条目(即命令git reflog显示的最后一个条目)应指示repo最初从何处克隆。

    例如

    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)

    事实上,我有一个称为aliass:是的。

    1
    2
    git remote -v
    git status

    你可以添加你的配置文件有两个: alias s='git remote -v && git status'


    获取origin的IP地址/主机名

    对于ssh://存储库:

    1
    git ls-remote --get-url origin | cut -f 2 -d @ | cut -f 1 -d"/"

    对于git://存储库:

    1
    git ls-remote --get-url origin | cut -f 2 -d @ | cut -f 1 -d":"


    一种简单的方法是打开.git/config文件:

    1
    cat .git/config

    编辑:

    vim .git/config

    nano .git/config


    使用git remote show origin时,您必须在项目目录中。但是,如果您想从其他任何地方确定URL你可以使用:

    1
    cat <path2project>/.git/config | grep url

    如果您经常需要这个命令,您可以使用MacOS在您的.bashrc.bash_profile中定义别名。

    1
    alias giturl='cat ./.git/config | grep url'

    所以您只需要调用git根文件夹中的giturl,就可以简单地获取其URL。

    如果您像这样扩展这个别名

    1
    alias giturl='cat .git/config | grep -i url | cut -d'=' -f 2'

    你只得到一个没有前面的普通URL

    "url="

    在里面

    url=http://example.com/repo.git

    您可以在使用中获得更多的可能性:

    例子

    在Mac上,您可以调用open $(giturl)在标准浏览器中打开URL。

    或者使用Linux上的chrome浏览器打开chrome $(giturl)


    打印任意命名的远程获取URL:

    1
    git remote -v | grep fetch | awk '{print $2}'

    如果你不知道这个名字的远程对上游的一个分支,你可以看,一线城市的IP inspecting上游分行,支行名称是建在流。使用这样的git rev-parse

    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

    现在把这两个git ls-remote和管它,你会把URL的上游遥这是电流源的分支:

    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