基本業(yè)務(wù)場(chǎng)景處理
1. 使用git查看比較兩次提交的差異沪曙,僅查看變化的文件:
git diff code1 code2 --name-only
2. 使用git比較兩次提交的差異:
git diff code1 code2
以上命令即將列出所有的文件及改動(dòng)部分纯丸;注意比較次序辛臊,是code2相對(duì)于code1的變動(dòng)娱据,即code2被認(rèn)為是最近提交;
同時(shí)如果想以當(dāng)前提交作為起點(diǎn)進(jìn)行推算膏斤,那么可以借助HEAD指針:
git diff HEAD~1 HEAD~0 --name-only
以上命令表示列出上上次提交和最近一次提交變動(dòng)的文件列表徐绑;
3. 使用git提交到遠(yuǎn)程分支
git push origin local_branch_name:remote_branch_name
其中 local_branch_name即為本地當(dāng)前的提交分支名稱; remote_branch_name即為將提交至的遠(yuǎn)程分支名稱掸绞;
當(dāng)然如果已經(jīng)建立了本地分支和遠(yuǎn)程分支的追蹤關(guān)系泵三,那么直接可以省掉后面的分支信息:
git push origin
4. 建立本地分支與遠(yuǎn)程分支的追蹤關(guān)系
為了便于分支管理和提交耕捞,最好的做法是在本地創(chuàng)建分支時(shí)即創(chuàng)建本地分支與遠(yuǎn)程分支的追蹤關(guān)系,并且為了方便烫幕,最好是使用相同的分支名稱:
git checkout -b local_branch_name origin/remote_branch_name
通過這種方式俺抽,就可以直接在本地創(chuàng)建一個(gè)與遠(yuǎn)程分支相對(duì)應(yīng)的本地分支;
5. 顯示當(dāng)前所有本地分支與遠(yuǎn)程分支的追蹤關(guān)系
為了實(shí)時(shí)看到當(dāng)前的分支狀態(tài):本地所有分支较曼、本地分支和遠(yuǎn)程分支的追蹤關(guān)系磷斧、當(dāng)前分支的更新狀態(tài)等;
git remote show origin
6. 本地修改與遠(yuǎn)程代碼合并
同一個(gè)分支當(dāng)前可能同時(shí)由多人在協(xié)同開發(fā)捷犹,因此在提交本地修改時(shí)弛饭,本地代碼可能已經(jīng)過期,需要與遠(yuǎn)程分支上最新代碼合并之后才能提交萍歉;
1. 首先需要保存下我們當(dāng)前本地的修改:
git stash
2.當(dāng)執(zhí)行完以上命令侣颂,當(dāng)前本地將回到修改前的commit,此時(shí)可以繼續(xù)更新遠(yuǎn)程分支上的提交:
git pull origin
此時(shí)枪孩,遠(yuǎn)程分支上的提交就會(huì)被同步下來憔晒,此時(shí)本地代碼已經(jīng)被同步為遠(yuǎn)程分支上的最新代碼;
3.此時(shí)需要將我們之前所做的修改與當(dāng)前庫上最新的代碼進(jìn)行合并
git stash pop
并且會(huì)執(zhí)行自動(dòng)合并蔑舞,如果沒有沖突拒担,那就可以檢查后直接提交;但是如果有沖突攻询,就去合并之后的文件中找到?jīng)_突的地方从撼,全部沖突修改完成之后,再提交即可钧栖;
關(guān)于stash 的用法后面還有更多介紹低零。
基本命令功能
本節(jié)對(duì)各個(gè)基本命令的用法/參數(shù)進(jìn)行詳細(xì)的介紹,深入了解各個(gè)命令的詳細(xì)用法桐经,可以覆蓋更多的業(yè)務(wù)場(chǎng)景毁兆,提升管理效率。
1. git stash
git stash 命令用于暫存當(dāng)前本地工作臨時(shí)狀態(tài)阴挣,用于緊急處理其他任務(wù)/合并遠(yuǎn)程修改進(jìn)行本地提交/本地變更嘗試方案等場(chǎng)景。
//會(huì)將當(dāng)前的工作狀態(tài)進(jìn)行緩存纺腊,默認(rèn)有遞增id
git stash
//在將當(dāng)前的工作狀態(tài)進(jìn)行緩存的同時(shí)畔咧,給本地緩存進(jìn)行備注
git stash save "description"
//查看當(dāng)前已經(jīng)緩存列表
git stash list
//根據(jù)在stash list中的緩存列表id,可以指定恢復(fù)到某一個(gè)工作狀態(tài)
git stash apply stash@{id}
//與apply命令不同揖膜,執(zhí)行pop命令之后誓沸,stash緩存中即會(huì)移除該stash;
git stash pop stash@{id}
//清除所有stash緩存
git stash clear
//移除某個(gè)stash
git stash drop stash@{id}
git stash 支持跨分支的壹粟,但是同時(shí)每個(gè)stash均會(huì)有所在分支及基準(zhǔn)commit記錄拜隧,因此可以清晰知道每個(gè)stash該合并到何處分支的commit宿百。
2. 關(guān)聯(lián)遠(yuǎn)程分支
git remote remove origin
git branch --set-upstream-to origin/develop develop
//或者
git remote add origin git@github.com:git_username/repository_name.git
3. 添加忽略文件
//將文件移除track, git status將不會(huì)看到
git update-index --assume-unchanged <files>
//重新恢復(fù)track
git update-index --no-assume-unchanged <files>
當(dāng)不小心忽略的文件過多,有如下相關(guān)操作:
- 查看當(dāng)前被忽略的文件
git ls-files -v | grep '^h\ '
- 查看所有當(dāng)天被忽略文件的路徑
git ls-files -v | grep '^h\ ' | awk '{print $2}'
- 恢復(fù)所有當(dāng)前被忽略的文件
git ls-files -v | grep '^h' | awk '{print $2}' |xargs git update-index --no-assume-unchanged
4. 統(tǒng)計(jì)代碼修改量
git log --author="username" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -
修改username之后執(zhí)行將會(huì)打雍樘怼:
added lines: 120745, removed lines: 71738, total lines: 49007
統(tǒng)計(jì)所有人的修改量:
//統(tǒng)計(jì)提交貢獻(xiàn)量前5名
git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 5
//查看所有人的提交情況
git log --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done
修改當(dāng)前分支名稱:
git branch -m old_branch_name new_branch_name
5. 配置difftool
安裝beyond compare之后可以設(shè)置為git比較工具垦页。
git查看有哪些對(duì)比工具可以設(shè)置命令:
$ git difftool --tool-help
會(huì)得到如下列表:
'git difftool --tool=<tool>' may be set to one of the following:
vimdiff
vimdiff2
vimdiff3
The following tools are valid, but not currently available:
araxis
bc
bc3
codecompare
deltawalker
diffmerge
diffuse
ecmerge
emerge
examdiff
guiffy
gvimdiff
gvimdiff2
gvimdiff3
kdiff3
kompare
meld
opendiff
p4merge
tkdiff
winmerge
xxdiff
Some of the tools listed above only work in a windowed
environment. If run in a terminal-only session, they will fail.
然后再設(shè)置對(duì)比工具:
$ git config --global diff.tool bc3
$ git config --global difftool.bc3.path "c:/program files/beyond compare 3/bcomp.exe"
如果安裝的是beyond compare 4,那么也同樣設(shè)置為bc3干奢,并指向beyond compare 4的安裝地址痊焊。
或者可以直接修改.gitconfig文件:
[diff]
tool = bc3
[difftool "bc3"]
path = d:/program files/beyond compare 3/bcomp.exe
[merge]
tool = bc3
[mergetool "bc3"]
path = d:/program files/beyond compare 3/bcomp.exe
6. --no-commit
如果在合并某個(gè)分支之前想看一下詳細(xì)的所有變動(dòng)后微調(diào)再提交而不是直接合并生成commit,那么這個(gè)命令就很管用忿峻。
常規(guī)場(chǎng)景:
git merge test_branch
假設(shè)此時(shí)test_branch上有一個(gè)0329c的commit即將合并到master薄啥,如果沒有沖突,執(zhí)行合并完成后會(huì)在master上生成一個(gè)新的commit逛尚。但是如果需要有人幫忙review或者還可能需要微調(diào)后才能合并到master垄惧,那么可以避免直接生成這個(gè)commit:
git merge test_branch --no-commit
git reset
git difftool
這樣不管有沒有沖突,就可以進(jìn)行改動(dòng)的review绰寞,十分方便赘艳。