LoginSignup
2
1

More than 1 year has passed since last update.

git command ちゃんと使ってる?

Last updated at Posted at 2021-12-07

はじめに

僕がよく使っているコマンドを、
help コマンドのドキュメントや、公式のドキュメントを見ながら解説していきます

git add -p

変更のあるファイルを staging に移動する際のコマンドです。

git help add
 -p, --patch
           Interactively choose hunks of patch between the index and the work tree and add them to the index. This gives the user a chance to review the difference before adding modified contents to the index.

           This effectively runs add --interactive, but bypasses the initial command menu and directly jumps to the patch subcommand. See “Interactive mode” for details.

時々 git add . をしている人がいますが、結構怖いことしてるなーって思ってます。

自分が変更した内容を信じすぎじゃないですか?

このコマンドは
staging に入っていない差分を、インタラクティブに staging に移動するかどうかを決めることができるオプションです。

つまり、差分を一個一個見て、余分な差分を安易に commit しないためのオプションです。

git push -u origin head

ローカルブランチが履歴を追うリモートブランチを設定する際のオプション --set-upstream の省略 -u を使ったコマンドです。

-u, --set-upstream
           For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands. For more information, see branch.<name>.merge in git-config(1).

公式を見ると

git push -u origin HEAD

となっていて、head が大文字です。
実はこれ知られていないのですが、 小文字でも大丈夫です。

公式だったり教科書で、よく大文字で定義されているので、わざわざ shift + [H E A D] と指に負担をかけながらタイプしてると思いますが、
そんなことしなくて大丈夫です。

git log #{hash} --stat

差分のあるファイルの名前と行数を表示してくれるコマンド

 --stat[=<width>[,<name-width>[,<count>]]]
           Generate a diffstat. By default, as much space as necessary will be used for the filename part, and the rest for the graph part. Maximum width defaults to terminal width, or 80 columns if not connected to a terminal, and can be
           overridden by <width>. The width of the filename part can be limited by giving another width <name-width> after a comma. The width of the graph part can be limited by using --stat-graph-width=<width> (affects all commands generating
           a stat graph) or by setting diff.statGraphWidth=<width> (does not affect git format-patch). By giving a third parameter <count>, you can limit the output to the first <count> lines, followed by ... if there are more.

           These parameters can also be set individually with --stat-width=<width>, --stat-name-width=<name-width> and --stat-count=<count>.

git diff にも同じオプションがあります。

git diff や、 git log #{hash} をしたときに、
差分の内容が表示されて、うわ、、ってなったことありませんか?

このオプションを使えば、ファイル名だけを表示できるので、
「何が変わったのか」だけを知りたい時に便利です。

終わり

少し物足りない感じはしましたが、
これ以上書いても「知ってるわ」ってのが多いなって判断したので、ここまでにします。

ちなみにここで書いた内容は以前にも記事にしたことがあります。

書いてから思い出しました...。

一応リンク貼っときます。
https://qiita.com/yasudanaoya/items/9afc490e9fcf0fc92e73

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