1. 代碼倉(cāng)庫(kù)及工作區(qū):
- 工作區(qū) working directory
- 暫存區(qū) staging area
- 倉(cāng)庫(kù)區(qū) repository
查看遠(yuǎn)程倉(cāng)庫(kù)
git remote -v
添加遠(yuǎn)程倉(cāng)庫(kù)
git remote add <repo_name> <git_url>
刪除遠(yuǎn)程倉(cāng)庫(kù)
git remote rm <repo_name>
重命名遠(yuǎn)程倉(cāng)庫(kù)
git remote rename <old_repo_name> <new_repo_name>
2. 代碼從拉到推過(guò)程
git fetch origin <branch>
git merge <branch>
git add xxx
git commit -m "xxx"
git push origin <branch>
3. 替換本地改動(dòng)
撤銷(xiāo)工作區(qū)修改
git checkout -- <filepath>
撤銷(xiāo)暫存區(qū)修改
git reset HEAD <filepath>
用遠(yuǎn)程倉(cāng)庫(kù)中的代碼覆蓋本地代碼
git fetch origin <branchname>
整個(gè)工作區(qū)用遠(yuǎn)程倉(cāng)庫(kù)的代碼分支代替
git reset --hard origin/<branchname>
git reset --hard HEAD
單個(gè)文件用遠(yuǎn)程文件代替
git checkout origin/master -- platform/src/utiltp/CmTraceFromT120.cpp
git checkout HEAD -- src/evt/CXmlDSEvent.cpp
4. 創(chuàng)建切換刪除分支
-
創(chuàng)建分支
git checkout -b <branchname>
-
切換分支
git checkout <branchname>
-
刪除分支
git branch -d <branchname>
刪除遠(yuǎn)程分支:
git push origin --delete <branchname>
or git push origin :<branchname>
清空本地分支:
git remote prune origin
-
查看分支
git branch -a
-
重命名分支
git branch -m <old_branch_name> <new_branch_name>
git push origin <new_branch_name>
5. 查看歷史
顯示最近5次的提交的概要?dú)v史記錄
git log --graph --stat --oneline -5
顯示最近3次的提交的詳細(xì)歷史記錄
git log -p -3
6. 比較區(qū)別
1) 比較工作區(qū)和暫存區(qū) git diff
2) 比較暫存區(qū)和倉(cāng)庫(kù)區(qū) git diff --cached
3) 比較工作區(qū)和倉(cāng)庫(kù)區(qū) git diff HEAD
git diff ref1:path/to/file1 ref2:path/to/file2
一般來(lái)說(shuō), ref1 and ref2 可以是分支名, 也可是倉(cāng)庫(kù)名/分支名, 或者 commit 哈希
6.1 比較遠(yuǎn)程文件與本地文件
git diff remotename/branchname:remote/path/file1.txt local/path/file1.txt
6.2 比較兩個(gè)分支中的文件
git diff HEAD:local/path/file1.txt remotename/branchname:remote/path/file1.txt
7. 管理標(biāo)簽
7.1 顯示標(biāo)簽
git tag
7.2 添加標(biāo)簽
git tag -a <tagname> -m <comments>
7.3 推送標(biāo)簽
git push --tags
7.4 獲取遠(yuǎn)程標(biāo)簽
git fetch origin tag <tagname>
7.5 刪除標(biāo)簽
git tag -d <tagname>
git push origin :refs/tags/<tagname>
git push origin --delete tag <tagname>
8. 子模塊
添加子模塊
git submodule addhttps://chromium.googlesource.com/v8/v8.git
初始化子模塊
git submodule init
修改子模塊
git submodule update
9. 回退
git log -10
git reset xxx
git push origin <branchname> --force
10. 沖突解決
git stash
git stash pop