LoginSignup
3
1

More than 1 year has passed since last update.

git switch, git restore, git grep, git blameを使ってみよう

Posted at

毎日息を吸うようにgitを使っているけど、gitのドキュメント見たことないなと思って実際に読んでみました。あまり使われていないが、便利そうなgitコマンドを見つけたので、共有します。

  • git switch
  • git restore
  • git grep
  • git blame
  • git log -p <filename>
  • git commit --amend --no-edit

git switch

ブランチを切り替えるコマンドです。git checkoutでできるのに、なぜわざわざgit switchを使うのかって思っていましたが、この記事を読んで意図を理解しました。

簡単に説明するとgit checkout

  • git checkout <branch_name> ブランチ・コミットを切り替える
  • git checkout -- <file_name> ファイルの変更を廃棄する(直近のcommitに戻す)

2つの機能を持っているので、少し紛らわしいですね。数年前初めてgitを勉強した頃、けっこうハマっていた記憶があります。
version 2.23にブランチ・コミットを切り替える機能のみ持っている機能git switchがリリースされました。

# git checkout your_branchと同じ
git switch your_branch

ただ、新しくブランチを切る際のオプションは-bではなく、-cになっています。

# git checkout -b your_branchと同じ
git switch -c new_branch

git restore

git restoreは、ファイルの変更を廃棄して、直近のcommitに戻すというgit checkoutの残り半分の機能を持っています。

# git checkout -- file.txtと同じ
git restore -- file.txt

git checkoutをやめてgit switchgit restoreを使ったほうがわかりやすいと思います。特に初心者の方はぜひ試してみてください。

git grep

kerasのリポを借りて説明します

https://github.com/keras-team/keras

特定キーワードが含まれているファイルを検索するコマンドです。挙動はvscodeの検索機能(cmd/ctrl + shift + f)とほぼ同じですが、ターミナルから実行できるので、個人的には使いやすいと思います。

git grep "bash"
.devcontainer/Dockerfile:    && apt-get install -y sudo bash \
.devcontainer/Dockerfile:CMD ["/bin/bash"]
.devcontainer/devcontainer.json:        "terminal.integrated.defaultProfile.linux": "bash"
ISSUE_TEMPLATE.md:```bash
keras/distribute/sidecar_evaluator.py:    ```bash
keras/kokoro/github/ubuntu/cpu/build.sh:#!/bin/bash
keras/kokoro/github/ubuntu/gpu/build.sh:#!/bin/bash
keras/tools/gpu_build/parallel_gpu_execute.sh:#!/usr/bin/env bash
keras/tools/pip_package/build_pip_package.sh:#!/usr/bin/env bash
shell/format.sh:#!/bin/bash
shell/lint.sh:#!/bin/bash

git blame <filename>

行ごとの変更履歴を確認するコマンドです。コミットid、コミットした人、コミットの時間を一気に確認できます。

git blame README.md
e1a17d32f (Francois Chollet      2021-06-15 13:29:07 -0700   1) # Keras: Deep Learning for humans
37a1db225 (Francois              2015-03-27 17:59:42 -0700   2) 
5b3cf890c (fchollet              2017-11-05 13:00:03 -0800   3) ![Keras logo](https://s3.amazonaws.com/keras.io/img/keras-logo-2018-large-1200.png)
059caec89 (Francois Chollet      2017-09-18 12:39:52 -0700   4) 
e1a17d32f (Francois Chollet      2021-06-15 13:29:07 -0700   5) This repository hosts the development of the Keras library.
e1a17d32f (Francois Chollet      2021-06-15 13:29:07 -0700   6) Read the documentation at [keras.io](https://keras.io/).
78691340e (Gabriel de Marmiesse  2019-09-15 07:40:35 +0200   7) 
88373a906 (Francois Chollet      2021-06-18 10:12:46 -0700   8) ## About Keras
e1a17d32f (Francois Chollet      2021-06-15 13:29:07 -0700   9) 
88373a906 (Francois Chollet      2021-06-18 10:12:46 -0700  10) Keras is a deep learning API written in Python,
88373a906 (Francois Chollet      2021-06-18 10:12:46 -0700  11) running on top of the machine learning platform [TensorFlow](https://github.com/tensorflow/tensorflow).
88373a906 (Francois Chollet      2021-06-18 10:12:46 -0700  12) It was developed with a focus on enabling fast experimentation.
88373a906 (Francois Chollet      2021-06-18 10:12:46 -0700  13) *Being able to go from idea to result as fast as possible is key to doing good research.*
88373a906 (Francois Chollet      2021-06-18 10:12:46 -0700  14) 
88373a906 (Francois Chollet      2021-06-18 10:12:46 -0700  15) Keras is:
88373a906 (Francois Chollet      2021-06-18 10:12:46 -0700  16) 
b6e8b9921 (TensorFlower Gardener 2022-03-10 14:27:15 -0800  17) -   **Simple** -- but not simplistic. Keras reduces developer *cognitive load*
b6e8b9921 (TensorFlower Gardener 2022-03-10 14:27:15 -0800  18)     to free you to focus on the parts of the problem that really matter.
b6e8b9921 (TensorFlower Gardener 2022-03-10 14:27:15 -0800  19) -   **Flexible** -- Keras adopts the principle of *progressive disclosure of
b6e8b9921 (TensorFlower Gardener 2022-03-10 14:27:15 -0800  20)     complexity*: simple workflows should be quick and easy, while arbitrarily
b6e8b9921 (TensorFlower Gardener 2022-03-10 14:27:15 -0800  21)     advanced workflows should be *possible* via a clear path that builds upon
b6e8b9921 (TensorFlower Gardener 2022-03-10 14:27:15 -0800  22)     what you've already learned.
b6e8b9921 (TensorFlower Gardener 2022-03-10 14:27:15 -0800  23) -   **Powerful** -- Keras provides industry-strength performance and
b6e8b9921 (TensorFlower Gardener 2022-03-10 14:27:15 -0800  24)     scalability: it is used by organizations and companies including NASA,
b6e8b9921 (TensorFlower Gardener 2022-03-10 14:27:15 -0800  25)     YouTube, and Waymo.
88373a906 (Francois Chollet      2021-06-18 10:12:46 -0700  26) 
88373a906 (Francois Chollet      2021-06-18 10:12:46 -0700  27) ---

git log -p <filename>

ファイルの変更履歴を確認するコマンドです。

git log -p README.md

コミットの履歴だけでなく、差分も同時に確認できます。

Author: Ikko Ashimine <eltociear@gmail.com>
Date:   Mon May 23 18:28:11 2022 +0900

    Fix typos

diff --git a/README.md b/README.md
index 37675e0a..09d1b7ce 100644
--- a/README.md
+++ b/README.md
@@ -174,7 +174,7 @@ version maps to a specific stable version of TensorFlow.
 The table below shows the compatibility version mapping
 between TensorFlow versions and Keras versions.
 
-All the release branches can be found on [Github](https://github.com/keras-team/keras/releases).
+All the release branches can be found on [GitHub](https://github.com/keras-team/keras/releases).
 
 All the release binaries can be found on [Pypi](https://pypi.org/project/keras/#history).
 

commit 5b6df0f0e67463ce9109c3f9799a26356f8cc3e5
Merge: 1601e591 4a7e71b7
Author: TensorFlower Gardener <gardener@tensorflow.org>
Date:   Tue Mar 22 09:20:44 2022 -0700

    Merge pull request #16259 from ahmedopolis:patch-1
    
    PiperOrigin-RevId: 436492033

commit 4a7e71b7502f455e2ad6b4c8e3cf26de75728a7c
Author: François Chollet <francois.chollet@gmail.com>
Date:   Mon Mar 21 13:41:49 2022 -0700

    Update README.md

git commit --amend --no-edit

コミットしちゃったけど、軽微な修正を更に追加したい場合、もう一つのコミットを作るの(コミットメッセージを考えるの)がめんどくさいですよね。

git add file.txt
git commit --amend --no-edit

を使うと、追加した軽微な修正は直前のコミットに統合されてとても便利です。

まだ使っていない方はぜひ試してみてください。

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