LoginSignup
3
2

More than 5 years have passed since last update.

Git add, commit, pushをコマンド一つにまとめるaliasの設定

Last updated at Posted at 2018-07-16

Mac編

準備

Macの場合はターミナルから.bash_profile.bashrcを作成します。

$ cd ~
$ echo > .bash_profile
$ echo > .bashrc

作成した.bashr_profileを編集します。

$ vim .bash_profile
.bash_profile
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

次に.bashrcにコマンドを記述します。

$ vim .bashrc
.bashrc
gitpush() {
  git add .
  git commit -m "$*"
  git push origin HEAD
}

alias gp=gitpush

※ファイルの編集だけではまだ反映されていません。一度ターミナルを閉じ、もう一度立ち上げると反映されます。

使い方

今回はgpというコマンドを登録しました。

$ gp YOUR_COMMIT_MESSAGE

上記コマンドを叩くと
git add .git commit -m "YOUR_COMMIT_MESSAGE"git push origin YOUR_BRANCH
のように処理されます。

Windows編

準備

Windowsの場合はGit Bashから.bash_profileを作成します。

$ cd ~
$ echo > .bash_profile

次に.bash_profileにコマンドを記述します。

$ vim .bash_profile
.bash_profile
gitpush() {
  git add .
  git commit -m "$*"
  git push origin HEAD
}

alias gp=gitpush

※ファイルの編集だけではまだ反映されていません。一度Git Bashを閉じ、もう一度立ち上げると反映されます。

使い方

今回はgpというコマンドを登録しました。

$ gp YOUR_COMMIT_MESSAGE

上記コマンドを叩くと
git add .git commit -m "YOUR_COMMIT_MESSAGE"git push origin YOUR_BRANCH
のように処理されます。

補足

現在のブランチにpush

git push origin HEADとすることで現在のブランチにpushすることができます。
masterブランチで作業している場合は
git push origin HEADgit push origin masterとなります。

pushせずにadd, commitまでにしたい場合

$ git commit -am "YOUR_COMMIT_MESSAGE"

上記コマンドで実行できます。
しかし、せっかくなのでailiasに登録してしまいましょう。

Macの場合は.bashrcWindowsの場合は.bash_profileを編集します。

gitcommit() {
  git add .
  git commit -m "$*"
}

alias gc=gitcommit

使い方はpushと同様に

gc YOUR_COMMIT_MESSAGE

上記コマンドで実行できます。

その他

他にもよく使うコマンドはaliasに登録すると良いでしょう。

alias gitb="git branch"
alias gitch="git checkout"
alias gits="git status"

参考

https://qiita.com/peccul/items/90dd469e2f72babbc106
https://qiita.com/ma_me/items/f76295f3da9579043bbc
https://qiita.com/yutat93/items/b5bb9c0366f21bcbea62
https://stackoverflow.com/questions/19595067/git-add-commit-and-push-commands-in-one

3
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
3
2