6
2

はじめてのgitコマンド

Last updated at Posted at 2020-12-20

はじめに

2020年8月に入社した新人エンジニアです。

gitコマンド??????? なんか難しいからSourceTree使お...
とずっとSourceTreeを使っていたのですが...

遡ること1ヶ月前。
とある先輩に

「SourceTree禁止!!!最低でも1年間、Gitが使いこなせるようになるまではGitコマンドを使え!!!!!!!」

とのお言葉を頂きまして...(本当はもっと優しく言ってくださいました。)
今まで使ったGitコマンドや使い方などを備忘録として残しておこうと思います。

リモートリポジトリのURL
https://github.com/◯◯◯◯/◯◯◯◯.git

開発ブランチ
development
masterから切ったブランチ。作業ブランチをココにマージする。

自分の作業ブランチ
feature/test
developmentから切ったブランチ。機能の追加や修正をするブランチ。

【git clone】リモートリポジトリのソースをローカルにクローン

$ git clone https://github.com/◯◯◯◯/◯◯◯◯.git

【git clone -b】 指定したのブランチのソースをローカルにクローン

git clone -b ブランチ名 https://github.com/◯◯◯◯/◯◯◯◯.git

リモートリポジトリにプッシュするまで...

// developmentブランチで
$ git checkout -b feature/test

// 現在のブランチの確認
$ git branch

// ステージングに追加・コミット
$ git add .
$ git commit -m 'コミットメッセージ'

// ブランチを移動
$ git checkout development

// リモートリポジトリとの差分を確認
$ git fetch

// リモートのdevelopmentの最新ソースをローカルのdevelopmentにプル
$ git pull origin development

// 作業ブランチに移動
$ git checkout feature/test

// ローカルのdevelopmentブランチのソースをfeature/testブランチにマージ
$ git merge development

// コンフリクトしていないか確認
$ git status

// リモートリポジトリにプッシュ
$ git push origin feature/test

// Github上でプルリクエストを作成!
// Githubからdevelopmentにマージしたら、リモートのdevelopmentも最新にしておく
$ git checkout development
$ git pull origin development

コミット後、安全にリモートブランチにプッシュする方法は先輩に伝授していただきました⇨【安全にリモートブランチにPushする

【git branch】ローカルブランチの一覧を表示

ローカルにあるブランチは何?
自分は今どこのブランチにいるの?
を確認する時に使う。

$ git branch
// 実行後
* development
  feature/test

「*」があるところが現在のブランチ

【git branch -r】リモートのブランチを表示

-r もしくは --remotes オプションをつける。

$ git branch -r
$ git branch --remotes

【git branch -a】ローカル・リモート全てのブランチを表示

-a もしくは --all オプションをつける。

$ git branch -a
$ git branch --all

【git checkout -b】新しくブランチを切って、そのブランチに移動

現在のブランチから新しいブランチを切ることになるので、現在のブランチは何か?を確認しておく。

$ git checkout -b feature/test

$ git branch
// 実行後
  development
* feature/test

【git checkout -b】リモートブランチをローカルに持ってくる & そのブランチに移動

リモートリポジトリのnew_branchブランチをローカルに持ってくる場合

$ git checkout -b new_branch origin/new_branch

$ git branch
// 実行後
  development
  feature/test
* new_branch // リモートのブランチが追加されている

【git add】編集・追加・削除したファイルをステージングに追加

コミットしたいファイルを追加する。

変更したファイル全てをステージングに追加↓

$ git add .

ファイルごとにステージングに追加したい↓

$ git add welcome.blade.php

【git commit】ステージングに追加したファイルをローカルリポジトリに記録

git logでコミット履歴を見た時に、
何をしたのかひと目で分かるようなメッセージを書く!

$ git commit -m 'コミットメッセージ'
// 実行後
[development fbcdc2f] コミットメッセージ
 1 file changed, 1 insertion(+), 1 deletion(-)

【git checkout [ブランチ名]】ブランチの移動

別のブランチに移動したい時。

$ git branch
// 実行後
  development
* feature/test // 現在のブランチ

$ git checkout development

$ git branch
// 実行後
* development // 現在のブランチ 「*」が移動している
  feature/test

【git fetch】リモートリポジトリから最新の情報を取得する

ローカルリポジトリのorigin/developmentが最新になる。
この時点では自分の作業ファイルなどは変更されない。

$ git fetch

【git merge】取得した最新の情報を現在のブランチに取り込む

git fetchで最新情報を取得した後に行う。
ここまですると、自分の作業ファイルも変更される。

$ git merge origin/development

【git pull】リモートリポジトリの最新のソースをローカルリポジトリに取り込む

git fetchgit mergeを一気に行っている。

$ git pull origin development

【git status】前回のコミットからの変更内容を表示

git add したっけ?
git commitしたっけ?
今どんな状態?
を確認する時に使う。

git addする前↓

$ git status
// 実行後
On branch development
Your branch is up to date with 'origin/development'.

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:   Test/resources/views/welcome.blade.php

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

git addした後、git commitする前↓

$ git status
// 実行後
On branch development
Your branch is up to date with 'origin/development'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   Test/resources/views/welcome.blade.php

【git push】ローカルリポジトリにコミットした履歴をリモートリポジトリにアップロード

$ git push origin feature/test
  .
  .
  .
  * [new branch]      feature/test -> feature/test

エラーが出なければGithub上でプルリクエストを作成できる。
レビューしてもらい、OKだったらGithub上からdevelopmentにマージしてもらう。

【git log】今までのコミット履歴を確認する

コミットメッセージが一覧で表示される。

$ git log
// 実行後
commit 1ab995b929ff92a4d180d7d0786e0f6496c884dd (origin/feature/test)
Author: emika_0402 <emika_0402@test.co.jp>
Date:   Mon Dec 21 12:00:00 2020 +0900

    コミットメッセージ

コミット履歴を1行で表示したい時は↓

$ git log --oneline
// 実行後
1ab995b (origin/feature/test) コミットメッセージ

【git checkout】ステージング前のローカルの変更を破棄する

全ての変更を破棄↓

$ git checkout .

指定したファイルの変更を破棄↓

$ git checkout Test/resources/views/welcome.blade.php

【git branch -m】ローカルのブランチ名を変更する

$ git branch
* development
  feature/test

// feature/test を feature/test2 にしたい
$ git branch -m feature/test feature/test2

$ git branch
* development
  feature/test2

【git branch -d】ローカルのブランチを削除する

マージ済みのローカルブランチを削除する時↓

$ git branch -d feature/test

どんなブランチでも削除できる↓

$ git branch -D feature/test

【git stash】コミットしたくない変更を退避させる

作業中だけれど、別のブランチに変えたい...
間違ったブランチで作業してしまったので、別のブランチに変更点を移動させたい...

$ git stash
// 実行後
Saved working directory and index state WIP on 作業ブランチ名: 親コミットID 親コミットメッセージ

【git stash save】コメント付きで変更点を退避させる

$ git stash save 'コメント'
// 実行後
Saved working directory and index state On 作業ブランチ名: コメント

【git stash -u】新規ファイルを退避させたい

$ git stash -u

【git stash save -u】新規ファイルを含む変更をコメント付きで退避させたい

$ git stash save -u 'コメント'

【git stash push】特定のファイルのみ退避させたい

$ git stash push -m 'コメント' -- ファイル名

【git stash list】退避させた内容のリストを見る

stash@{0}はスタッシュの管理番号みたいなもので、
一番新しいstashがstash@{0}

$ git stash list
// 実行後
stash@{0}: WIP on branch3: 789ijkl third commit
stash@{1}: WIP on branch2: 456efgh second commit
stash@{2}: WIP on branch1: 123abcd first commit

【git stash apply】退避させた変更を現在のブランチに戻す

// 直前の変更(stash@{0})を戻す
$ git stash apply

// 特定のstashを指定して戻す
$ git stash apply stash@{2}

【git stash drop】退避させた変更を削除する

// 直前の変更(stash@{0})を削除する
$ git stash drop

// 特定のstashを指定して削除する
$ git stash drop stash@{2}

【git stash pop】退避させた変更を現在のブランチに戻す&リストから削除する

git stash applygit stash dropを一気にやってくれるイメージ

// 直前の変更(stash@{0})を戻す
$ git stash pop

// 特定のstashを指定して戻す
$ git stash pop stash@{1}

【git stash clear】退避させた変更を全て削除する

git stash listした時に表示されている内容を全て削除したい時

$ git stash clear

さいごに...

新しく使ったgitコマンドがあったらどんどん増やしていこうと思います!

6
2
2

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