gitコマンドのメモ
自分用 適当に追記
Clone
git clone https://github.com/napoporitataso/piyopiyo.git
git clone https://github.com/napoporitataso/piyopiyo.git foo
リモートの変更を取り込む
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 committed
…git add
済みの変更ファイルChanges not staged for commit
…git 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
git reset --hard origin/master
インデックスに追加
コミット対象にするということ。新規のファイル、変更したファイル、削除したファイル、どれもインデックスに追加する必要がある。
git add path/to/file
git add .
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^
git update-ref -d HEAD
プッシュ
git push origin master
git push origin --all
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
git log -5
git log --stat
git log --pretty=format:"[%ad] %h %an %s" --date=iso
diffを見る
git diff -- path/to/file
git diff <特定のコミットのSHA> -- path/to/file
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 config --global credential.helper store
参考にさせていただいた記事
git fetch コマンドでリモートリポジトリの内容をローカルリポジトリに取り込む方法
【初心者向け】git fetch、git merge、git pullの違いについて - Qiita
git add -u と git add -A と git add . の違い | note.nkmk.me
Git の Reset, Checkout, Revert の違い - yu8mada
git config --global で設定した値を削除する方法 - Qiita
忘れやすい人のための git diff チートシート - Qiita
Gitリポジトリをメンテナンスして軽量化する - Qiita
git filter-branchで過去の全てのcommitから画像ファイルの追加/変更をなかったことにしてリポジトリを軽量化する - dskd