1 常用
[](javascript:void(0); "復(fù)制代碼")
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> git pull origin master # 下載代碼及快速合并
git fetch origin # 從遠(yuǎn)程庫(kù)獲取代碼
git checkout master # 切換到master分支
git commit -m "first version" # 提交
git log # 查看提交歷史
git config core.ignorecase false # 設(shè)置大小寫敏感
git config --global user.email "YOUR EMAIL ADDRESS" # 設(shè)置郵箱</pre>
](javascript:void(0); "復(fù)制代碼")
2 別名Alias
[](javascript:void(0); "復(fù)制代碼")
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> git config --global alias.co="checkout" # 切換分支
git config --global alias.cm="commit -m" # 提交
git config --global alias.pullm="pull 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" # 復(fù)雜顯示</pre>
](javascript:void(0); "復(fù)制代碼")
3 創(chuàng)建版本庫(kù)
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> git init # 初始化本地版本庫(kù)</pre>
4 修改和提交
[](javascript:void(0); "復(fù)制代碼")
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> git diff # 查看變更內(nèi)容
git add <file> # 跟蹤指定的文件
git rm <file> # 刪除文件
git commit -m “commit message” # 提交所有更新過的文件
$ git commit --amend # 修改最后一次提交</pre>
](javascript:void(0); "復(fù)制代碼")
5 查看提交歷史
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> git log -p <file> # 查看指定文件的提交歷史
$ git blame <file> # 以列表方式查看指定文件的提交歷史</pre>
6 撤消
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> git reset --hard <version> # 撤銷到某個(gè)特定版本
git checkout -- <file> # 同上一個(gè)命令
$ git revert <commit> # 撤消指定的提交</pre>
7 分支與標(biāo)簽
[](javascript:void(0); "復(fù)制代碼")
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> git checkout <branch/tag> # 切換到指定分支或標(biāo)簽
git branch -d <branch> # 刪除本地分支
git tag <tagname> # 基于最新提交創(chuàng)建標(biāo)簽
git tag -d <tagname> # 刪除標(biāo)簽
git cherry-pick 62ecb3</pre>
](javascript:void(0); "復(fù)制代碼")
8 合并與衍合
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> git merge --abort # 取消當(dāng)前合并芹彬,重建合并前狀態(tài)
git rebase <branch> # 衍合指定分支到當(dāng)前分支</pre>
9 遠(yuǎn)程操作
[](javascript:void(0); "復(fù)制代碼")
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> git remote show <remote> # 查看指定遠(yuǎn)程版本庫(kù)信息
git remote remove <remote> # 刪除指定的遠(yuǎn)程版本庫(kù)
git pull <remote> <branch> # 下載代碼及快速合并
git push <remote> :<branch/tag-name> # 刪除遠(yuǎn)程分支或標(biāo)簽
$ git push --tags # 上傳所有標(biāo)簽</pre>
](javascript:void(0); "復(fù)制代碼")
10 打包
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> git archive --format=zip --output ../v1.2.zip v1.2 # 打包v1.2標(biāo)簽的文件囊骤,保存在上一級(jí)目錄v1.2.zip文件中
$ git archive --format=zip v1.2 > ../v1.2.zip # 作用同上一條命令</pre>
git打包命令會(huì)自動(dòng)忽略.gitignore
中指定的目錄和文件貌矿,以及.git
目錄示括。
11 全局和局部配置
- 全局配置保存在:
$Home/.gitconfig
- 本地倉(cāng)庫(kù)配置保存在:
.git/config
12 遠(yuǎn)程與本地合并
如果在遠(yuǎn)程創(chuàng)建了代碼倉(cāng)秘通,而且已經(jīng)初始化为严,本地是具體的源代碼,那么工作流程應(yīng)該是:
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> git add . # 添加本地代碼
git pull origin master # 下載遠(yuǎn)程代碼
git push -u origin master # 上傳代碼</pre>