以前喜歡用GUI來(lái)管理代碼榴嗅,輕松簡(jiǎn)單,但遇到各種棘手的問(wèn)題時(shí)覺(jué)得還是終端好用陶舞。尤其是在Iterm + Zsh + agnoster + Solarized Dark下嗽测,簡(jiǎn)直想用他來(lái)寫日常代碼...
給張效果圖:
終端.png
下面介紹一些常用的:
Gits復(fù)原
改動(dòng)代碼不小心出現(xiàn)了難以解決的bug,或者做了大量無(wú)用改動(dòng)想要放棄時(shí)可以使用
git reset --hard
回復(fù)到?jīng)]修改的狀態(tài)肿孵。
Git 新增檔案
git add .
將資料先暫存到 staging area, add 之后再新增的資料, 于此次 commit 不會(huì)含在里面论咏。
git add filename
git add modify-file
修改過(guò)的檔案, 也要 add. (不然 commit 要加上 -a 的參數(shù))
git add -u # 只加修改過(guò)的檔案, 新增的檔案不加入.
git add -i # 進(jìn)入互動(dòng)模式Git 刪除檔案
git rm filenameGit 修改檔名、搬移目錄
git mv filename new-filename
Git status 看目前的狀態(tài)
git status # 看目前檔案的狀態(tài)
Git 提交
git commit
git commit -m 'commit message'
git commit -a -m 'commit -message' # 將所有修改過(guò)得檔案都 commit, 但是 新增的檔案 還是得要先 add.
git commit -a -v # -v 可以看到檔案哪些內(nèi)容有被更改, -a 把所有修改的檔案都 commit
Git 產(chǎn)生新的 branch
git branch # 列出目前有多少 branchgit branch new-branch # 產(chǎn)生新的 branch (名稱: new-branch), 若沒(méi)有特別指定, 會(huì)由目前所在的 branch / master 直接復(fù)制一份.
git branch new-branch master # 由 master 產(chǎn)生新的 branch(new-branch)
git branch new-branch v1 # 由 tag(v1) 產(chǎn)生新的 branch(new-branch)
git branch -d new-branch # 刪除 new-branch
git branch -D new-branch # 強(qiáng)制刪除 new-branch
git checkout -b new-branch test # 產(chǎn)生新的 branch, 并同時(shí)切換過(guò)去 new-branch
與 remote repository 有關(guān)
git branch -r # 列出所有 Repository branch
git branch -a # 列出所有 branch
Git checkout 切換 branch
git checkout branch-name # 切換到 branch-name
git checkout master # 切換到 master
git checkout -b new-branch master # 從 master 建立新的 new-branch, 并同時(shí)切換過(guò)去 new-branch
git checkout -b newbranch # 由現(xiàn)在的環(huán)境為基礎(chǔ), 建立新的 branch
git checkout -b newbranch origin # 于 origin 的基礎(chǔ), 建立新的 branch
git checkout filename # 還原檔案到 Repository 狀態(tài)
git checkout HEAD . # 將所有檔案都 checkout 出來(lái)(最后一次 commit 的版本), 注意, 若有修改的檔案都會(huì)被還原到上一版. (git checkout -f 亦可)
git checkout xxxx . # 將所有檔案都 checkout 出來(lái)(xxxx commit 的版本, xxxx 是 commit 的編號(hào)前四碼), 注意, 若有修改的檔案都會(huì)被還原到上一版.
git checkout -- * # 恢復(fù)到上一次 Commit 的狀態(tài)(* 改成檔名, 就可以只恢復(fù)那個(gè)檔案)
初次使用可能提示需要完善配置信息:
git config --global user.name "Your Name Comes Here" //配置使用git倉(cāng)庫(kù)的人員姓名
git config --global user.email you@yourdomain.example.com //配置使用git倉(cāng)庫(kù)的人員email
Git diff
git diff master # 與 Master 有哪些資料不同
git diff --cached # 比較 staging area 跟本來(lái)的 Repository
git diff tag1 tag2 # tag1, 與 tag2 的 diff
git diff tag1:file1 tag2:file2 # tag1, 與 tag2 的 file1, file2 的 diff
git diff # 比較 目前位置 與 staging area
git diff --cached # 比較 staging area 與 Repository 差異
git diff HEAD # 比較目前位置 與 Repository 差別
git diff new-branch # 比較目前位置 與 branch(new-branch) 的差別
git diff --stat
Git Tag
git tag v1 ebff
git tag 中文 ebff # tag 也可以下中文, 任何文字都可以
git tag -d 中文 # 把 tag=中文 刪掉
Git log
git log # 將所有 log 秀出
git log --all # 秀出所有的 log (含 branch)
git log -p # 將所有 log 和修改過(guò)得檔案內(nèi)容列出
git log -p filename # 將此檔案的 commit log 和 修改檔案內(nèi)容差異部份列出
git log --name-only # 列出此次 log 有哪些檔案被修改
git log --stat --summary # 查每個(gè)版本間的更動(dòng)檔案和行數(shù)
git log filename # 這個(gè)檔案的所有 log
git log directory # 這個(gè)目錄的所有 log
git log -S'sso()' # log 里面有 sso() 這字串的.
git log --no-merges # 不要秀出 merge 的 log
git log --since="2 weeks ago" # 最后這 2周的 log
git log --pretty=oneline # 秀 log 的方式
git log --pretty=short # 秀 log 的方式git log --pretty=format:'%h was %an, %ar, message: %s'
git log --pretty=format:'%h : %s' --graph # 會(huì)有簡(jiǎn)單的文字圖形化, 分支等.
git log --pretty=format:'%h : %s' --topo-order --graph # 依照主分支排序
git log --pretty=format:'%h : %s' --date-order --graph # 依照時(shí)間排序
Git show
git show ebff # 查 log 是 commit ebff810c461ad1924fc422fd1d01db23d858773b 的內(nèi)容
git show v1 # 查 tag:v1 的修改內(nèi)容
git show v1:test.txt # 查 tag:v1 的 test.txt 檔案修改內(nèi)容
git show HEAD # 此版本修改的資料
git show HEAD^ # 前一版修改的資料git show HEAD^^ # 前前一版修改的資料
git show HEAD~4 # 前前前前一版修改的資料
Git reset 還原
git reset --hard HEAD # 還原到最前面
git reset --hard HEAD~3
git reset --soft HEAD~3
git reset HEAD filename # 從 staging area 狀態(tài)回到 unstaging 或 untracked (檔案內(nèi)容并不會(huì)改變)
Git grep
git grep "te" v1 # 查 v1 是否有 "te" 的字串
git grep "te" # 查現(xiàn)在版本是否有 "te" 的字串
Git stash 暫存
git stash # 丟進(jìn)暫存區(qū)
git stash list # 列出所有暫存區(qū)的資料
git stash pop # 取出最新的一筆, 并移除.
git stash apply # 取出最新的一筆 stash 暫存資料. 但是 stash 資料不移除
git stash clear # 把 stash 都清掉
Git merge 合并
git merge
git merge master
git merge new-branch
git merge # 合并另一個(gè) branch颁井,若沒(méi)有 conflict 沖突會(huì)直接 commit厅贪。若需要解決沖突則會(huì)再多一個(gè) commit。
git merge --squash # 將另一個(gè) branch 的 commit 合并為一筆雅宾,特別適合需要做實(shí)驗(yàn)的 fixes bug 或 new feature养涮,最后只留結(jié)果葵硕。合并完不會(huì)幫你先 commit。
git cherry-pick 321d76f # 只合并特定其中一個(gè) commit贯吓。如果要合并多個(gè)懈凹,可以加上 -n 指令就不會(huì)先幫你 commit,這樣可以多 pick幾個(gè)要合并的 commit悄谐,最后再 git commit 即可介评。
Git blame
git blame filename # 關(guān)于此檔案的所有 commit 紀(jì)錄
Git 還原已被刪除的檔案
git ls-files -d # 查看已刪除的檔案
git ls-files -d | xargs git checkout -- # 將已刪除的檔案還原
Git 維護(hù)
git gc # 整理前和整理后的差異, 可由: git count-objects 看到.
git fsck --fullGit revert 資料還原
git revert HEAD # 回到前一次 commit 的狀態(tài)
git revert HEAD^ # 回到前前一次 commit 的狀態(tài)
git reset HEAD filename # 從 staging area 狀態(tài)回到 unstaging 或 untracked (檔案內(nèi)容并不會(huì)改變)
git checkout filename # 從 unstaging 狀態(tài)回到最初 Repository 的檔案(檔案內(nèi)容變回修改前)
git Rollback 還原到上一版
Git remote 維護(hù)遠(yuǎn)端檔案
git remote
git remote add new-branch [http://git.example.com.tw/project.git](http://git.example.com.tw/project.git) # 增加遠(yuǎn)端 Repository 的 branch(origin -> project)
git remote show # 秀出現(xiàn)在有多少 Repository
git remote rm new-branch # 刪掉
git remote update # 更新所有 Repository branch
git branch -r # 列出所有 Repository branch抓取 / 切換 Repository 的 branch
git fetch origin
git checkout --track -b reps-branch origin/reps-branch # 抓取 reps-branch, 并將此 branch 建立于 local 的 reps-branch刪除 Repository 的 branch
git push origin :heads/reps-branch