![[小ネタ] Gitのエイリアス設定を晒してみる(19個)](https://devio2023-media.developers.io/wp-content/uploads/2019/08/eyecatch_git.png)
[小ネタ] Gitのエイリアス設定を晒してみる(19個)
この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
Gitのコマンドは様々なことができますが、たまに長ったらしく感じます。
そんなときに便利な機能がエイリアスです。
というわけで、私が使っているエイリアスを晒してみます。気になるものがあれば、ぜひお使いください。
目次
- 環境
 - Gitエイリアスの一覧
 - git ss: status表示
 - git br: ブランチ関連
 - git brm: ブランチの名前変更
 - git brd: ブランチの削除
 - git co: ブランチ移動
 - git cob: ブランチを新規作成して移動
 - git adu: 変更ありファイルのみ追加
 - git adup: 変更ありファイルの差分を一部だけ追加
 - git com: コミットする
 - git mg: マージする(No Fast-forward)
 - git mgff: マージする(Fast-forward)
 - git cp: 特定コミットを採用する
 - git log1: 直前1件のログを見る
 - git logo: コミットログを1行ずつ表示する
 - git logn: コミットログにファイル名を表示する
 - git firstcom: 空コミットをする
 - さいごに
 - 参考
 
環境
| 項目 | バージョン | 
|---|---|
| macOS | Mojave 10.14.6 | 
| Git | 2.23.0 | 
Gitエイリアスの一覧
.gitconfigのalias部分です。
設定用コマンド
git config --global alias.ss status git config --global alias.br branch git config --global alias.brm "branch -m" git config --global alias.brd "branch -d" git config --global alias.brdd "branch -D" git config --global alias.co checkout git config --global alias.cob "checkout -b" git config --global alias.adu "add -u" git config --global alias.adup "add -u -p" git config --global alias.com commit git config --global alias.mg "merge --no-ff" git config --global alias.mgff "merge --ff" git config --global alias.cp cherry-pick git config --global alias.log1 "log -1" git config --global alias.log2 "log -2" git config --global alias.log3 "log -3" git config --global alias.logo "log --oneline" git config --global alias.logn "log --name-status --oneline" git config --global alias.firstcom "commit --allow-empty -m \"Initial commit\""
補足
個人的に下記の方針です。
- コマンド操作する(GUIはコミットログを眺めるときぐらい)
 git push、git pull、git fetchなど、リモートリポジトリが絡むコマンドは、安全重視でエイリアスは使わない(眼チェックも兼ねて)- なるべく元のコマンドの原型を保ちたい
 
git ss: status表示
git statusです。入力しやすいです。
$ git ss On branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) nothing to commit, working tree clean
git br: ブランチ関連
git branchです。入力しやすいです。
ブランチの作成
$ git br test-branch
ブランチの一覧表示
$ git br * master test-branch
git brm: ブランチの名前変更
git branch -mです。
現在ブランチの名前を変更
$ git brm new-branch-name
任意ブランチの名前を変更
$ git brm old-branch-name new-branch-name
git brd: ブランチの削除
git branch -dです。
$ git brd hoge-branch Deleted branch hoge-branch (was 1e76558).
未マージブランチ(宙ぶらりんのブランチ)は、-Dオプションが必要なので、下記を用います。
$ git brdd hoge-branch Deleted branch hoge-branch (was 1e76558).
git co: ブランチ移動
git checkoutです。ブランチ移動もよく使うコマンドなので、短いと楽です。
$ git co any-branch
また、ファイル名を指定すると、「指定したファイルに対する編集をリセット(現在のブランチの最新内容にする)」できます。
$ git ss On branch master Your branch is ahead of 'origin/master' by 2 commits. (use "git push" to publish your local commits) Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: memo.txt no changes added to commit (use "git add" and/or "git commit -a") $ git co memo.txt $ git ss On branch master Your branch is ahead of 'origin/master' by 2 commits. (use "git push" to publish your local commits) nothing to commit, working tree clean
git cob: ブランチを新規作成して移動
git checkout -bです。これもよく使うコマンドですね。
現在のコミットに対して、ブランチを作成して移動
$ git cob any-branch
任意のコミットに対して、ブランチを作成して移動
$ git cob any-branch origin/any-branch
git adu: 変更ありファイルのみ追加
git add -uです。新規作成したファイルを除いてaddします。(Untracked filesは追加しません。)
コミットしたくないファイル(ゴミファイルや一時ファイルなど)がある場合に使います。
$ git adu .
git adup: 変更ありファイルの差分を一部だけ追加
git add -u -pです。-uは上記のヤツですが、-pは変更を「差分毎」に追加できます。(追加しないもできます)
たとえば、ファイルAに「動作確認用に変更した部分」と「修正した部分」が混在しているとき、-pを使えば、「修正した部分」のみをaddできます。
$ git adup . diff --git a/memo.txt b/memo.txt index 43f2d46..53c4700 100644 --- a/memo.txt +++ b/memo.txt @@ -1,7 +1,7 @@ aaaaa bbbbb - +hoge ccccc ddddd Stage this hunk [y,n,q,a,d,e,?]? y
git com: コミットする
git commitです。よく使うコマンドなので、短いと楽です。
$ git com -m "any message"
git mg: マージする(No Fast-forward)
git merge --no-ffです。マージコミットを作成するマージです。
別途、マージ時のデフォルト動作を設定できますが、git pullにも影響するので、手動マージするとき用に意図的にエイリアスを使っています。
$ git mg any-branch
git mgff: マージする(Fast-forward)
git merge --ffです。Fast-Forward(早送り)マージします。
個人的にgit pullよりもgit fetchを使う派なので、重宝しています。
$ git mgff origin/master
マージの種類については、下記のスライドが分かりやすいです。
git cp: 特定コミットを採用する
git cherry-pickです。たまに使うとき楽です(長いので……)。
$ git cherry-pick 12345abcdef...
git log1: 直前1件のログを見る
git log -1です。「タグ付けたっけ?」など、直近n件のコミットログを見れます。
$ git log1
commit ee62d3a3d47fa81890beb8c53afe313ccfc24e1d (HEAD -> master)
Author: cm-fujii-genki <xxx.yyy@zzz.jp>
Date:   Wed Aug 21 14:14:24 2019 +0900
    add memo
下記も同様です。
- git log2
 - git log3
 
git logo: コミットログを1行ずつ表示する
git log --onelineです。コミットログを眺めるときに使います。
$ git logo ee62d3a (HEAD -> master) add memo 1e76558 (tag: v1.0.0, new-branch-name) update config.yml c75c027 (origin/master, hoge) add config.yml 7d3da6d add set_aws.sh 31c56e8 pipenv install awscli cd91fe1 update awscdk-circle_ci_deploy_sample.ts bc55342 add circleci-user-stack.ts
git logn: コミットログにファイル名を表示する
git log --name-status --onelineです。過去のコミットで変更されたファイル名が分かります。
$ git logn ee62d3a (HEAD -> master) add memo A memo.txt 1e76558 (tag: v1.0.0, new-branch-name) update config.yml M .circleci/config.yml c75c027 (origin/master, hoge) add config.yml A .circleci/config.yml 7d3da6d add set_aws.sh A scripts/set_aws.sh 31c56e8 pipenv install awscli A Pipfile A Pipfile.lock cd91fe1 update awscdk-circle_ci_deploy_sample.ts M bin/awscdk-circle_ci_deploy_sample.ts
git firstcom: 空コミットをする
git commit --allow-empty -m \"Initial commit\"です。
git init直後によく使います。初回の空コミットを作成できます。
$ git firstcom
さいごに
エイリアスの名前や内容は、個性が表れて面白いと思います。
どんどんカスタマイズして便利にしていきましょう!







