简体   繁体   中英

Git - How do I log the current branch name AND the last remote URL I pushed to into the console

I want to create a git alias (or bash alias) that will print out (into the console) the currently checked out branch & also the last remote (url not name) that it was pushed to.

Ideally I want it to work like this:

> git log-branch-fork
Branch Name: features/current_feature_name
Form URL: git@github.io:UserName/ForkUrl.git

Alternatively, I could pass in a parameter which contains the name of the remote:

> git log-branch-fork my-fork
Branch Name: features/current_feature_name
Form URL: git@github.io:UserName/my-fork.git

the last remote (url not name) that it was pushed to.

This cannot be determined completely reliably as Git doesn't store information regarding when pushes occur.

But, by looking at the current branch's tracking branch we can at least see the remote that Git uses when git-push is invoked without explicitly using the remote argument.

Here is a shell script which uses the tracking branch to determine the associated remote branch. You can also optionally pass in a remote :

#!/bin/sh

# get full tracking branch name
# (e.g origin/master)
BR=`git rev-parse --abbrev-ref --symbolic-full-name @{u}`

# either get remote from user
# or from parsing the tracking branch's name
if [ $# -eq 1 ]
  then
    REMOTE=$1
else
  REMOTE=`echo $BR | cut -d/ -f 1`
fi

# get url associated with remote
URL=`git config --get remote.$REMOTE.url`

# print out results
echo "Branch Name: $BR"
echo "From URL: $URL"

I would recommend putting this script on your PATH and then when your current working directory is within a Git repo invoke it just by specifying it's name (and optionally passing in the remote ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM