推薦學習資源
- 官方網站:總得到官網下載安裝後才能練習、使用。
- try git:互動網頁做中學,快速熟悉基本指令。
- 連猴子都能懂的Git入門指南:目前看過最好上手的中文資源。
- Introduction to Git with Scott Chacon of GitHub
一定要實際操作,學習速度才會快。下的指令有問題通常Git Bash會提示該怎麼做。
同義名詞
同義(或近似)名詞弄清楚比較不會在看不同資源的時候混淆。
working tree = working directory = workspace
index = staging area
repository = .git repository = .git directory
不commit的檔案狀態暫存法(非stash)
可能是個人習慣不好,偶而會coding到一半不保存資料怕危險,commit又嫌不夠具代表性。一個解決方法是先commit,事後再git commit --amend。但因檔案能在working tree、index與last commit有三種不同版本,其實也可以先add,保存一版,在working tree持續工作,待整理到一個段落再add、commit。
假如很遺憾地將程式改爆炸了,取回檔案index版本(已add之版本)之方法為
$ git checkout -- myFile
假如更悲慘地在add前就爆炸了,取回檔案last commit版本之方法為
$ git checkout HEAD myFile
真的掛光光就git reset HEAD --hard吧,阿彌陀佛。
已push,但想reset回某次commit→revert
問題已發生,就先不檢討為什麼不在commit前做好測試了。reset掉已push出去的commit會造成下次push的困擾(簡單講就是我不會處理),這種情況下git revert可派上用場。
回到上上次commit
$ git revert HEAD
此時working tree與git reset HEAD^ --hard相同
git revert會自動commit,預設下迫使你進入可愛的Vim編輯環境。若想手動commit,可以git revert HEAD -n。如此working tree與index已變動,待自行commit。
若很不幸地需要回到上n+1次commit狀態
$ git revert HEAD~n..HEAD -n
如此會消除上n+1次commit後所做變動。若不-n,每revert一次commit就會建立一個commit。
以usb做為遠端repo,於兩台PC工作
大多網路資料都在網路上建立remote repository,但如果只是個人要在兩台PC切換工作,其實也可用usb當remote repository,以下簡單介紹之。
首先在PC1插入usb,假設usb路徑為/F,工作directory為/D/myFolder
$ cd /F
$ git clone /D/myFolder --bare
這樣就clone好bare的git repository到抽取式硬碟了。
移至PC2,假設usb路徑仍為/F,在想拷貝檔案資料夾的位置右鍵開啟Git Bash
$ git clone /F/myFolder.git
$ cd myFolder
$ git remote rename origin usb
進行一些修改,完成commit,將資料push回usb
$ git push usb master
回到原電腦,插入usb,更新資料
$ git remote add usb /F/myFolder.git
$ git pull usb
git pull usb也可拆解為git fetch usb、git merge remotes/usb/master,個人比較習慣後者。
編寫程式、修改、commit後再push資料
$ git push usb master
以上,loop pull與push。
沒有留言:
張貼留言