辞書的に使いたいのと一元管理したい。
https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F70342%2F47f54fc4-60fa-9384-ba77-0ef4c0ed958d.png?ixlib=rb-1.2.2&auto=format&gif-q=60&q=75&s=e293b6c0ef856a14f44c07dcb5f19a81qiita-user-contents.imgix.net
基本原理
(0) 元の状態 | | | file change saved (ファイル変更) | <reset --hardはここまで> | | (1) ワーキングツリー | | | git add (ステージングする / 登録する) | <reset --mixedはここまで> | * resetのデフォルトはmixed | | (2) インデックス | | git commit (HEADの位置を1つ進める) | <reset --softはここまで> | | ======================= (3) ローカルブランチ [ローカルリポジトリ内に存在] | | | git push [前半部分] | | | (*) リモートtrackingブランチ | (origin/feautre) | [ローカルリポジトリ内に存在] | ======================= | | | git push [後半部分] / git fetch | | (4) リモートブランチ [ローカルリポジトリ内に存在]
[EX.1] commitの取り消し (パターンA)
# commitの取り消し $ git reset --hard HEAD^ --hard:コミット取り消した上でワークディレクトリの内容も書き換えたい場合 --soft:ワークディレクトリの内容はそのままでコミットだけを取り消したい場合 HEAD^:直前のコミット HEAD~{n} :n個前のコミット
[EX.1] commitの取り消し (パターンB)
(もしくは topicブランチ[のHEAD]を、指定のコミットまで巻き戻したい)
# commitの取り消し $ git reset --hard [コミットハッシュ値]
[EX.2] ファイルを指定コミットまで戻す
# ファイルを指定コミットまで戻す $ git checkout <コミットのハッシュ値> <ファイル名>
(0-A) ワーキングディレクトリの変更で、追跡済 (かつステージング前) のものを削除したい
$ git reset --hard HEAD # ファイル指定はできない。
(0-B) ワーキングディレクトリの変更で、未追跡 (かつステージング前) のものを削除したい
// 削除対象の確認 $ git clean -dn // 削除の実行 $ git clean -df
(0-C) ワーキングディレクトリの変更(saved)を戻したい?
(これはステージング状態から、ステージング前に戻すということかな?)
# すべて $ git checkout -- . # ファイル指定 $ git checkout -- <ファイル名>
(1) ステージング(git add)したものをワーキングツリーのみが変更された状態に戻したい
追跡(track)されている?
=> Yes (緑文字 modified:)なら、
# 全て $ git reset HEAD # ファイル指定 $ git reset HEAD <ファイル名> # recursivelyオプションは有効か? (ディレクトリ丸ごと) git reset HEAD <ディレクトリ名>
追跡(track)されている?
=> No (緑文字 new file:)なら、
# 全てのファイルを取り消し git rm --cached -r . # ファイル指定 $ git rm --cached <ファイル名> # recursivelyオプション git rm --cached -r vendor/
(2) リモートブランチの名称変更
1.ローカルのブランチ名を変更する $ git branch -m <古い> <新しい> ※現在使用しているブランチ名を変更する場合は下記でOK 2.リモートのブランチを削除
$ git push origin :hoge
<br> (ex) ブランチの削除
[ローカル] $ git branch --delete [ブランチ名] [リモート] $ git push --delete origin [ブランチ名] or $ git push origin :[ブランチ名]
3.ブランチ名を変更したローカルブランチをプッシュ $ git push origin fuga
(3) リポジトリの設定変更 (git config)
systemレベル / globalレベル / localレベル
情報の一覧表示
$ git config --list --local $ git config --list $ git config --list --global ($ git config --list --system)
リポジトリごとにconfig変更
$ git config --local user.name [変更ユーザ名] $ git config --local user.email [変更Email]
(4) git-flow
(5) 現在bashrcに書いているAlias
alias gs='git status' alias gd='git diff' alias gcm='git commit -m' alias gca='git commit --amend' alias glo='git log --oneline' alias glog='git log --oneline --graph' alias mkpr='hub pull-request' # PRを書く alias prs='ghi | grep ↑' # 現在のレポジトリのPR取得 alias gch='git checkout' alias gb='git branch -a' alias gmnoff='git merge --no-ff' alias gmerge='git merge' alias gpo='git push origin'
(5) 今後参考にしたAlias
[6] リモートとローカルでブランチ名が異なる
$ git push origin (ローカル):(マスター) # 同じ時 $ git push origin test # 異なる時 $ git push origin test:test2
[7] git stash pop, git stash list
[8] git rebase -i
[9] git merge --squash
[10] git revert
[11] git cherry-pick
# ローカルブランチから取得する場合 $ git cherry-pick {対象となるコミットID}
# リモートブランチから取得する場合 $ git fetch origin $ git cherry-pick {対象となるコミットID}
[12] git fetch --prune
他人が削除したリモートブランチがローカルに残ってる git branch -a を実行したときに、恐ろしい量のbranchリストが出てくるって人はこれが原因かもしれない。 他人が追加したリモートブランチはfetchで取ってこれるが、削除したブランチはそのまま残る。 削除されたリモートブランチをローカルに適用するにはオプションが必要。
[13] ブランチを前に戻す (masterブランチもこの方法でOK)
$ git checkout -b [仮の一時的なブランチ名] $ git branch -m [仮の一時的なブランチ名] [元に戻したいブランチ名]
[14] featureブランチを過去に戻したい
$ git rebase -i It seems that there is already a rebase-merge directory, and I wonder if you are in the middle of another rebase. If that is the case, please try git rebase (--continue | --abort | --skip) If that is not the case, please rm -fr "/Users/[ユーザ名]/Projects/laradock_projects/[プロジェクト名]/.git/rebase-merge" and run me again. I am stopping in case you still have something valuable there. $ rm -fr "/Users/[ユーザ名]/Projects/laradock_projects/[プロジェクト名]/.git/rebase-merge" $ gs On branch feature nothing to commit, working tree clean $ gloga
[15] remote set-url origin hugahugaとremote add originの違い
[16] remoteをoriginから他の名称に変更したい。
$ git remote rename <old_name> <new_name>
[17] git diffコマンド
[18] git log --grep=検索文字列
[19] 特定ファイルを特定のcommitに戻す
[20] Git の delete オプションで消えるもの
(-d) , (--delete)
ポイント
- ローカルでマージされているか否か。
git branch -a
で大量に出てくるブランチ一覧の中で、
既にマージしたものを消したいときは、
とりあえず-d
オプションを試すと良い。
もしマージされていなければ、-D
で強制削除してくださいと案内が出る。
[21] gitの管理対象から特定のファイル、ディレクトリを削除する
ファイルを残したいとき --cachedオプションを付けることにより、 ファイルを残したまま管理対象から外すことができます。
git rm --cached [削除したいファイル]
[参照] qiita.com