首先下圖是git的流程圖
相關概念
svn與git命令的對比
git常用命令
git status ./ // 查看當前庫的狀態
git diff ./ // 比較當前庫的修改情況
git add ./ // 將當前庫的代碼修改提交到暫存區
git commit ./ // 將代碼提交到本地分支
git commit --amend ./ // 追加修改
git reset HEAD~1 // 將當前庫恢復到HEAD的上一個版本
其他命令
git config -l // 參看配置信息
git show HEAD^ // 查看HEAD的上一個版本信息
git show HEAD~4 // 查看HEAD的上溯4代的信息
git reset --hard HEAD^^ // 回退兩個版本
git reset --hard 8308f03 // 回退到指定的commitID前7位的版本
git clean -dfx //清除庫上沒有的東西
git remote -v // 參看遠程倉庫
git branch -a // 參看遠程分支
git log --oneline --decorate --graph --all // 圖像顯示git log信息
git log --pretty=format:"%h - %cd %s" --graph // 列出指定格式的log
git log -since="2 weeks ago" // 顯示2周前到現在所有的歷史記錄
遠程同步
# 下載遠程倉庫的所有變動
$ git fetch [remote]
# 顯示所有遠程倉庫
$ git remote -v
# 顯示某個遠程倉庫的信息
$ git remote show [remote]
# 增加一個新的遠程倉庫,並命名
$ git remote add [shortname] [url]
# 取回遠程倉庫的變化,並與本地分支合並
$ git pull [remote] [branch]
# 上傳本地指定分支到遠程倉庫
$ git push [remote] [branch]
# 強行推送當前分支到遠程倉庫,即使有沖突
$ git push [remote] --force
# 推送所有分支到遠程倉庫
$ git push [remote] --all
撤銷
# 恢復暫存區的指定文件到工作區
$ git checkout [file]
# 恢復某個commit的指定文件到暫存區和工作區
$ git checkout [commit] [file]
# 恢復暫存區的所有文件到工作區
$ git checkout .
# 重置暫存區的指定文件,與上一次commit保持一致,但工作區不變
$ git reset [file]
# 重置暫存區與工作區,與上一次commit保持一致
$ git reset --hard
# 重置當前分支的指針為指定commit,同時重置暫存區,但工作區不變
$ git reset [commit]
# 重置當前分支的HEAD為指定commit,同時重置暫存區和工作區,與指定commit一致
$ git reset --hard [commit]
# 重置當前HEAD為指定commit,但保持暫存區和工作區不變
$ git reset --keep [commit]
# 新建一個commit,用來撤銷指定commit
# 后者的所有變化都將被前者抵消,並且應用到當前分支
$ git revert [commit]
# 暫時將未提交的變化移除,稍后再移入
$ git stash
$ git stash pop
repo常用命令
repo upload // 將代碼提交到gerrit.
repo abandon master // 放棄master分支
repo forall -c "git reset --hard HEAD" // 所有代碼執行git命令,回退到HEAD
// repo sync相當於git clone會把repository中的所有內容拷貝到本地,非首次運行repo sync相當於更新和合並.
// repo sync會更新.repo下面的文件,如果在merge的過程中出現沖突,這需要手動運行git rebase --continue.
repo sync -c -j 4
repo start master --all // 創建新分支
其他幫助參考
git使用的更詳細參考: 可以百度“Pro Git 中文版”這個很不錯的資料。
另外可以利用工具自帶的幫助功能查詢常用命令:
repo help
git help
git help [command]