LoginSignup
0
0

Gitのコマンド集 v1

Posted at

はじめに

Gitを使っていく上で、使用したことがあるコマンドや、今後も使用するであろうコマンドを備忘録として記録
※学習途中なため、これから記事内容は加筆する可能性があります

Gitの基礎用

  • リポジトリ
     ⇒Git管理をするファイルのまとまり

  • コミット
     ⇒データを記録すること。リモートリポジトリには、反映されない。

  • ブランチ
     ⇒Gitで管理しているファイルの履歴を分岐する操作。開発上でメインとなるブランチを主軸として、開発過程でメインとなるブランチから派生させたい別のブランチ達がある。

  • マージ
     ⇒別のブランチで変更した結果を取り込む

Gitの初期に使ったコマンド

gitのバージョン確認

git --version

実行結果↓

git version 2.42.0.windows.2

gitの設定

自身のPC内のGit(アプリケーション)にメールアドレスとユーザー名を設定。
後から変更も可能。

git config --global user.email "任意のeメールアドレス"
(Ex):git config --global user.email "test.red.newt@gmail.com"

git config --global user.name "任意のユーザー名"
(Ex):git config --global user.name "test newt"

今後も使うことがあるコマンド

Gitで管理を開始する(リポジトリの初期化)

#### Git管理したいフォルダ直下で実行
git init

現在のGitの状況確認

git status

実行結果↓

On branch feature/excel_class # 今のブランチ名がfeature/excel_class
Your branch is up to date with 'origin/feature/excel_class'.

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:   __pycache__/excel_clas.cpython-38.pyc
        modified:   excel_clas.py  ←コミットが必要なファイル
        modified:   test_1.ipynb  ←コミットが必要なファイル

Untracked files:  ←gitの管理下にないファイルがあると言っている(Untracked=追跡できていない)
  (use "git add <file>..." to include in what will be committed)
        copy_excel_content_test/
        penguins_data_origin_test1.xlsx  ←追跡できてないファイル

no changes added to commit (use "git add" and/or "git commit -a")

つまるところ、

  • gitで管理されているファイルの中で変更があったものはChanges not staged for commitで示されている。
  • gitで管理されていないファイルがあるという報告はUntracked filesで示される。新しくファイルを作ると必ずと言っていいほど報告される。

なので、以下に示すcommit関連の作業を行って、git管理に自分で行ったファイルの変更を反映させる必要がある。別にしなくてもファイルに影響はないが、git管理は当然煩雑になったりする。

コミット予約

コミットを行う準備をするようなもの。先ほどあげたUntracked filesファイルたちはこのgit addのcommandによってgit管理下に置くことができる。

#### 指定のファイルをコミット予約
git add 任意のファイル名
(Ex):git add test_code.py

#### すべてのファイルをコミット予約
git add .

実行結果↓

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   __pycache__/excel_clas.cpython-38.pyc
        new file:   copy_excel_content_test/penguins_data_origin_test1.xls
        new file:   copy_excel_content_test/penguins_data_origin_test1.xlsb
        new file:   copy_excel_content_test/penguins_data_origin_test1.xlsm
        new file:   copy_excel_content_test/penguins_data_origin_test1.xlsx
        new file:   copy_excel_content_test/test1_origin.xlsx
        modified:   excel_clas.py
        new file:   penguins_data_origin_test1.xlsx
        modified:   test_1.ipynb

先ほどのUntracked filesたちがChanges not staged for commitの方へ移っています。ですが、まだgit管理内での反映が済んでいません。次のcommitコマンドでgit管理内へ反映させましょう。

コミット

git commit -m "任意のコメント文"
(Ex):git commit -m "excel test file commit"      

実行結果↓

[feature/excel_class 2d21008] excel test file commit
 9 files changed, 170 insertions(+), 30 deletions(-)
 create mode 100644 copy_excel_content_test/penguins_data_origin_test1.xls
 create mode 100644 copy_excel_content_test/penguins_data_origin_test1.xlsb
 create mode 100644 copy_excel_content_test/penguins_data_origin_test1.xlsm
 create mode 100644 copy_excel_content_test/penguins_data_origin_test1.xlsx
 create mode 100644 copy_excel_content_test/test1_origin.xlsx
 create mode 100644 penguins_data_origin_test1.xlsx

ここで、2d21008という番号みたいなのが出てきましたが、これはcommitした履歴番号の一部です。簡易的に表示されてます。
こういった番号は何に使うかと言うと、例えば、commitした内容や実際にgitへ反映した作業をある時点まで戻したいと言った時に使います。それ用のコマンドもしっかりあります。気になる方は是非調べてみてください。

コミットの履歴確認

常葉のままです。git操作を行った作業履歴です。先ほどの2d21008のような履歴番号も出てきます。

git log

実行結果↓

commit 2d2100833454ff3588e93178feb07a5264c350d2 (HEAD -> feature/excel_class)
Author: test newt <test.red.newt@gmail.com>
Date:   Sat Jan 27 23:02:05 2024 +0900

    excel test file commit

commit e1085be2ca1f1fa50aac69e67bd6fd2298544864 (origin/feature/excel_class)
Author: test newt <test.red.newt@gmail.com>
Date:   Sun Dec 24 17:31:02 2023 +0900

見ると分かりますが、履歴番号は2d2100833454ff3588e93178feb07a5264c350d2となっておりかなり長いですね。

※git管理をしていると、git logにより表示される履歴内容が大量になる時がある。
そういった時は、

q

を実行して停止することも可能

ブランチの作成

メインとするブランチから枝分かれしたブランチを作成

git checkout -b 任意のブランチ名
(Ex)git checkout -b feature/newt_branch

※ git checkout -b 任意のブランチ名 を実行後は作成したブランチに入っている状態になります。別のブランチに入ることを「切り替わる」または「スイッチした」と言います。

ローカルにあるブランチを確認

git branch

実行結果↓

* feature/excel_class   ←今いるブランチ名
  main

*で表されているのは今いるブランチ名

ブランチの切り替え

git checkout 存在しているブランチ名

マージ

git merge メインブランチに対してマージしたいブランチ名

# メインブランチに対して、feature/newt_branchというブランチの内容を反映(マージ)させたい時は
(Ex)git merge feature/newt_branch

実行結果↓

Already up to date.

その他 メモしたいコマンド

コミットされたブランチを元に戻す

commit_id:git logにより、元に戻したい時点のコミットID(作業履歴)

git reset --hard commit_id

※元に戻したという履歴は、記録されないことを知っておいてください。

おわりに

今回、初めてGitコードの内容について備忘録を書きましたが、まだ覚えているコマンドが少ないことが分かりました。
他にも知っていないコマンドもあるので、何度も使用したりして覚えていくしかないですね。
こういうコマンドって、説明文で理解するには限界があるので、実際にコードを何度も実行していくことがてっとりばやいですね。
今後も、本記事は頻繁に修正・加筆していくでしょう。
次回は、リモートリポジトリにローカルリポジトリの内容を反映(push)させていこうと思います。

最後に、ここまで記事を見ていただき、ありがとうございました。

参考文献

Gitの超初心者におすすめ!!

Gitのことをある程度知っている方はおすすめ、早見表!!

この記事↓はGitの構造を図解していて詳しかったです。

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