2013年10月1日 星期二

Git基本操作指令

這幾天看了一些Git和GitHub的資料,覺得Git真的是不錯的版本控管的軟體,在這邊記錄幾個比較常用的指令吧,網路上已經有許多的教學可以看了,因此,在這我主要是記錄一些我覺得我之後可能比較會用到的指令,需要進一步的資料的話,可以看我的參考資料。

初始化設定
  • 安裝git和設定
我主要是使用ubuntu的環境,所以使用apt-get來安裝,第一次使用時,需要設定使用者名稱和email。
sudo apt-get install git

git config --global user.name "your_name"

git config --global user.email


也可以設定alias name來簡化輸入,直接編輯~/.gitconfig
cd ~

vi .gitconfig



[vi]

alias

  checkout = co


之後如果需要連到github上的話,需要建立ssh的public key
cd ~

mkdir .ssh

cd .ssh

然後使用預設值,不用設定密碼
再將id_rsa.pub的全部內容,複製到github上(在個人設定有一個ssh key的地方)
ssh-keygen -t rsa -C your_email


  • 建立git的專案
初始化一個全新的資料夾,可以使用
git init


如果要複製專案下來,可以使用
git clone https|ssh

https的網址可以去github上面找,可以用來複製別人的專案
ssh可能用來處理自已的專案,就不用再輸入帳密


本機端處理

圖一:git的檔案狀態圖(資料來源[6])


從圖一中可以看出,在本機端中,git的檔案可分成三個階段,working, staging,和repository,我正在編輯的程式會存在working中,然後新增和修改的檔案我們會移至staging階段,當要寫這些修改的部分進行版本儲存時,會移至 repository。

  • 新增和儲存
當有檔案修改或新增時,需將檔案推至staging
git add your_file

或將所有新檔案全部推至staging
git add .

當需要將staging的檔案推至本機的repository時
git commit

然後會跳出vi視窗,然後輸入你要寫入的註解
可以使用-a和-m簡化步驟
-a 跳過add的步驟
-m 直接寫入comment

  • 刪除和移動檔案
需要移除檔案使可使用
git rm
需要移動檔案時,可使用
git mv

  • 狀態查詢
查詢git的狀態
git status

查詢commit的記錄
git log
查看各版本相異點
git diff

  • 建立tag
建立tag
git tag your_tag

  • 建立和管理branch
建立branch,ref可以參考tag或是它他branch
git branch branch_name [ref]

還原檔案或是移至分技
git checkout

將分技分併一起
git merge a1 a2

  • 進階應用
修改comment(應該會很少用到,確切使用方法也不清楚)
git rbase

  •  其它
其它可能用不到的,需要時再查詢
git show

git reset

git grep

git stash

git revert



遠端處理

  • 設定
遠端設定
git remote add new-branch project-url


  • 取得和上傳資料
取得遠端資料
git pull

上傳資料到遠端
git push




參考資料:
[1]  http://pulipuli.blogspot.tw/2013/02/github-part3-git.html
[2]  http://gogojimmy.net/2012/01/17/how-to-use-git-1-git-basic/
[3]  http://blog.longwin.com.tw/2009/05/git-learn-initial-command-2009/
[4]  http://ncu-csie-snmg.github.io/2013-NCU-CSIE-Website-Design-Competition/git.html
[5]  http://www.youtube.com/playlist?list=PLuW7NCGvKlyXAOC4Kn7VW0vtPNxSqICrX
[6]  http://git-scm.com/book/en/Getting-Started-Git-Basics







沒有留言:

張貼留言