一、追溯,查看提交歷史。
git shortlog #查看每個人提交的commit 備註,以及次數。
git log -graph
好用的追責工具git blame。
它可以查看一個文件的每一行的提交記錄,想知道這個文件的代碼裏面每一行是誰提交的都可以查得到。
git blame 【filename】 #顯示文件的每一行的提交者。
#假如說現在有一個需求,想查看代碼的第6行到第11行是誰提交的:
git blame [filename] -L [6],+[5]
二、在git倉庫中搜索。
git grep #在git倉庫中以關鍵字的形式搜索文件,用法和linux系統命令的grep是一樣的,下面是用法示例:
git grep [hellotest] #查找在git倉庫中所有含有hello字符串的文件。
git grep -n [hellotest] #顯示關鍵字所在文件的行號。
git grep -name-only [hellotest] # 只顯示文件名。
git grep -c [hellotest] #查看每個文件有多少個匹配。
and / or:
git grep -e [hello] --and -e [world]
git grep -e [hello] --or -e [world]
在指定tag中搜索:
git grep string [tag]
三、修改提交。
增補提交。
最後一次提交如果有問題,需要修改最後一次的提交,而不是從重新commit產生一個新的提交的話,就可以用--amend參數。
git commit --amend #不會生成新的commit,只修改最後一個commit。
撤銷提交。
git revert [SHA1]#將原來的提交撤銷。
四、版本回退。
git reset --mixed [SHA1] #將版本回退到工作區。
git reset --soft [SHA1] #將版本回退到暫存區。
git reset --hard [SHA] #回退後直接刪除提交。
五、分支相關。
git branch [branchname] #創建一個分支,默認從當前節點下創建一個分支。
git branch [branchname][SHA1] #如果想在指定節點下創建一個分支,那麼後面只要跟上指定的 commit的SHA1值就可以了。
git branch -b [new_branchname] [old_branchname] #在某個分支下再新建一個分支。
git branch -a #查看所有分支。
git branch --merged #查看已合併分支。
git branch --no-merged #查看未合併分支。
注意!!在切換分支的時候,需要保證工作區和暫存區的清潔!!
git checkout [branchname]
git branch -m [oldbranch] [newbranch] #分支重命名。
git branch -d [branchname] #已合併的分支直接刪除。
git branch -D [branchname] #強制刪除未合併的分支。
分支合併:
git checkout [brachname] #首先切換到要合併到的分支。(目的地)
git merge [brachname] #將指定分支合併到當前分支。
----------------------------------------------------------未完待續