git
参考:
table:commands
【初期化関連】
git用ディレクトリにセットアップ git init
リモートリポジトリの登録 git remote add origin <repository>
リモートリポジトリの削除 git remote rm origin
リモートリポジトリを取り込む git clone <url>
リモートから特定のブランチを指定してcloneする git clone -b <branch> <url>
【作業コピーの操作】
ステージング git add .
コミット git commit
直前のコミットに追加 git commit --amend --no-edit
直前のコミットメッセージを編集 git commit --amend
コミットの履歴を見る git log
コミット履歴のサマリを見る git log --pretty=oneline
特定のコミットをチェックアウト git checkout <commit-id>
作業コピーのリセット git checkout .
直前のコミットを無かったことにする git reset --soft HEAD^
【ローカルブランチ操作】
今のブランチから新たにブランチを作成 git checkout -b <new_branch_name>
ブランチの一覧表示 git branch
ブランチの切り替え git checkout <branch>
git switch <branch>
ブランチ名の変更 git branch -m <old_branch> <new_branch>
ブランチの削除 git branch -d <branch>
ブランチの強制削除 git branch -D <branch>
指定ブランチを取り込む(マージ) git merge <branch>
指定ブランチの続きへ移動(リベース) git rebase <branch>
元のコミット日付を維持してリベース git rebase -i --committer-date-is-author-date <branch>
指定のコミットだけを取り込む(チェリーピック) git cherry-pick <commit-id>
今のブランチを特定のコミットに戻す git reset --soft <commit-id> ソース維持
git reset --hard <commit-id> ソース破棄
作業コピーの変更を消す git reset --hard HEAD
直前のコミットを取り消す git reset --soft HEAD^
マージ済みブランチの一斉削除 省略
【リモートブランチ操作】
リモートブランチの一覧 bit branch -r
リモートブランチのチェックアウト git checkout -b <local_branch> origin/<remote_branch>
git switch -c <local_branch> origin/<remote_branch>
ローカルで作ったブランチをリモートにアップロード git push -u origin <local_branch>
【プル操作】
現在のブランチにリモートの変更を取り込む(pull) git pull
ローカルコミットがある状態でpullする git pull --rebase
【プッシュ操作】
プッシュ git push
(問題なければ)強制プッシュ git push --force-with-lease
【リモートブランチ操作】
リモートブランチの削除 git push --delete origin <branch>
【タグ付け】
今のコミットにタグを付ける git tag <tag-name>
タグの削除 git tag -d <tag-name>
【コードの比較】
差分を見る git log HEAD..<branch_or_commit>
片方だけの差分を見る git log HEAD...<branch_or_commit>(ドット3つ)
あるファイルの前回の状態を見る
code:git
git diff HEAD <filename>
リモートブランチをチェックアウトする
code:git
git pull
git checkout -b <local branch name> origin/<remote branch name>
今のブランチを特定のコミットに戻す
git reset --soft <commit id> : ソースは今のまま
git reset --hard <commit id> : ソースも戻す
コミットIDにHEADを使うと、最新のコミットになる
git reset --hard HEADで、作業コピーの変更を全消去できる
コミットIDにHEAD^を使うと、直前のコミットになる
git reset --soft HEAD^で、コミットの取り消しができる
commit idはgit logで調べられる
マージ済みブランチを一括削除
git branch --merged|egrep -v '\*|develop|master'|xargs git branch -d
ローカルとリモートで異なるコミットを行った場合のPull
git pull --rebase
git pullだけだと、デフォルト動作として、マージコミットが発生する
ブランチ同士のDIFF
git diff <branch1> <branch2>
コミットメッセージの修正
git commit --amend -m "cssを修正"
gitは、コミットメッセージの最初の空行までをタイトル、空行移行を本文と扱う
git rebase
複数のコミットをまとめて一つにしたり、コミット順を付け替えたりする
その他機能とか
git config周り
table:git config
git config --list 現在のコンフィグ一覧を見る
git config --global システム全体に設定
git config --local 今のフォルダにだけ設定
フォルダローカルでgitのコミットアカウントを操作
code:sh
git config --local user.name 名前
git config --local user.email "メールアドレス"
githubでissueを自動でクローズする
コミットのコメントに close #10 などとすると勝手にイシューをクローズしてくれる。らしい
コミット履歴からファイルを削除
でかいファイルを入れてしまって、githubにアップロードできなくなった時などに
多分最初の1回目でしか動作しない?
リベース
参考
リベースとマージは似た概念である
マージが、マージコミットを作成して複数のブランチを統合するのに対して、リベースは移植元のコミットを移植先に継ぎ足す形で統合する。
code:rebase.sh
git checkout experiment
git rebase master # masterの末尾にexperimentを接続する
code:after rebase.sh
git checkout master
git merge experiment # experimentの内容をmasterにマージ
マージと違って、あたかも移植元ブランチが存在しなかったような履歴になるので、スッキリする。
コミット履歴を編集したい
要らないFixコミットなんかを消すってのはよくやる作業になると思われる
考え方:
gitの履歴編集は、基本的にブランチAからブランチBへの転記の形で行われる。
この転記は同一ブランチ間でも行える。が、ちょっと難しくなるのでそれは忘れる
転記ブランチBを作って、元ブランチAから消すコミット、残すコミットを選択して転記する。
この操作は git rebase -i <toBranch> によって行われる
code:sample
$ git checkout edit-branch
$ git rebase -i --committer-date-is-author-date new-branch; こうしないとコミット日付が変わってしまう
$ git checkout master
$ git mearge new-branch
パッチの適用
code:git
# パッチの作成
$ git diff > diff.patch
# パッチの適用
$ git apply diff.patch