几天前我从GitHub上撤了一个项目。我已经发现GitHub上有几个叉子,我忽略了我最初采用的那个。如何确定我拉出的哪个叉子?
如果您只想要远程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:
如果要使用脚本中的值,可以使用此答案中列出的第一个命令。
Git URL将在Git配置文件中。该值对应于密钥url
。
对于Mac和Linux,请使用以下命令:
cd project_dir
cat .git/config | grep url | awk '{print $3}'
对于Windows,在任何文本编辑器中打开以下文件,并找到键url
的值。
project_dir/.git/config
注意:即使您处于脱机状态或远程git服务器已关闭,这也将起作用。
我永远不会记住Git命令的所有参数,所以我只是在~/.gitconfig
文件中添加了一个对我来说更有意义的别名,所以我记得它,它导致更少的输入:
[alias]
url = ls-remote --get-url
重新加载终端后,您只需键入:
> git url
以下是我经常使用的一些内容:
[alias]
cd = checkout
ls = branch
lsr = branch --remote
lst = describe --tags
我主要使用:
git remote get-url origin
它适用于Windows中的Git Bash命令控制台或CMD命令控制台。也就是说,它适用于Git版本2.x.
上游的遥控器可能不被称为“原点”,所以这里有一个变化:
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
origin
对于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 ":"
为了补充其他答案:如果遥控器由于某种原因被更改,因此不能反映原始原点,则reflog中的第一个条目(即命令git reflog
显示的最后一个条目)应指示repo最初的位置克隆自。
EG
$ git reflog | tail -n 1
f34be46 HEAD@{0}: clone: from https://github.com/git/git
$
(请记住,可能会清除reflog,因此无法保证其正常工作。)
使用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 =”
在
你有更多的使用可能性:
例
在Mac上,您可以调用open $(giturl)
在标准浏览器中打开URL。
或者chrome $(giturl)
在Linux上使用Chrome浏览器打开它。
一个简单的方法是打开.git/config
文件:
cat .git/config
编辑:
vim .git/config
或
nano .git/config
打印任意命名的远程提取URL:
git remote -v | grep fetch | awk '{print $2}'
如果您不知道分支的上游远程名称,可以先查看当前分支所依据的上游分支名称。像这样使用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)
现在应该注意,这不一定与从中克隆的源远程存储库相同。但在许多情况下,这就足够了。
如果您希望将此用于脚本编写,则只能获取URL
git config --get remote.origin.url
#!/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
git config --list
此命令将提供与存储库相关的所有信息。
你可以试试:
git remote -v
它将打印所有遥控器的提取/推送URL。
得到答案:
git ls-remote --get-url [REMOTE]
这比阅读配置更好;参考man page for git-ls-remote
:
--get-URL
扩展给定远程存储库的URL,同时考虑任何
"url.<base>.insteadOf"
配置设置(请参阅git-config(1)
)并退出而不与远程通信。
使用Git 2.7(2015年1月5日发布),您可以使用git remote
获得更加一致的解决方案:
git remote get-url origin
(git remote set-url origin <newurl>
好的吊坠)
请参阅commit 96f78d3撰写的Ben Boeckel (mathstuf
)(2015年9月16日)。
(由Junio C Hamano -- gitster
--在commit e437cbd合并,2015年10月5日):
remote: add get-url subcommand
扩大
insteadOf
是ls-remote --url
的一部分,也没有办法扩大pushInsteadOf
。 添加get-url
子命令以便能够查询这两个子命令以及获取所有已配置URL的方法。
get-url:
检索远程的URL。
insteadOf
和pushInsteadOf
的配置在这里扩展。 默认情况下,仅列出第一个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]
总而言之,至少有四种方式:
(以下是针对官方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)
简短回答:
$ 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
它不会获取远程头(AKA分支)。你不需要那种类型的信息,对吗?
http://www.kernel.org/pub//software/scm/git/docs/git-remote.html
您可以将| grep -i fetch
应用于所有三个版本,以仅显示提取URL。
如果您需要纯粹的速度,那么使用:
$ git config --get remote.origin.url
感谢@Jefromi指出这一点。
如果你没有操纵它,我想你可以在.git/config
和remote["origin"]
下找到它。
对我来说,这是更简单的方法(减少打字):
$ git remote -v
origin https://github.com/torvalds/linux.git (fetch)
origin https://github.com/torvalds/linux.git (push)
实际上,我把它变成了一个名为alias
的s
:
git remote -v
git status
您可以添加到您的个人资料:alias s='git remote -v && git status'