Git 實用技巧和觀念

  • 在 Faceook 做專案時,常常遇到的問題就是 branch 出來的版本要跟 Faceook mainline merge 時,要解一大堆 conflict, 而很多 code 若不是 owner, 很難知道 conflict merge 如何才是正解,且萬一 merge 時不小心 merge 錯誤,要如何再補救修正,往往 merge + build 就要花上一兩天的時間。
  • 在做 Google 專案時,常會發生 repo sync project error,但透過 gerrit 得到 git fetch 的指令,可以繞過 repo sync 不 work 的問題而順利下載/上傳 code.
  • 所以 git 在實用上可能會遇到不少狀況。git 的指令和參數很多,文件也很多,但一般網站卻很少提到:

※ git 與 repo 有何不同? (簡單說 git 由是 Linux 作者開發出來的 open source, repo 是 Google 以 python 包裝 git)

※git 與 svn 差異? (git 多了 stage, code commit 後要再 git push / repo upload 才真正上傳到 remote repository)

※如何只下載某 brach 的資料而不要下載全部的 branch 以縮短下載量和時間? (git clone --single-branch 或 git remote set-branches origin <BRANCH>; git fetch)

※git reset --hard 與 --soft 與 --mixed 有何不同? (--soft 只更動 repository 而不 reset working 和 staging 的狀態; --mixed 是 git reset 的預設行為,只保留 working 的狀態而 reset staging 和 repository; --hard 是全都更動)

※git fetch 與 pull 有何不同? (簡單來說 git pull = git fetch + git merge, 所以如果 staging 裡已有 commit 還沒上傳,如果用 git fetch 則會失敗,必須先 git push, 但用 git pull 就比較沒這個問題)

※git reset / revert / rebase 有何不同? (git reset 更改 HEAD 指向的位置、rebase 更改 base 避免 conflict、revert 是再做一個新的 commit,來取消不要的 commit, 若要取消 revert, 就再 revert 一次或 git reset HEAD^ --hard)

※github 與 gitlab 有何不同? (這說來話長請自行爬文吧)

  • 我常用的 git 指令 recap 如下:

$ git commit -c <SHA> --amend // 修改某 <SHA> 的 commit 的 comment

$ git log --graph --pretty=oneline // 以 ASCII 畫出 branch merge history,並且每一筆紀錄皆以一行敘述

$ git reset --hard HEAD^^ // rollback 到兩筆的狀態

$ git reset --hard HEAD~<N> // rollback 到前 N 筆

$ git reflog // 列出所有對 git 下的指令紀錄,含 SHA, 方便 rollback 過頭的時候還可以回到未來

$ git checkout -- <FILE> // 恢復 <FILE> 到修改前的內容

$ git checkout -b <BRANCH> // git branch <BRANCH> + git checkout <BRANCH>

$ git remote add origin [email protected]:3456/cloud_infra/Auto_Test.git // 以下先 BJ4 不解釋,或等我有空再補充

$ git reset HEAD <FILE>

$ git merge --no-ff <BRANCH>

$ git branch -d <BRANCH>

$ git push -u origin master

$ git stash list/apply

$ git remote -v

$ git rebase

$ git reset

$ git revert HEAD --no-edit

$ git fetch --prune

$ git tag

0 articles

results for ""

    No results matching ""