如何确定本地 Git 存储库最初克隆的 URL
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(26)
仅获取远程 URL:
如果您需要完整输出,并且您所在的网络可以访问源所在的远程存储库:
使用 git clone 时(来自 GitHub 或任何源存储库)没关系)克隆源的默认名称是“origin”。使用
git remote show
将显示有关此远程名称的信息。前几行应显示:如果您想在脚本中使用该值,您将使用此答案中列出的第一个命令。
To obtain only the remote URL:
If you require full output, and you are on a network that can reach the remote repo where the origin resides:
When using
git clone
(from GitHub, or any source repository for that matter) the default name for the source of the clone is "origin". Usinggit remote show
will display the information about this remote name. The first few lines should show:If you want to use the value in a script, you would use the first command listed in this answer.
这仅给出 URL,这对于脚本编写很有用:
This gives only the URL, which is useful for scripting purposes:
这将打印所有遥控器的获取/推送 URL:
This will print all your remotes' fetch/push URLs:
得到答案:
这比读配置要好;请参阅
git-ls-remote
的手册页< /a>:正如 @Jefromi 所指出的,此选项是在 v1.7.5 中添加的,并且直到v1.7.12.2 (2012-09)。
To get the answer:
This is better than reading the configuration; refer to the man page for
git-ls-remote
:As pointed out by @Jefromi, this option was added in v1.7.5 and not documented until v1.7.12.2 (2012-09).
使用 Git 2.7(2015 年 1 月 5 日发布),您可以使用
git remote
获得更连贯的解决方案:(git remote set-url origin
的漂亮挂件)请参阅 提交 96f78d3(2015 年 9 月 16 日),作者:Ben Boeckel (
mathstuf
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 e437cbd,2015 年 10 月 5 日):在 git 2.7 之前,您有:
With Git 2.7 (release January 5th, 2015), you have a more coherent solution using
git remote
:(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):Before git 2.7, you had:
总而言之,至少有四种方法:
使用官方 Linux 存储库进行尝试:
最少信息:
和
更多信息:
甚至更多信息:
To summarize, there are at least four ways:
Trying it out using the official Linux repository:
Least information:
and
More information:
Even more information:
对我来说,这是更简单的方法(更少的打字):
输出:
实际上,我将其放入名为
s
的alias
中,它的作用是:您可以使用以下命令添加到您的个人资料中 : :
alias s='git remote -v && git 状态'
For me, this is the easier way (less typing):
Output:
Actually, I've that into an
alias
calleds
that does:You can add to your profile with:
alias s='git remote -v && git status'
我认为如果您没有操作它,您可以在
.git/config
和remote["origin"]
下找到它。I think you can find it under
.git/config
andremote["origin"]
if you didn't manipulate that.简短的回答:
或者,纯快速脚本的替代方案:
一些信息:
$ 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
对所有三个版本仅显示获取 URL。如果您需要纯粹的速度,请使用:
感谢 @Jefromi 指出这一点。
Short answer:
or, an alternative for pure quick scripts:
Some info:
$ git remote -v
will print all remotes (not what you want). You want origin right?$ git remote show origin
much better, shows onlyorigin
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:
Thanks to @Jefromi for pointing that out.
Git URL 将位于 Git 配置文件内。该值对应于键
url
。对于 Mac 和 Linux,请使用以下命令:
对于 Windows,在任何文本编辑器中打开以下文件并找到键
url
的值。注意:即使您处于离线状态或远程 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:
For Windows, open the below file in any text editor and find the value for key
url
.Note: This will work even if you are offline or the remote Git server has been taken down.
我永远记不住 Git 命令的所有参数,所以我只是在
~/.gitconfig
文件中添加一个对我来说更有意义的别名,这样我就可以记住它,并且可以减少输入次数:重新加载终端后,您只需输入:
以下是我常用的一些:
我也强烈推荐 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:After reloading the terminal, you can then just type:
Here are a few more of my frequently used ones:
I also highly recommend git-extras which has a
git info
command which provides much more detailed information on the remote and local branches.我更喜欢这个,因为它更容易记住:
它将列出所有有用的信息,例如:
I prefer this one as it is easier to remember:
It will list all useful information such as:
我基本上使用:
它适用于 Windows 中的 Git Bash 命令控制台或 CMD 命令控制台。也就是说,它适用于 Git 2.x 版本。
I basically use:
It works for Git Bash command console or CMD command console in Windows. That said, it works with version 2.x of Git.
上游的远程可能不被称为“origin”,所以这里有一个变体:
或者:
对于更有用的变量,有:
The upstream's remote may not be called "origin", so here's a variation:
Or:
For more useful variables there's:
此命令将提供与您的存储库相关的所有信息。
This command will give all information related to your repository.
您使用 SSH 克隆克隆了存储库。
但您想要获取一个 HTTP URL 以在浏览器中打开它或共享它:
GitHub 或 GitLab 并不重要。
You cloned your repository with SSH clone.
But you want to get an HTTP URL to open it in the browser or share it:
GitHub or GitLab doesn’t matter.
一个简单的方法是打开
.git/config
文件:编辑:
vim .git/config
或nano .git/config
A simple way is to open the
.git/config
file:To edit:
vim .git/config
ornano .git/config
打印任意命名的远程获取 URL:
Print arbitrarily named remote fetch URLs:
获取
origin
的 IP 地址/主机名对于
ssh://
存储库:对于
git://
存储库:To get the IP address/hostname of
origin
For
ssh://
repositories:For
git://
repositories:补充其他答案:如果遥控器由于某种原因被更改,因此不反映原始来源,则引用日志中的第一个条目(即命令
显示的最后一个条目>git reflog
) 应指示存储库最初克隆的位置。例如
(请记住,引用日志可能会被清除,所以这不能保证有效。)
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.
(Bear in mind that the reflog may be purged, so this isn't guaranteed to work.)
使用 git remote show origin 时,您必须位于项目目录中。但如果你想从其他地方确定 URL
你可以使用:
如果你经常需要这个命令,你可以在 MacOS 的
.bashrc
或.bash_profile
中定义一个别名。所以你只需要在Git根文件夹中调用
giturl
就可以简单地获取它的URL。如果你像这样扩展这个别名,
你只会得到没有前面的纯 URL
中
您可以在其使用中获得更多可能性:
<示例
在 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 elseyou could use:
If you'll need this command often, you could define an alias in your
.bashrc
or.bash_profile
with MacOS.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
you get only the plain URL without the preceding
in
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.如果您不知道分支的上游远程名称,可以首先通过检查构建当前分支的上游分支名称来查找。像这样使用 git rev-parse :
这表明上游分支是当前分支的源。可以解析它以获取如下所示的远程名称:
现在将其通过管道传输到 git ls-remote ,您将获得作为当前分支源的上游远程的 URL:
现在应该注意的是,这不一定与从中克隆的源远程存储库相同。然而,在许多情况下,这就足够了。
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:This shows that upstream branch that was the source for the current branch. This can be parsed to get the remote name like this:
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: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.
用途:
输出:
Usage:
Output:
仅获取远程 URL:
为了获取有关特定远程的更多详细信息,请使用
查看远程 URL:
查看 .git 文件夹放置的位置:
To get only the remote URL:
In order to get more details about a particular remote, use the
To see the remote URL:
To see where you .git folder placed:
将此表达式添加到 .zshrc 或 .bashrc 主目录中的文件。
之后,您可以使用类似
Add this expression to the .zshrc or .bashrc file in the main directory.
After that, you can use like
我最喜欢的是这个(仅适用于公共存储库)。
检查模式
检查 Web 请求是否返回有效的 GitHub 存储库。
) 如果不是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).
Check the pattern
Check that a web request returns a valid GitHub repository.
) 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.')