我在GitHub上拖了一個有幾個福克的項目,但忘記了它是哪個福克。


当前回答

上流的远程可能不会被称为“起源”,所以这里有一个变量:

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

其他回答

我认为你可以找到它在.git/config 和远程[“起源”] 如果你没有操纵它。

这只提供URL,这对脚本目的有用:

git config --get remote.origin.url

如果你不知道一个分支的上流远程的名称,你可以首先通过检查当前分支的上流分支名称来查看它。

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)

现在应该注意到,这并不一定与源的远程存储库相同,但在许多情况下,这将是足够的。

#!/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 git@github.com:your-username/your-repository.git
cd your-repository

git-remote-url origin

出口:

https://github.com/your-username/your-repository

使用 git 远程显示起源,您必须在项目目录中,但如果您想从其他地方确定 URL 您可以使用:

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

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

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

因此,您只需要在 Git 根文件夹中呼叫 giturl 以便简单地获得其 URL。


如果你延伸这个 alias 如此

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

你只得到平坦的URL没有先前的

“URL”

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

您在使用中获得更多机会:

例子

在 Mac 上,您可以打开 $(giturl) 以在默认浏览器中打开 URL。

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