經(jīng)典Git操作與對(duì)應(yīng)區(qū)域圖
Git庫(kù)創(chuàng)建
// 遠(yuǎn)程倉(cāng)庫(kù)克隆到本地
git clone [ssh]
// 本地構(gòu)建倉(cāng)庫(kù)
git init
Git配置
以MAC系統(tǒng)抗碰,Git配置文件一般有兩個(gè)配置文件,其作用域分別為全局級(jí)茂缚、倉(cāng)庫(kù)級(jí)
- 全局級(jí)配置: ~/.gitconfig
- 倉(cāng)庫(kù)級(jí)配置: ${ProjectFile}/.git/.gitconfig
// 查看當(dāng)前配置信息
git config -l
// 設(shè)置git用戶信息
git config [--local | --global] user.name "[name]"
git config [--local | --global] user.email "[email address]"
Git分支相關(guān)
查看
// 列出所有本地分支
git branch
// 列出所有遠(yuǎn)程分支
git branch -r
// 列出所有本地分支和遠(yuǎn)程分支
git branch -a
// 列出本地分支及其對(duì)應(yīng)的遠(yuǎn)端分支,并附最新commit信息 (-v )
git branch -vv
新建
// 新建一個(gè)分支,但依然停留在當(dāng)前分支
git branch [branch-name]
// 新建一個(gè)分支嵌戈,指向指定commit
git branch [branch] [commit]
// 如果本地有branch-name分支,則切換到該分支听皿,如果沒(méi)有切遠(yuǎn)程有branch-name分支熟呛,則直接以遠(yuǎn)程分支為基準(zhǔn)創(chuàng)建branch-name本地分支
git checkout [branch-name]
// 新建一個(gè)分支,并切換到該分支
git checkout -b [branch-name]
// 執(zhí)行本地分支 branch 追蹤遠(yuǎn)端 remote-branch
git branch --set-upstream [branch] [remote-branch]
同步&合并
// 同步遠(yuǎn)端變到本地
git fetch
// 同步遠(yuǎn)端變更尉姨,并與本地分支合并庵朝,可能有conflict
git pull
// 合并N個(gè)提交記錄為一個(gè)
git rebase -i HEAD~N
// 場(chǎng)景:個(gè)人分支 rebase 其它分支 ,(分支commit記錄更清爽又厉,相比merge操作少一個(gè)Merge commit)
git rebase [branch]
// 合并指定分支到當(dāng)前分支
git merge [branch]
推送
// 上傳當(dāng)前分支到跟蹤的遠(yuǎn)端分支
git push
// 上傳本地指定分支到遠(yuǎn)程倉(cāng)庫(kù)
git push [remote] [branch]
// 強(qiáng)行推送當(dāng)前分支到遠(yuǎn)程倉(cāng)庫(kù)九府,即使有沖突
git push [remote] --force
刪除
// 刪除本地分支
git branch -d [branch-name]
// 刪除遠(yuǎn)端分支
git push origin --delete [branch-name]
Git提交相關(guān)
查看
// 顯示當(dāng)前分支的提交歷史
git log
// 顯示當(dāng)前分支的最近幾次提交
git reflog
提交
// 提交暫存區(qū)到本地倉(cāng)庫(kù)區(qū)
git commit
// 提交快捷方式 帶message
git commit -m [message]
// 提交到上一次commit,可變更message
git commit --amend -m [message]
// 將某個(gè)commit覆致,合并到當(dāng)前分支
git cherry-pick [commit]
撤銷
// 重置暫存區(qū)的指定文件侄旬,與上一次commit保持一致,但工作區(qū)不變
git reset [file]
// 重置暫存區(qū)與工作區(qū)煌妈,與上一次commit保持一致
git reset --hard
// 重置當(dāng)前分支的指針為指定commit儡羔,同時(shí)重置暫存區(qū),但工作區(qū)不變
git reset [commit]
// 重置當(dāng)前分支的HEAD為指定commit璧诵,同時(shí)重置暫存區(qū)和工作區(qū)汰蜘,與指定commit一致
git reset --hard [commit]
// 新建一個(gè)提交,并對(duì)指定commit后的所有變更進(jìn)行回滾
git revert [commit]
Git文件相關(guān)
查看
// 顯示變更的文件
git status
// 顯示暫存區(qū)和工作區(qū)的差異
git diff
操作
// 添加指定文件到工作區(qū)
git add [file]
// 添加所有文件到工作區(qū)
git add .
// 移除工作區(qū)指定文件
git rm [file]
撤銷
// 恢復(fù)暫存區(qū)的指定文件到工作區(qū)
git checkout [file]
// 恢復(fù)暫存區(qū)的所有文件到工作區(qū)
git checkout .
// 將所有工作區(qū)文件 存儲(chǔ)到stash區(qū)
git stash
// 將stash 存儲(chǔ)區(qū)最上面的一個(gè)之宿,恢復(fù)到工作區(qū)
git stash pop
Git標(biāo)簽
// 列出所有的標(biāo)簽
git tag
// 新建一個(gè)tag
git tag [tag]
// 指定commit傻姑娘新建一個(gè)tag
git tag [tag] [commit]
// 刪除本地tag
git tag -d [tag]
// 刪除遠(yuǎn)程tag
git push origin :refs/tags/[tagName]
// 推送指定tag
git push [remote] [tag]
// 以指定某個(gè)分支的tag處新建一個(gè)分支
git checkout -b [branch][tag]