LoginSignup
8
2

More than 5 years have passed since last update.

ターミナルからカレントディレクトリのGitHubページを開く

Last updated at Posted at 2018-08-12

Mac — コンソールからGithubレポジトリをWebブラウザ開くコマンド例を参考に.
自分の環境だとsshでクローンしていたため動かなかったので改変させて頂きました.

前提

$ git remote -v

> origin git@github.com:hoge/fuga.git (fetch)
> origin git@github.com:hoge/fuga.git (push)

このようにGitHubが登録されている必要があります

コマンド

$ open $(git remote -v | head -n 1 | awk '{ print $2 }'| awk -F'[:]' '{ print $2 }'| awk -F'.git' '{ print "https://github.com/" $1 }')

解説

git remote -v

.git/に保存されているリモートリポジトリを表示します
結果

origin git@github.com:hoge/fuga.git (fetch)
origin git@github.com:hoge/fuga.git (fetch)

head -n 1

haedコマンドでremote -vコマンドの結果の1行目を取得する

結果

origin git@github.com:hoge/fuga.git (fetch)

awk '{ print $2 }'

awkコマンドでスペースで区切られている2つ目の部分を取得する
結果

git@github.com:hoge/fuga.git

awk -F'[:]' '{ print $2 }'

awkコマンドで:を区切り文字にし、2つ目の部分を取得する
結果

hoge/fuga.git

awk -F'.git' '{ print "https://github.com/" $1 }'

awkコマンドで.gitを区切り文字にし、先頭を取得
その先頭にhttps://github.com/をつける

結果

https://github.com/hoge/fuga

open $()

最後にそれらをopenコマンドでひらく

これらをまとめて

$ open $(git remote -v | head -n 1 | awk '{ print $2 }'| awk -F'[:]' '{ print $2 }'| awk -F'.git' '{ print "https://github.com/" $1 }')

最後に

自分はこのようにaliasを作成しました.

alias gopen="open $(git remote -v | head -n 1 | awk '{ print $2 }'| awk -F'[:]' '{ print $2 }'| awk -F'.git' '{ print "https://github.com/" $1 }')"
8
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
2