はじめに
長いコマンドを打つのは面倒ですし、なによりタイポが多かったので alias に登録して自分でカスタムしたコマンドを打てるようにしました。
alias 関連の記事はたくさんあるけど、同じようにやってもできなかったり記事によって変更しているファイルが違ったりして結局どうすればいいの?って悩んでいる方。
git コマンドは .gitconfig を編集すれば alias を登録できますが
「git と打つのも省略したい」 という方。
ぜひ試してみてください。
環境
macOS Catalina 10.15.7
Windows10
目次
はじめに
環境
目次
~/.bash_profile
Windows
おわりに
参考文献
~/.bash_profile
早速 alias を登録してきます。
vim や VSCode などで ~/.bash_profile を編集してください。
下記私の ~/.bash_profile です。
記事を執筆時のものになりますので追加や変更していくと思いますが、書き方の参考にしていただけたらと思います。
~/.bash_profile
# git alias
alias g="git"
alias gaa='git add -A'
alias gcm="git commit -m"
alias gpoh='git push origin HEAD'
alias gst='git status'
alias gc='git checkout'
alias gcb='git checkout -b'
alias gb='git branch'
alias pull='git pull'
alias diff='git diff'
alias log='git log --oneline -n'
# docker alias
alias dk='docker'
alias dp='docker ps'
alias dcu='docker-compose up -d'
alias dcd='docker-compose down'
alias dcs='docker-compose stop'
alias dcr='docker-compose restart'
alias dcx='docker-compose exec'
# 【windows の方は以下も追記が必要なようです】
test -r ~/.bashrc && . ~/.bashrc
~/.bash_profile に記述・保存後に今回の変更を反映させるために以下のコマンドを打ってください。
ターミナル
source ~/.bash_profile
Macの方は設定は終わりです。
Windows
Windows環境では ~/.bash_profile に加え ~/.bashrc にも同様の記述が必要でした。source コマンドで反映しても再起動すると消えてしまいます。
この問題に対してこちらの記事がわかりやすく説明をしてくださっているので合わせてご覧ください。
そのため、上記の ~/.bash_profile に書いた内容を ~/.bashrc でも行い、
~/.bash_profile の最後にこちらを追記してください。
~/.bash_profile
test -r ~/.bashrc && . ~/.bashrc
最後に以下のコマンドを打ったら登録した alias を使用することができるようになります。
ターミナル
source ~/.bashrc
おわりに
タイポも減りますしコマンドを打つ時間が短くなるので alias 登録はとても便利です。
自分の覚えやすい形で登録してみてはいかがでしょうか。
参考文献
【mac】Gitのエイリアス(alias)設定
【 source 】コマンド/【 . 】コマンド――シェルの設定を即座に反映させる
↧