gitコマンド覚書

色付け

git config --global color.diff auto
git config --global color.status auto
git config --global color.branch auto

更新

cd git/test-core/
git status
git pull

最新に上書き

git status
git checkout -- JP/script/test/ttttt.pl
git status
git pull

リポジトリを作成する+gitにpushする

git branch
git checkout -b topic_branch_xxxxx
git branch
git push origin topic_branch_xxxxx

ファイルを追加する

git add .
git commit
git commit -m "test desu"
git status
git push origin topic_branch_xxxxx

ファイル追加+ファイル修正/削除

git add -A

※(1.6.0以前の場合は)
git add . && git add -u

ブランチの切り替え

git checkout master
git checkout branch_xxx

特定ディレクトリ配下を綺麗にする

git clean -n  /controller/components/ <--削除する対象を表示 
git clean -f  /controller/components/ <--削除

間違えてコミットしたものを戻す

git reset hard HEAD~1

修正したものを綺麗な状態にもどした

git checkout HEAD--.

ファイルを削除したい

git status  <---delete ~ と出てくる
git add -u
git commit -m "sakujo"

ブランチを削除したい

git branch -d topic_branch_xxxx

接続されているリモートを見たい

git config -l | grep remote

2個前のcommit時点に戻りたい

①最新のコミットを取り消し
git reset --hard HEAD~1
②綺麗な状態に戻す
git checkout HEAD -- .
③管理外にできたファイルを削除する
git clean -n
git clean -f

あるバージョンまでコミットを戻したい

git-revert(戻すバッチを生成)/
git-reset(なかった事にする) http://d.hatena.ne.jp/murank/20110327/1301224770
2通りあるが、共有リポジトリの場合はgit-revertを使用。

①git log または git log -p でどこまで戻すか決める。
----------------------------------------------
commit 08b5b8c8726001b0acc28574aa5daaf88594d0ce
Merge: 2e607d9 31e234e
Author: root <root@www15227u.sakura.ne.jp>
Date:   Fri Aug 24 18:34:08 2012 +0900
----------------------------------------------
②git revert f64dc32
③git checkout -f チェックアウト
④git clean -n -> git clean -f でいらないファイルを削除
⑤git push

コンフリクト編

作業中に(commitまでいかない)別のブランチに切り替えたい

①topic_branch_Aで修正
②git checkout master
----------------------------------------------
error: Your local changes to the following files would be overwritten by checkout:
        controllers/vv.txt
Please, commit your changes or stash them before you can switch branches.
Aborting
----------------------------------------------
③git stash -> git checkout master -> いろんな修正
④git checkout topic_branch_A -> git stash pop

pullしたらコンフリクトした場合の対処

①user_1からaaa.txtに修正->push
②user_2からaaa.txtに修正->pull
----------------------------------------------
error: Your local changes to the following files would be overwritten by merge:
        controllers/test_re.txt
Please, commit your changes or stash them before you can merge. <--コミットするかstashしろ。
----------------------------------------------

③user_2からgit add . -> git commit -> git pull
Auto-merging controllers/test_re.txt
CONFLICT (content): Merge conflict in controllers/test_re.txt
Automatic merge failed; fix conflicts and then commit the result. <--コンフリクトを修正しろ。

④user_2から修正-> git add. -> git commit -> git pull

Already up-to-date.

pushしたらコンフリクトした場合の対処

①user_1からaaa.txtに修正->push
②user_2からaaa.txtに修正->push
----------------------------------------------
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.
----------------------------------------------

③user_2からgit pull
----------------------------------------------
Auto-merging controllers/test_re.txt
CONFLICT (content): Merge conflict in controllers/test_re.txt
Automatic merge failed; fix conflicts and then commit the result.
----------------------------------------------

④user_2で修正->git add. ->git commit->git push
||< 

**削除編
>||
git にあるファイルを削除する
git rm ./controllers/vv.txt

git add .を取り消す
git reset HEAD

LOG編

git log --graph --oneline
git log --graph --pretty='format:%C(yellow)%h%Cblue%d%Creset %s %C(black bold)%an, %ar%Creset'
http://hail2u.net/blog/software/easy-to-read-git-log.html

DIFF編

git diffいろいろ
HEAD^ - HEAD - インデックス <=> ワーキングツリー
git diff
   
HEAD^ - HEAD <=> インデックス - ワーキングツリー
git diff --cached

HEAD^ - HEAD <= インデックス => ワーキングツリー
git diff HEAD

HEAD^ <=> HEAD - インデックス - ワーキングツリー
git diff HEAD^..HEAD

別コミットの差分
git diff コミットA...コミットB

戻りたい編

最新の状態(HEAD)に戻したい

git checkout HEAD -- .