Git常用命令及技巧
git commit:
git commit --amend
撤銷上一次提交
git push:
git push [remote-name] [master]
推送資料到遠端倉庫git push origin :[branch-name]
刪除遠端分支.
git diff:
git diff --name-only 73a79c 2d49d2
檢視兩個版本中間改動過的檔案列表
git diff : workspace and index file.
git diff HEAD: workspace and commint
git diff --cached: index file and commit
git fetch:
git fetch [remote-name]
取回遠端倉庫的所有提交資訊
git clone:
git clone [url]
獲取遠端倉庫的 master 分支
git log:
git log -1 HEAD
顯示最後一次提交資訊git log -p
顯示每次提交的內容差異, 可加引數 -num 顯示最近num次提交差異git log --stat
僅顯示增改行數統計.git reflog
檢視rest, checkout 等操作紀錄.git log --pretty=oneline
用一行顯示每次提交資訊.git log --pretty=fuller
額外顯示提交日期.git log --since=2.weeks
顯示最近兩週修改git log --pretty=format:"%h - %an, %ar : %s"
選項 說明
%H 提交物件(commit)的完整雜湊字串
%h 提交物件的簡短雜湊字串
%T 樹物件(tree)的完整雜湊字串
%t 樹物件的簡短雜湊字串
%P 父物件(parent)的完整雜湊字串
%p 父物件的簡短雜湊字串
%an 作者(author)的名字
%ae 作者的電子郵件地址
%ad 作者修訂日期(可以用 -date= 選項定製格式)
%ar 作者修訂日期,按多久以前的方式顯示
%cn 提交者(committer)的名字
%ce 提交者的電子郵件地址
%cd 提交日期
%cr 提交日期,按多久以前的方式顯示
%s 提交說明
git checkout:
git checkout -- <file>
撤銷對檔案file的修改git checkout -b branch-name
建立並切換到分割槽.
git branch:
git breanch -d branch-name
刪除分支.git branch -D branch-name
強制刪除分支.git merge master
合併主分支到當前分支.
git remote:
git remote
列出當前專案的遠端庫git remote -v
顯示當前專案對應的克隆地址git remote add [shortname] [url]
新增遠端倉庫git remote show [remote-name]
檢視遠端倉庫的詳細資訊git remote [remote-old-name] [remote-new-name]
修改遠端倉庫名稱git remote rm [remote-name]
刪除遠端倉庫
git reset:
git reset HEAD <file>
撤銷已經被暫存(git add)的檔案git reset --soft: 撤銷並回退 commit, 不影響 index file, 撤銷到哪個位置由最後一個引數指定. git reset --soft HEAD^
git reset --hard: 撤銷 commit, index file and workspace
git reset --mixed: 預設選項, 撤銷 commit and index file, 只保留workspace.
git reset --: 刪除登記在 index file 裡的某個檔案.
git show-branch:
+(加號)表示所在分支包含此行所標識的commit
(空格)表示所在分支不包含此行所標識的commit
-(減號)表示所在分支是經過merge得到的,而所在行的內容即是merge的基本資訊。
*(星號)表示如果需要在某列標識+(加號),且此列為當前分支所在列,那麼則將+(加號)轉變為*(星號)。
git:
git gc
收縮空間git count-objects -v
檢視佔用空間大小.
git恢復刪除檔案
git 建立分支恢復檔案步驟:
git branch [recover-branch]
git checkout [recover-branch]
git checkout master
git branch -D recover-branch
要檢視刪除的檔案: git ls-files –deleted
恢復則需要從新checkout: git checkout – <deleted_file>
多個檔案同時操作可以使用xargs
git ls-fies -d | xargs git checkout --
git checkout -f 恢復刪除檔案
忽略提交某些檔案或資料夾
1. 在 .gitignore 檔案內寫入檔名或目錄名即可忽略提交, 但只對只對沒有被 track 的檔案或目錄有效, 對於已經加入版本管理的檔案是無效的.
2. 已經加入版本管理的檔案或目錄可使用如下命令忽略:
git update-index --assume-unchanged PATH
常見問題
1. 執行 git remote add origin... 時出現錯誤: fatal: remote origin already exists.
執行如下指令後再git remote add origin... :
git remote rm origin