ツール | |

gitコマンドのメモ

自分用 適当に追記

  1. Clone
  2. リモートの変更を取り込む
  3. マージを取り消す
  4. 全てのリモートブランチをfetch
  5. 変更したファイルを表示
  6. コミットしていないファイルの変更を元に戻す
  7. コンフリクトした時
  8. インデックスに追加
  9. インデックスへの追加や削除を取り消す
  10. コミット
  11. コミットを取り消す
  12. プッシュ
  13. 一旦バージョン管理されたものを管理外にする
  14. リモートブランチを全て表示
  15. ローカルブランチを全て表示
  16. ローカルブランチを作る
  17. 現在のブランチを切り替える
  18. リモートブランチをチェックアウトする
  19. リモートブランチを削除する
  20. 歴史から抹消する
  21. ログを見る
  22. diffを見る
  23. ガベージコレクション
  24. 設定

Clone

リポジトリ名のディレクトリを作成してClone
git clone https://github.com/napoporitataso/piyopiyo.git
fooディレクトリを作成してClone
git clone https://github.com/napoporitataso/piyopiyo.git foo

リモートの変更を取り込む

リモートoriginのmasterブランチから
git fetch origin master
git merge origin/master

# または
git pull origin master

origin/ブランチ名 っていうのは、ローカルに存在する、リモートと同じものが保持されてるブランチの名前。

git fetch は、指定したリモートブランチからローカルの origin/ブランチ名 ブランチに、最新の状態を持ってくる。

git merge は、指定したブランチを現在の作業ブランチにマージする。

マージを取り消す

conflictした時、とりあえず元に戻せる

git merge --abort

全てのリモートブランチをfetch

未知のブランチも取れる

git fetch --all

変更したファイルを表示

git status
実行結果の例
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   path/to/staged/file.js

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   path/to/not/staged/file_1.js
        modified:   path/to/not/staged/file_2.js

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        path/to/nntracked/file.js
  • Changes to be committedgit add 済みの変更ファイル
  • Changes not staged for commitgit add してない変更ファイル
  • Untracked files … インデックスされてない (まだGitの管理下に無い) ファイル

コミットしていないファイルの変更を元に戻す

指定したファイルのみ
git checkout path/to/file
カレントディレクトリ以下の全ファイル
git checkout .

コンフリクトした時

全面的に相手側を採用
git checkout --theirs path/to/file
git add path/to/file
全面的に自分側を採用
git checkout --ours path/to/file
git add path/to/file
とにかく全部リモートのHEADに合わせる
git reset --hard origin/master

インデックスに追加

コミット対象にするということ。新規のファイル、変更したファイル、削除したファイル、どれもインデックスに追加する必要がある。

指定したファイルのみ
git add path/to/file
カレントディレクトリ以下の全ファイル
git add .
カレントディレクトリ以下の.jsで終わる全ファイル
git add *.js
変更・削除した全ファイル
git add --update
新規・変更・削除した全ファイル
git add --all

インデックスへの追加や削除を取り消す

git reset

コミット

git commit --message <message>

コミットを取り消す

HEAD^ はHEADの一つ前を表す

コミットだけ取り消す (インデックスはそのまま)
git reset --soft HEAD^
インデックスも取り消す (ファイルの変更はそのまま)
git reset --mixed HEAD^
ファイルの変更も全て取り消す
git reset --hard HEAD^
いま初回コミットにいるとき、初回コミットを取り消す (HEADを削除)
git update-ref -d HEAD

プッシュ

originにローカルのmasterブランチをプッシュ
git push origin master
originにローカルのすべてのブランチをプッシュ
git push origin --all
次回以降は git push だけで同じところに上げられるようにする
git push --set-upstream origin master
強制プッシュ
git push origin master --force

一旦バージョン管理されたものを管理外にする

指定したファイルのみ (ファイルシステムからは消さない)
git rm --cached path/to/file
git rm --cached *.abc
指定したディレクトリ (ファイルシステムからは消さない)
git rm --cached -r path/to/dir
ファイルシステムからも消す
git rm path/to/file
削除対象になるファイルのリストを表示するだけ
git rm --dry-run -r path/to/dir
マッチするファイルが一つも無くてもエラー終了させない
git rm --ignore-unmatch -r path/to/dir
(通常、マッチするファイルが一つも無ければエラー終了する)
fatal: pathspec 'xxx' did not match any files

リモートブランチを全て表示

git branch -r

ローカルブランチを全て表示

git branch

ローカルブランチを作る

ブランチを作るだけ
git branch <branchname>
ブランチを作って切り替える
git checkout -b <branchname>

現在のブランチを切り替える

git checkout <branchname>

リモートブランチをチェックアウトする

同じ名前でチェックアウト
git checkout -t <remotebranchname>
別名でチェックアウト
git checkout -b <localbranchname> <remotebranchname>

リモートブランチを削除する

git push --delete origin <remotebranchname>

歴史から抹消する

※危険、要バックアップ

filter-branch--index-filter に渡したコマンドを全てのコミットに対して実行し、インデックスを書き換える。よって rm コマンドを指定すると、歴史からファイルを抹消できる。

--prune-empty をつけると、ファイルが抹消された結果としてカラになってしまったコミットを自動で消せる。

完了後は要強制プッシュ。また、他のメンバーには再Cloneしてもらった方がいいらしい。

全てのブランチの歴史からファイルを抹消 (ローカルには残る)
git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch path/to/file" --prune-empty -- --all
複数ファイル・ディレクトリ
git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch path/to/file path/to/dir/*" --prune-empty -- --all

ログを見る

長すぎてページめくりが発生した場合、終了するには q を入力

全件表示
git log
直近5件のみ表示
git log -5
変更のあったファイル名と行数も表示
git log --stat
いろいろフォーマットして表示
git log --pretty=format:"[%ad] %h %an %s" --date=iso

diffを見る

あるファイルについて、直前のコミット⇔現在のdiff
git diff -- path/to/file
あるファイルについて、特定のコミット⇔現在のdiff
git diff <特定のコミットのSHA> -- path/to/file
改行コードを無視してdiff
git diff -w -- path/to/file

ガベージコレクション

git gc

設定

全ての設定値を表示
git config --list
ユーザー名とメールアドレスを設定
git config --global user.name napoporitataso
git config --global user.email napoporitataso@example.com
改行コードを勝手に統一させないようにする
git config --global core.autoCRLF false
設定を削除する
git config --global --unset <name>
認証情報を ~/.git-credentials に保存させる (※生パスワード書かれる注意)
git config --global credential.helper store

参考にさせていただいた記事