# Git 常用命令
---
## 1.常用
---
```bash
git config --global core.editor vim # 設置默認編輯器為vim(git默認用nano)
git config core.ignorecase false # 設置大小寫敏感
git config --global user.name "YOUR NAME" # 設置用戶名
git config --global user.email "YOUR EMAIL ADDRESS" # 設置郵箱
git clone <url> # 克隆遠程版本庫
git init # 初始化本地版本庫
git remote add origin git@github.com:shw2018/test.git # 配置遠程git版本庫
git pull origin master # 下載代碼及快速合并
git push origin master # 上傳代碼及快速合并
git fetch origin # 從遠程庫獲取代碼
git branch # 顯示所有分支
git checkout master # 切換到master分支
git checkout -b dev # 創建并切換到dev分支
git commit -m "first version" # 提交
git status # 查看狀態
git log # 查看提交歷史
```
## 2.別名Alias
---
```bash
git config --global alias.br="branch" # 創建/查看本地分支
git config --global alias.co="checkout" # 切換分支
git config --global alias.cb="checkout -b" # 創建并切換到新分支
git config --global alias.cm="commit -m" # 提交
git config --global alias.st="status" # 查看狀態
git config --global alias.pullm="pull origin master" # 拉取分支
git config --global alias.pushm="push origin master" # 提交分支
git config --global alias.log="git log --oneline --graph --decorate --color=always" # 單行、分顏色顯示記錄
git config --global alias.logg="git log --graph --all --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(bold white)— %an%C(reset)%C(bold yellow)%d%C(reset)' --abbrev-commit --date=relative" # 復雜顯示
```
## 3.創建版本庫
---
```bash
git clone <url> # 克隆遠程版本庫
git init # 初始化本地版本庫
```
## 4.修改和提交
---
```bash
git status # 查看狀態
git diff # 查看變更內容
git add . # 跟蹤所有改動過的文件
git add <file> # 跟蹤指定的文件
git mv <old> <new> # 文件改名
git rm <file> # 刪除文件
git rm --cached <file> # 停止跟蹤文件但不刪除
git commit -m “commit message” # 提交所有更新過的文件
git commit --amend # 修改最后一次提交
```
## 5.查看提交歷史
---
```bash
git log # 查看提交歷史
git log -p <file> # 查看指定文件的提交歷史
git blame <file> # 以列表方式查看指定文件的提交歷史
```
## 6.撤消與回退
---
```bash
git reset --hard HEAD # 撤消工作目錄中所有未提交文件的修改內容
git reset --hard <version> # 撤銷到某個特定版本
git checkout HEAD <file> # 撤消指定的未提交文件的修改內容
git checkout -- <file> # 同上一個命令
git revert <commit> # 撤消指定的提交
```
## 7.分支與標簽
---
```bash
git branch # 顯示所有本地分支
git checkout <branch/tag> # 切換到指定分支或標簽
git branch <new-branch> # 創建新分支
git branch -d <branch> # 刪除本地分支
git tag # 列出所有本地標簽
git tag <tagname> # 基于最新提交創建標簽
git tag -a "v1.0" -m "一些說明" # -a指定標簽名稱,-m指定標簽說明
git tag -d <tagname> # 刪除標簽
git checkout dev # 合并特定的commit到dev分支上
git cherry-pick 62ecb3
```
## 8.合并與衍合
---
```bash
git merge <branch> # 合并指定分支到當前分支
git merge --abort # 取消當前合并,重建合并前狀態
git merge dev -Xtheirs # 以合并dev分支到當前分支,有沖突則以dev分支為準
git rebase <branch> # 衍合指定分支到當前分支
```
## 9.遠程操作
---
```bash
git remote -v # 查看遠程版本庫信息
git remote show <remote> # 查看指定遠程版本庫信息
git remote add <remote> <url> # 添加遠程版本庫
git remote remove <remote> # 刪除指定的遠程版本庫
git fetch <remote> # 從遠程庫獲取代碼
git pull <remote> <branch> # 下載代碼及快速合并
git push <remote> <branch> # 上傳代碼及快速合并
git push <remote> :<branch/tag-name> # 刪除遠程分支或標簽
git push --tags # 上傳所有標簽
```
## 10.打包
---
```bash
git archive --format=zip --output ../file.zip master # 將master分支打包成file.zip文件,保存在上一級目錄
git archive --format=zip --output ../v1.2.zip v1.2 # 打包v1.2標簽的文件,保存在上一級目錄v1.2.zip文件中
git archive --format=zip v1.2 > ../v1.2.zip # 作用同上一條命令
```
git打包命令會自動忽略.gitignore中指定的目錄和文件,以及.git目錄。
## 11.全局和局部配置
---
```bash
全局配置保存在:$Home/.gitconfig
本地倉庫配置保存在:.git/config
```
## 12.遠程與本地合并
---
如果在遠程創建了代碼倉,而且已經初始化,本地是具體的源代碼,那么工作流程應該是:
```bash
git init # 初始化本地代碼倉
git add . # 添加本地代碼
git commit -m "add local source" # 提交本地代碼
git pull origin master # 下載遠程代碼
git merge master # 合并master分支
git push -u origin master # 上傳代碼
```
