2015年3月6日 星期五

git (指令/概念) 筆記

//指令參數
git commit -m "Fixed a typo." //短指令
git commit --message="Fixed a typo." //長指令
通常短指令是長指令相對應的縮寫,但有些指令只存在在其中之一。

//容器 = repository
一、建立repository
1.先開一個folder
mkdir ~/public_html
cd ~/public_html
echo 'My Website is alive' > index.html
2.轉換成repository
git init

二、在repository中加入檔案
git add index.html
//查詢status
git status
//commit(用-m參數加說明)
git commit -m "Your Commend"

三、在commit前要先將你的姓名跟信箱給註冊好,不然無法執行
git config user.name "Harvey"
git config user.email "harveyhu@hotmail.com"

四、檢視你的commit記錄
git log
將以時間最近往下排到時間最遠
log會顯示:作者(email)、commit時間、註解、commit id
//如果要查看更詳細的內容
git show yourCommitID
//如果不指名commitID,則會顯示最近的一筆
git show
//查看目前branch的詳細資料,--more=10代表最多顯示10筆commit
git show-branch --more=10

五、檢視兩個commit程式的差異(較早的commit \ 較近的commit)
git diff olderCommitID \ newerCommitID

六、移除檔案和更改檔名
//移除檔案
git rm poem.html
git commit - m "Remove a poem"
//更改檔名有兩種方式
1.先改檔名,再改git
mv foo.html bar.html
git rm foo.html
git add bar.html
git commit -m "Moved foo to bar"
2.直改用git mv
git mv foo.html bar.html
git commit -m "Moved foo to bar"

七、clone你的repository
//先到你要放new repository的folder,就會將my_website建立在這個folder中
git clone public_html my_website

//如果你有空的話,也可以用linux指令來看看兩邊有什麼差異
ls -lsa public_html my_website
diff -r public_html my_website

八、設定config檔
//git的config檔都是以.ini為副檔名的文字檔案
//它的優先順序如下

  1. .git/config:這個config的scope只影響repository之中的檔案,可以用--file來設定
  2. ~/.gitconfig:以使用者為scope,使用--global來設定
  3. /etc/gitconfig:影響範圍可以到全系統,這檔案可能在/usr/local/etc/gitconfig或者根本不存在。
//如果想要看目前的config設定,可以用-l
git config -l

//可以用--unset來移除某設定
git config --unset --global user.email

九、建立指令的縮寫
//建一個show-gragh別名,並讓它可在所有我建立的repository中都可以使用,用來取代冗長的log指令
git config --global alias.show-gragh \ ''log --graph --abbrev-commit --pretty=oneline

十、找出之前改出bug的commit
//使用git bisect
git bisect start
//告知目前HEAD是壞的
git bisect bad
//告知之前正確的版本是在何時
git bisect good v2.6.27
//接下來git會自動幫你用二分法挑選你之前的commit,你只要回應它是好的或是壞的
git bisect good
git bisect bad
//你可以看這次bisect的所有log
git bisect log
//你可以目前仍在範圍中的commit (--pretty是用來對輸出結果排版的參數)
git bisect visualize --pretty=oneline
//你如果查看目前的分支,就會發現git是幫你新建一個branch來執行bisect,當你要結束查詢時,git會幫你switch回原本的分支
git bisect reset
//如果你想重新來一次
git bisect replay

十一、檢視分支
//列出本地端所有分支名稱
git branch
//列出遠端追踨分支
git branch -r
//同時列出本地和遠端
git branch -a
//列出詳細branch資料
git show-branch

十二、diff的四種用法
git diff //比較目前工作目錄與上一次add的差異
git diff HEAD //比較目前工作目錄與上一次commit的差異
git diff --cache  //比較現在add的與上次commit的差異
git diff version1 version2 //比較兩次commit之間的差異(參數順序有差異)

沒有留言:

張貼留言