Git的配置文件
gitconfig:對(duì) git 行為進(jìn)行配置
項(xiàng)目配置優(yōu)先級(jí)高于全局配置
.gitignore:指定文件忽略規(guī)則
查看配置信息:git config –list
配置個(gè)人信息
# 配置郵箱(當(dāng)前項(xiàng)目)git config user.email# 配置用戶名(當(dāng)前項(xiàng)目)git config user.name# 配置郵箱(全局)git config –global user.email# 配置用戶名(全局)git config –global user.name# 配置郵箱(系統(tǒng))git config –system user.email# 配置用戶名(系統(tǒng))git config –system user.name
設(shè)置快捷鍵
修改 .gitconfig 文件
[alias] di = diff dis = diff –staged dic = diff HEAD^ HEAD dit = difftool –no-prompt –extcmd “icdiff” co = checkout br = branch ci = commit st = status pl = pull lg = log –graph –pretty=format:’%Cred%h%Creset -%C(yellow)%d%Creset %C(bold blue)%s%Creset %Cgreen(%cr) %Creset’ –abbrev-commit –date=relative lgs = log –stat –graph –pretty=format:’%Cred%h%Creset -%C(yellow)%d%Creset %C(bold blue)%s%Creset %Cgreen(%cr) %Creset’ –abbrev-commit –date=relative lga = log –all last = log -1[icdiff] options = –highlight –line-numbers –no-bold
查看提交記錄
# 查看過往提交記錄git log# 查看每次提交具體改動(dòng)的文件git log —stat# 查看文件的具體改動(dòng)git log -p# 展示所有分支的改動(dòng)git log -a# 查看最近1次提交git log -1# 查看最近5次提交git log -5# 查看改動(dòng)內(nèi)容包含patten的部分git log —G patten# 查看工作區(qū)的變動(dòng)git diff# 查看暫存區(qū)的變動(dòng)git diff –staged# 查看最近一次提交的變動(dòng)git diff HEAD^ HEAD# 查看內(nèi)容包含patten部分的改動(dòng)git grep –break –heading -n patten# -L 表示行內(nèi)查找git grep -L (line_begin,line_end):filepath patten
分支操作
# 顯示所有本地分支,* 所在的位置,表示當(dāng)前分支git branch# 創(chuàng)建新分支git branch new_branch# 切換分支git checkout branch_name# 創(chuàng)建新分支并切換到新分支git checkout -b branch_name# 遠(yuǎn)程檢出新分支(遠(yuǎn)程和本地創(chuàng)建一個(gè)叫feature的分支)git checkout –track origin/feature# 拉取暫存區(qū)文件并將其替換成工作區(qū)文件git checkout —- file_name# 刪除分支git branch -d branch_name# 強(qiáng)行刪除分支git branch -D branch_name# 打tag版本git tag tag_name# 刪除taggit tag -d tag_name
代碼修改
# 儲(chǔ)藏當(dāng)前未提交的改動(dòng)git stash# untracked 的文件也儲(chǔ)藏git stash -u# 復(fù)原儲(chǔ)藏(文件都復(fù)原成未暫存狀態(tài))git stash pop# 復(fù)原儲(chǔ)藏(已暫存的文件,還是恢復(fù)為暫存的狀態(tài))git stash pop -—index# 讓歷史區(qū)與指定的提交保持一致,可以理解為撤銷 git commitgit reset –soft# 讓暫存區(qū)和歷史區(qū)與指定的提交保持一致,可以理解為撤銷 git addgit reset or git reset –mixed# 讓工作區(qū)、暫存區(qū)和歷史區(qū)都與指定的提交保持一致,可以理解為撤銷所有改動(dòng),這是一個(gè)不可挽回的操作,請(qǐng)謹(jǐn)慎執(zhí)行g(shù)it reset –hard
代碼同步
# 將遠(yuǎn)程倉(cāng)庫(kù)的代碼、 分支和 tag 都下載到本地。但不會(huì)改變本地代碼git fetch# 變基,改變某次提交的父提交(假設(shè)你的父分支是 base_branch)git rebase base_branch# 把 base_branch 的代碼合并到你當(dāng)前的分支git merge base_branch# 舉個(gè)例子,你目前的開發(fā)分支是 branch_a,branch_a 是從上午10:30的master分支檢出的# 上午10:45,你的同事對(duì)master分支進(jìn)行了提交# 你有兩種方式去拉取代碼:#(1)git rebase(branch_a): git rebase master# ==> 此時(shí)你的 branch_a 分支的代碼的父提交變成了從上午10:45的提交中檢出的,你在你的 branch_a 中 git log 將看不到你同事的提交記錄,保持分支整潔#(2)git merge(branch_a): git merge master# ==> 此時(shí)你的 branch_a 分支代碼把最新的master代碼合并到了你的代碼中,你在你的 branch_a 中 git log 可以看到你同事的提交記錄,但這樣也會(huì)擾亂你的提交記錄,讓你看著很多很亂
**git rebase ** 還是 git merge ?:
git pull
git pull branch_name 從 branch_name 分支,拉取最新的提交并合并分支,等價(jià)于git fetch + git merge
git commit
git commit -m 提交信息 將暫存區(qū)的代碼提交到歷史區(qū)
git commit –all -m 提交信息 等價(jià)于git add . && git commit -m 提交信息
git push
git push 把本地歷史區(qū)的代碼提交到遠(yuǎn)程倉(cāng)庫(kù)分支
解決沖突
在 rebase 或者 merge 之前,務(wù)必確保工作目錄是干凈的
查看文件沖突的方法
#(1)git diff –cc file# ++<<<<<<<<<>>>>>>>>> conflict_branch
放棄解決沖突
git rebase –abortgit merge –abort
解決沖突
# –ours:以我們?yōu)闇?zhǔn), –theirs:以他們?yōu)闇?zhǔn)git checkout –ours conflict_filegit add conflict_filegit commit# 注意不用加 -m 選項(xiàng),git 會(huì)默認(rèn)生成一個(gè) merge 的 message
撤銷合并
git reset –hard HEAD~ 回到上一次提交
git reflog
git reflog git 命令進(jìn)行操作的日志