0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Gitとbashに設定しているaliasをメモする.

Last updated at Posted at 2024-07-06

作業環境

項目 バージョン / 詳細
OS Windows 11 (WSL2)
エディタ VSCode
拡張機能 Git Graph
Git 2.39.3

誰に向けた記事か

  • Docker のコマンドを打つのが面倒 / 打ち間違えがちな人
  • Git の基本はわかるけど、もっと便利に使いたい人
  • (サブ)Ruby on Rails を使って開発している人

やらないこと

  • alias の設定方法の解説
  • 基本的なコマンドの解説

設定内容

bashのalias

alias d='docker'
alias dc='docker compose'
alias dcud='docker compose up -d'
alias dceab='docker compose exec app bash'

alias gst='git stash'
alias gstp='git stash pop'
alias gcan='git commit --amend --no-edit'
alias gp-rdev='git pull --rebase -autostash origin develop'
alias gf='git fetch'

alias create-todo='rubocop --auto-gen-config --exclude-limit 999999'

alias ..='cd ..'

Gitのalias

[alias]
	sw = switch
	edit-comment = commit --amend
	current-branch = ! git branch | grep -e '^* ' | sed -e 's/^* //g'
 	where = branch --contains=HEAD
	fpull = !git fetch && git reset --hard origin/$(git current-branch)
	fpush = push --force-with-lease
	fixup = commit --fixup
	autosquash = rebase -i --autostash --autosquash
	commits = log --pretty=oneline --grep=\"^fixup\\!\" --invert-grep
	head-hash = log -1 --pretty=format:\"%H\" --grep=\"^fixup\\!\" --invert-grep
	wip = !git commit --fixup $(git head-hash)
	upstream = !git branch --format='%(if)%(HEAD)%(then)* %(else)  %(end)%(refname:short) : %(upstream:short)' | grep $(git current-branch)
 	alias = !"if test -z $1; then loop=; for k in `git config --get-regexp ^alias\\. | sort | sed -e 's/^alias\\.\\(\\S*\\).*/\\1/g'`; do echo -n $k; loop="${loop}1"; if test ${#loop} -lt 5; then echo -n '\t'; else echo; loop=; fi; done; exit; fi; git config --list | grep ^alias\\.$1= | sed -e s/^alias\\.//; exit;"

主要なものについて設定してみての所感

bash のaliasについて

コマンド 所感
dcdcud 高頻度で利用している。タイポが激減してとても嬉しい
gcangf git操作でよく利用する。雑に打つことが多いため重宝している。
gstgstp git の--autostashオプションを知ってからは利用頻度が少ない。稀にコンフリクト等で事故が発生するため注意が必要。
gp-rdev rebase派のあなたはぜひ設定を。

Git の alias

コマンド 所感
edit-comment 直前のコミットメッセージを編集する。alias を使うか直接打つかは気分次第。
current-branch 現在のブランチを表示する。後述の alias でも使用。もっと簡単なコマンドがありそう。
where 現在のブランチを表示する alias その2。コンソールで打ちがちなのはこちら。
fpull force push はあるのに force pull がないので作成。コンフリクトや rebase に失敗したときに使う。
fpush force push を気軽に使うために作成。--force-with-lease を付けているため、自分以外の変更がある場合は push できないので比較的安全。
fixupautosquash 2つセットで利用。コミット履歴を整理する際に便利。fixup は多用しすぎるとコンフリクトするため、定期的に autosquash を実行するのがおすすめ。
commits fixup で作成したコミット以外のハッシュとメッセージを表示する。git commits -5 のように打つと5個分のコミットが表示可能。
wip fixup タグのない最新のコミットへ向けたfixupコミットを作成。git commit --amend --no-edit とは違い、履歴が残るため、キリが悪い時の保存に便利。
upstream push 前にリモートブランチを確認するための alias。誤ったブランチへの push を防げる(n敗)。
alias 設定した alias の一覧や内容を表示する。たまに alias を忘れるので作成。alias {command} でaliasの内容を表示。

終わりに

備忘録的な形で記事を作成しました。

より良い alias や便利なコマンド、記事内の誤りなどありましたらコメントでご指摘いただけると嬉しいです。

ご拝読ありがとうございました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?