如何确定本地 Git 存储库最初克隆的 URL

发布于 2024-10-01 04:01:17 字数 55 浏览 3 评论 0原文

我在 GitHub 上拉了一个有多个分支的项目,但忘记了它是哪个分支。如何确定我拔的是哪个叉子?

I pulled a project with several forks on GitHub, but forgot which fork it was. How do I determine which fork I pulled?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(26

-残月青衣踏尘吟 2024-10-08 04:01:17

仅获取远程 URL:

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: [email protected]:jaredpar/VsVim.git
  Push  URL: [email protected]:jaredpar/VsVim.git
  HEAD branch: master
  Remote branches:

如果您想在脚本中使用该值,您将使用此答案中列出的第一个命令。

To obtain only the remote URL:

git config --get remote.origin.url

If you require full output, and you are on a network that can reach the remote repo where the origin resides:

git remote show origin

When using git clone (from GitHub, or any source repository for that matter) the default name for the source of the clone is "origin". Using git remote show will display the information about this remote name. The first few lines should show:

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:

If you want to use the value in a script, you would use the first command listed in this answer.

生死何惧 2024-10-08 04:01:17

这仅给出 URL,这对于脚本编写很有用:

git config --get remote.origin.url

This gives only the URL, which is useful for scripting purposes:

git config --get remote.origin.url
初心 2024-10-08 04:01:17

这将打印所有遥控器的获取/推送 URL:

git remote -v

This will print all your remotes' fetch/push URLs:

git remote -v
深府石板幽径 2024-10-08 04:01:17

得到答案:

git ls-remote --get-url [REMOTE]

这比读配置要好;请参阅 git-ls-remote 的手册页< /a>:

--获取网址

考虑到扩展给定远程存储库的 URL
任何 "url..insteadOf" 配置设置(请参阅 git-config(1))和
退出而不与遥控器对话。

正如 @Jefromi 所指出的,此选项是在 v1.7.5 中添加的,并且直到v1.7.12.2 (2012-09)。

To get the answer:

git ls-remote --get-url [REMOTE]

This is better than reading the configuration; refer to the man page for git-ls-remote:

--get-url

Expand the URL of the given remote repository taking into account
any "url.<base>.insteadOf" config setting (See git-config(1)) and
exit without talking to the remote.

As pointed out by @Jefromi, this option was added in v1.7.5 and not documented until v1.7.12.2 (2012-09).

乖乖哒 2024-10-08 04:01:17

使用 Git 2.7(2015 年 1 月 5 日发布),您可以使用 git remote 获得更连贯的解决方案:(

git remote get-url origin

git remote set-url origin 的漂亮挂件)

请参阅 提交 96f78d3(2015 年 9 月 16 日),作者:Ben Boeckel (mathstuf)
(由 Junio C Hamano -- gitster -- 合并于 提交 e437cbd,2015 年 10 月 5 日)

远程:添加 get-url 子命令

扩展insteadOfls-remote --url的一部分,没有办法
也可以扩展 pushInsteadOf
添加 get-url 子命令以便能够查询两者以及获取所有配置的 URL 的方法。

get-url:

检索远程的 URL。
insteadOfpushInsteadOf 的配置在此处展开​​。
默认情况下,仅列出第一个 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]

With Git 2.7 (release January 5th, 2015), you have a more coherent solution using git remote:

git remote get-url origin

(nice pendant of git remote set-url origin <newurl>)

See commit 96f78d3 (16 Sep 2015) by Ben Boeckel (mathstuf).
(Merged by Junio C Hamano -- gitster -- in commit e437cbd, 05 Oct 2015):

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.

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.

Before git 2.7, you had:

 git config --get remote.[REMOTE].url
 git ls-remote --get-url [REMOTE]
 git remote show [REMOTE]
‖放下 2024-10-08 04:01:17

总而言之,至少有四种方法:

使用官方 Linux 存储库进行尝试:

最少信息:

$ git config --get remote.origin.url
https://github.com/torvalds/linux.git

$ git ls-remote --get-url
https://github.com/torvalds/linux.git

更多信息:

$ git remote -v
origin    https://github.com/torvalds/linux.git (fetch)
origin    https://github.com/torvalds/linux.git (push)

甚至更多信息:

$ 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)

To summarize, there are at least four ways:

Trying it out using the official Linux repository:

Least information:

$ git config --get remote.origin.url
https://github.com/torvalds/linux.git

and

$ git ls-remote --get-url
https://github.com/torvalds/linux.git

More information:

$ git remote -v
origin    https://github.com/torvalds/linux.git (fetch)
origin    https://github.com/torvalds/linux.git (push)

Even more information:

$ 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)
不气馁 2024-10-08 04:01:17

对我来说,这是更简单的方法(更少的打字):

git remote -v

输出:

origin    https://github.com/torvalds/linux.git (fetch)
origin    https://github.com/torvalds/linux.git (push)

实际上,我将其放入名为 salias 中,它的作用是:

git remote -v
git status

您可以使用以下命令添加到您的个人资料中 : :

alias s='git remote -v && git 状态'

For me, this is the easier way (less typing):

git remote -v

Output:

origin    https://github.com/torvalds/linux.git (fetch)
origin    https://github.com/torvalds/linux.git (push)

Actually, I've that into an alias called s that does:

git remote -v
git status

You can add to your profile with:

alias s='git remote -v && git status'

甜尕妞 2024-10-08 04:01:17

我认为如果您没有操作它,您可以在 .git/configremote["origin"] 下找到它。

I think you can find it under .git/config and remote["origin"] if you didn't manipulate that.

向地狱狂奔 2024-10-08 04:01:17

简短的回答:

$ git remote show -n origin

或者,纯快速脚本的替代方案:

$ git config --get remote.origin.url

一些信息:

  1. $ git remote -v 将打印所有遥控器(不是您想要的)。你想要原产地吗?
  2. $ 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 对所有三个版本仅显示获取 URL。

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

$ git config --get remote.origin.url

感谢 @Jefromi 指出这一点。

Short answer:

$ git remote show -n origin

or, an alternative for pure quick scripts:

$ git config --get remote.origin.url

Some info:

  1. $ git remote -v will print all remotes (not what you want). You want origin right?
  2. $ git remote show origin much better, shows only origin but takes too long (tested on git version 1.8.1.msysgit.1).

I ended up with: $ git remote show -n origin, which seems to be fastest. With -n it will not fetch remote heads (AKA branches). You don't need that type of info, right?

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

You can apply | grep -i fetch to all three versions to show only the fetch URL.

If you require pure speed, then use:

$ git config --get remote.origin.url

Thanks to @Jefromi for pointing that out.

落日海湾 2024-10-08 04:01:17

Git URL 将位于 Git 配置文件内。该值对应于键 url

对于 Mac 和 Linux,请使用以下命令:

awk '/url/{print $3}' project_dir/.git/config

对于 Windows,在任何文本编辑器中打开以下文件并找到键 url 的值。

project_dir/.git/config

注意:即使您处于离线状态或远程 Git 服务器已关闭,这也将起作用。

The Git URL will be inside the Git configuration file. The value corresponds to the key url.

For Mac and Linux, use the commands below:

awk '/url/{print $3}' project_dir/.git/config

For Windows, open the below file in any text editor and find the value for key url.

project_dir/.git/config

Note: This will work even if you are offline or the remote Git server has been taken down.

So要识趣 2024-10-08 04:01:17

我永远记不住 Git 命令的所有参数,所以我只是在 ~/.gitconfig 文件中添加一个对我来说更有意义的别名,这样我就可以记住它,并且可以减少输入次数:

[alias]
url = ls-remote --get-url

重新加载终端后,您只需输入:

> git url

以下是我常用的一些:

[alias]
cd = checkout
ls = branch
lsr = branch --remote
lst = describe --tags

我也强烈推荐 git-extras 有一个 git info 命令,它提供有关远程和本地分支的更多详细信息。

I can never remember all the parameters to Git commands, so I just put an alias in the ~/.gitconfig file that makes more sense to me, so I can remember it, and it results in less typing:

[alias]
url = ls-remote --get-url

After reloading the terminal, you can then just type:

> git url

Here are a few more of my frequently used ones:

[alias]
cd = checkout
ls = branch
lsr = branch --remote
lst = describe --tags

I also highly recommend git-extras which has a git info command which provides much more detailed information on the remote and local branches.

落叶缤纷 2024-10-08 04:01:17

我更喜欢这个,因为它更容易记住:

git config -l

它将列出所有有用的信息,例如:

user.name=Your Name
[email protected]
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

I prefer this one as it is easier to remember:

git config -l

It will list all useful information such as:

user.name=Your Name
[email protected]
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
多彩岁月 2024-10-08 04:01:17

我基本上使用:

git remote get-url origin

它适用于 Windows 中的 Git Bash 命令控制台或 CMD 命令控制台。也就是说,它适用于 Git 2.x 版本。

I basically use:

git remote get-url origin

It works for Git Bash command console or CMD command console in Windows. That said, it works with version 2.x of Git.

阳光的暖冬 2024-10-08 04:01:17

上游的远程可能不被称为“origin”,所以这里有一个变体:

remote=$(git config --get branch.master.remote)
url=$(git config --get remote.$remote.url)
basename=$(basename "$url" .git)
echo $basename

或者:

basename $(git config --get remote.$(git config --get branch.master.remote).url) .git

对于更有用的变量,有:

$ git config -l

The upstream's remote may not be called "origin", so here's a variation:

remote=$(git config --get branch.master.remote)
url=$(git config --get remote.$remote.url)
basename=$(basename "$url" .git)
echo $basename

Or:

basename $(git config --get remote.$(git config --get branch.master.remote).url) .git

For more useful variables there's:

$ git config -l
许仙没带伞 2024-10-08 04:01:17
git config --list

此命令将提供与您的存储库相关的所有信息。

git config --list

This command will give all information related to your repository.

乖乖兔^ω^ 2024-10-08 04:01:17

您使用 SSH 克隆克隆了存储库。

git config --get remote.origin.url
[email protected]:company/product/production.git

但您想要获取一个 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

GitHub 或 GitLab 并不重要。

You cloned your repository with SSH clone.

git config --get remote.origin.url
[email protected]:company/product/production.git

But you want to get an HTTP URL to open it in the browser or share it:

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

GitHub or GitLab doesn’t matter.

叹沉浮 2024-10-08 04:01:17

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

cat .git/config

编辑:

vim .git/config

nano .git/config

A simple way is to open the .git/config file:

cat .git/config

To edit:

vim .git/config or

nano .git/config

只为一人 2024-10-08 04:01:17

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

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

Print arbitrarily named remote fetch URLs:

git remote -v | grep fetch | awk '{print $2}'
聊慰 2024-10-08 04:01:17

获取 origin 的 IP 地址/主机名

对于 ssh:// 存储库:

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 ":"

To get the IP address/hostname of origin

For ssh:// repositories:

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

For git:// repositories:

git ls-remote --get-url origin | cut -f 2 -d @ | cut -f 1 -d ":"
离鸿 2024-10-08 04:01:17

补充其他答案:如果遥控器由于某种原因被更改,因此不反映原始来源,则引用日志中的第一个条目(即命令显示的最后一个条目>git reflog) 应指示存储库最初克隆的位置。

例如

$ git reflog | tail -n 1
f34be46 HEAD@{0}: clone: from https://github.com/git/git
$

(请记住,引用日志可能会被清除,所以这不能保证有效。)

To supplement the other answers: If the remote has for some reason been changed and so doesn't reflect the original origin, the very first entry in the reflog (i.e. the last entry displayed by the command git reflog) should indicate where the repo was originally cloned from.

e.g.

$ git reflog | tail -n 1
f34be46 HEAD@{0}: clone: from https://github.com/git/git
$

(Bear in mind that the reflog may be purged, so this isn't guaranteed to work.)

零崎曲识 2024-10-08 04:01:17

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

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

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

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

所以你只需要在Git根文件夹中调用giturl就可以简单地获取它的URL。


如果你像这样扩展这个别名,

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

你只会得到没有前面的纯 URL

“网址=”

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

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

<示例

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

或者 chrome $(giturl) 在 Linux 上使用 Chrome 浏览器打开它。

With git remote show origin you have to be in the projects directory. But if you want to determine the URLs from anywhere else
you could use:

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

If you'll need this command often, you could define an alias in your .bashrc or .bash_profile with MacOS.

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

So you just need to call giturl in the Git root folder in order to simply obtain its URL.


If you extend this alias like this

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

you get only the plain URL without the preceding

"url="

in

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

you get more possibilities in its usage:

Example

On Mac you could call open $(giturl) to open the URL in the standard browser.

Or chrome $(giturl) to open it with the Chrome browser on Linux.

夜灵血窟げ 2024-10-08 04:01:17

如果您不知道分支的上游远程名称,可以首先通过检查构建当前分支的上游分支名称来查找。像这样使用 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 ls-remote ,您将获得作为当前分支源的上游远程的 URL:

git ls-remote --get-url \
  $(git rev-parse --symbolic-full-name --abbrev-ref @{upstream} | cut -d / -f 1)

现在应该注意的是,这不一定与从中克隆的源远程存储库相同。然而,在许多情况下,这就足够了。

If you do not know the name of the upstream remote for a branch, you can look that up first by inspecting the upstream branch name that the current branch was built upon. Use git rev-parse like this:

git rev-parse --symbolic-full-name --abbrev-ref @{upstream}

This shows that upstream branch that was the source for the current branch. This can be parsed to get the remote name like this:

git rev-parse --symbolic-full-name --abbrev-ref @{upstream} | cut -d / -f 1

Now take that and pipe it to git ls-remote and you'll get the URL of the upstream remote that is the source of the current branch:

git ls-remote --get-url \
  $(git rev-parse --symbolic-full-name --abbrev-ref @{upstream} | cut -d / -f 1)

Now it should be noted, that this is not necessarily the same as the source remote repository that was cloned from. In many cases however it will be enough.

二智少女猫性小仙女 2024-10-08 04:01:17
#!/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 [email protected]:your-username/your-repository.git
cd your-repository

git-remote-url origin

输出:

https://github.com/your-username/your-repository
#!/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"
}

Usage:

# 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

Output:

https://github.com/your-username/your-repository
分分钟 2024-10-08 04:01:17

仅获取远程 URL:

git config --get remote.origin.url

为了获取有关特定远程的更多详细信息,请使用

git remote show [remote-name] command

查看远程 URL:

git remote show origin

查看 .git 文件夹放置的位置:

git config --get remote.origin.url

To get only the remote URL:

git config --get remote.origin.url

In order to get more details about a particular remote, use the

git remote show [remote-name] command

To see the remote URL:

git remote show origin

To see where you .git folder placed:

git config --get remote.origin.url
泼猴你往哪里跑 2024-10-08 04:01:17
alias git-repo="git config --get remote.origin.url | sed -e 's/:/\//g'| sed -e 's/ssh\/\/\///g'| sed -e 's/git@/https:\/\//g'"
alias git-pr="git config --get remote.origin.url | sed -e 's/:/\//g'| sed -e 's/ssh\/\/\///g'| sed -e 's/git@/https:\/\//g' | sed 's/....$//' | sed -ne 's/$/\/pulls &/p'"

将此表达式添加到 .zshrc.bashrc 主目录中的文件。

之后,您可以使用类似

git-repo
git-pr
alias git-repo="git config --get remote.origin.url | sed -e 's/:/\//g'| sed -e 's/ssh\/\/\///g'| sed -e 's/git@/https:\/\//g'"
alias git-pr="git config --get remote.origin.url | sed -e 's/:/\//g'| sed -e 's/ssh\/\/\///g'| sed -e 's/git@/https:\/\//g' | sed 's/....$//' | sed -ne 's/$/\/pulls &/p'"

Add this expression to the .zshrc or .bashrc file in the main directory.

After that, you can use like

git-repo
git-pr
小草泠泠 2024-10-08 04:01:17

我最喜欢的是这个(仅适用于公共存储库)。

  1. 检查模式

  2. 检查 Web 请求是否返回有效的 GitHub 存储库。

    导入重新
    导入请求
    
    def is_github_repo(url):
        模式 = re.compile(r'^https://github\.com/[^/]+/[^/]+
    
) 如果不是pattern.match(url): 返回错误 响应 = requests.head(url) 返回response.status_code == 200 和\ response.headers['Content-Type'].startswith('text/html') url = 'https://github.com/username/repo-name' 如果 is_github_repo(url): print(f'{url} 是 GitHub 存储库。') 别的: print(f'{url} 不是 GitHub 存储库。')

My favorite is this (only works for public repositories).

  1. Check the pattern

  2. Check that a web request returns a valid GitHub repository.

    import re
    import requests
    
    def is_github_repo(url):
        pattern = re.compile(r'^https://github\.com/[^/]+/[^/]+
    
) if not pattern.match(url): return False response = requests.head(url) return response.status_code == 200 and \ response.headers['Content-Type'].startswith('text/html') url = 'https://github.com/username/repo-name' if is_github_repo(url): print(f'{url} is a GitHub repository.') else: print(f'{url} is not a GitHub repository.')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文