11
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

個人的Gitの小技集

Last updated at Posted at 2018-06-27

はじめに

どうも、tkszkです:relaxed:
新卒3年目で、今後の人生に危機感を感じたので、Qiita始めました。
これまでROM専でしたがついに投稿してみます。

目次

はい、本題です。Gitの小技とは?
Gitのチートシートは色々なところに載っているんですが、そうはいっても自分はこうやってるぞというのが何点かあったりするので、それを今回紹介したいと思います。

  • squashは git reset で爆速コミット
  • git add -N . からの git diff で一括チェック
  • git reset 万能説
  • git add -p で個別コミット

おまけ

  • force push を恐れるな(※自分専用のブランチだけね)
  • tigを使おう

squashはgit resetで爆速コミット

※squashとはコミットを1つのコミットにまとめることです
rebaseをするときに、-i をつけるとコミットをどのように処理するか選べる画面に遷移します。

git rebase -i
                 
pick 6ebb91a add b
pick 43c3b80 add c

# Rebase bb94995..43c3b80 onto bb94995 (2 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

上記画面で、"43c3b80"のpickをsquashにすると1つ前のコミット("6ebb91a")に統合されます。
コミット数が2,3個であれば問題ないのですが、squashするときは大抵コミットの数が結構多くなってます。
しかも、だいたい全部squashにして1つのコミットにまとめることが多いんですよね…

その時は

git reset 6ebb91a
からの
git add .
git commit -m "コミットメッセージ"

これで問題なし。squashよりもずっと早くsquashコミットができます。

git add -N . からの git diff で一括チェック

git diff しても差分が出ないのなんでなん?!???
と焦る前に

git add -N .

これで新規ファイルがgitに認知されます。
それ以降 git diff のみで差分が出るので楽。

git reset 万能説

# 指定したハッシュ時点の状態に戻す
git reset --hard "コミットハッシュ"

# rebaseミスってもreflogに残っているので大丈夫
# git reflog で戻りたいコミットハッシュを探す
git reset --hard "コミットハッシュ"

# 今のソースコードの状態を保持して、指定したハッシュの状態になる
git reset "コミットハッシュ"

git add -p でファイル内を細分化してコミット

コードレビューを依頼する前によくやります

git add -p "ファイル名"

これは説明が長くなるので、適当にググってください…
https://qiita.com/crifff/items/1abf08bca4ce51db4775

force push を恐れるな(※ただし自分専用のブランチだけね)

チームによってはforce pushは禁止になっている可能性があります。
そのためforce pushは害悪みたいな印象があるとかないとか…
が、個人の開発ブランチは例外だと思っています。
リモートにあるブランチを整形したブランチに書き換えるので色々恩恵が大きいです。(無駄なブランチの増殖の予防とか恥ずかしいコミットの削除とか…)
チームで使う場合は、git push --force-with-leaseを使いましょう。誰かがコミットしていると弾かれるので万が一の予防になります(てかpushする前にpullしr)

tigを使おう

これは色々な派閥があるので好みですが、tigは良いです。
もう文章を書く体力が尽きたので気になる方は調べてください…(すみません)

終わりに

はじめての投稿ですが、どうでしょうか?
アウトプットする経験がほぼないので駄文で失礼しました。。
間違いの指摘やまた別の俺流があればどんどん教えてほしいです。
これから引き続き投稿していけるようがんばりますー

11
8
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
11
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?