LoginSignup
1
1

More than 5 years have passed since last update.

カレントブランチを調べることなくgit push origin hogeを一発で行えるaliasを作成した

Last updated at Posted at 2016-12-20

フィードバックをいただくまで知らなかったのですが、以下で説明する設定などはgit push origin HEADコマンドで済む内容となっております。(参考: まだ git push origin するときに current branch 名を入力して消耗しているの?)

gitを使っていると、git push origin hogeというコマンドをよく使うと思います。
このhogeに当たる部分は現在のブランチ名である場合がほとんどだと思いますが、git branchで確認したり、ブランチ名をコピペしたり若干面倒です。
なので、現在のブランチ名を調べることなくカレントブランチをpushするエイリアスを作成しました。
以下が設定ファイルの抜粋です。(git push originの略でgpoでエイリアスを作成してあります)

.zshrc
function _git-push-current-branch() {
  current_branch=`git rev-parse --abbrev-ref HEAD 2> /dev/null`
  git push origin ${current_branch}
}
alias gpo="_git-push-current-branch"

git rev-parse --abbrev-ref HEAD 2> /dev/nullの部分は現在のブランチ名を取得するコマンドです。

初めは単純に以下のようにエイリアスを作成していました。

alias gpo="git push origin `git rev-parse --abbrev-ref HEAD 2> /dev/null`"

ただ上記の場合、カレントブランチが動的に取得できないため、ブランチをチェックアウトした場合にうまく動作しませんでした。ですので、冒頭で説明したようなファンクションを呼び出すという形にしました。

これで、今までよく実行したであろう

$ git branch 
* hoge
master
$ git push origin hoge

というコマンドが

$ gpo

一発で済むようになりました。

参考
gitでカレントブランチをpushする
alias で 引数を動的に指定するためのやり方

1
1
6

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
1
1