配置信息
查看用戶名和郵箱地址:
git config user.name
git config user.email
修改用戶名和郵箱地址
git config --global user.name "xxxx"
git config --global user.email "xxxx"
生成密鑰
ssh-keygen -t rsa -C "email"
按3個回車,密碼為空。
基礎(chǔ)操作
| 命令 | 說明 |
|---|---|
| git diff | 查看修改信息 |
| git diff filePath | 查看文件變動信息 |
| git add . | 添加所有修改 |
| git checkout filename | 撤銷此文件的修改 |
| git checkout . | 撤銷所有未提交的修改 |
| git commit -m "提交信息" | 提交代碼 |
| git commit | 進入vim界面添加提交信息(比較詳細) |
| git push | 上傳提交到服務(wù)器 |
log操作
| 命令 | 說明 |
|---|---|
| git log | 查看提交歷史記錄 |
| git log --oneline 或者 git log --pretty=oneline | 以精簡模式顯示 |
| git log --stat | 顯示文件更改列表 |
| git log --author= 'name' | 顯示某個作者的日志 |
| git log -p filepath | 查看某個文件的詳細修改 |
| git log -L start,end:filepath | 查看某個文件某幾行范圍內(nèi)的修改記錄 |
| git log --stat commitId 或者 git show --stat commitId | 查看某一次提交的文件修改列表 |
tag相關(guān)命令
| 命令 | 說明 |
|---|---|
| git tag | 列出所有的tag |
| git tag name | 打輕量標簽 name |
| git tag -d name | 刪除本地的tag |
| git push origin --delete tag name | 刪除遠程的tag |
| git show name | 查看tag信息 |
| git push origin name | 將tag提交到遠程 |
隱藏與恢復(fù)
| 命令 | 說明 |
|---|---|
| git stash | 把當前的工作隱藏起來 等以后恢復(fù)現(xiàn)場后繼續(xù)工作 |
| git stash list | 查看所有被隱藏的文件列表 |
| git stash apply | 恢復(fù)被隱藏的文件,但是內(nèi)容不刪除 |
| git stash drop | 刪除文件 |
| git stash pop | 恢復(fù)文件的同時 也刪除文件 |
| git stash clear | 刪除所有保存的信息 |
分支操作
| 命令 | 說明 |
|---|---|
| git branch | 查看本地分支 |
| git branch -a | 查看本地和遠程分支 |
| git branch -vv | 列出本地分支和對應(yīng)的遠程分支及最新一條提交日志 |
| git checkout -b branch_name | 添加新分支 |
| git checkout branch_name | 切換本地分支 |
| git branch -d branch_name | 刪除本地分支,如果有為合并代碼則刪除失敗 |
| git branch -D branch_name | 強制刪除本地分支 |
| git push origin --delete branch_name | 刪除遠程分支 |
倉庫操作
| 命令 | 說明 |
|---|---|
| git remote -v | 遠程倉庫路徑查詢 |
| git remote add origin(本地版本庫) url(遠程倉庫地址) | 添加遠程倉庫 |
| git remote rm origin(遠程倉庫名) | 刪除遠程倉庫 |
| git remote rename old_name new_name | 修改倉庫名 |
| git remote add origin url(遠程倉庫地址) git push -u origin master |
上傳到服務(wù)器 |
搜索
| 命令 | 說明 |
|---|---|
| git log --grep="commit提交的關(guān)鍵字" | 搜索相關(guān)信息列表 |
reset 命令
| 命令 | 說明 |
|---|---|
| git reset HEAD^ | 回退所有內(nèi)容到上一個版本 |
| git reset HEAD^ filename | 回退 filename 文件的版本到上一個版本 |
| git reset commitId | 回退到指定版本 |
| git reset --soft HEAD | --soft 參數(shù)用于回退到某個版本 |
| git reset --hard HEAD | --hard 參數(shù)撤銷工作區(qū)中所有未提交的修改內(nèi)容,并刪除之前的所有信息提交 |
補丁操作
| 命令 | 說明 |
|---|---|
| git diff > 0001.patch | 針對已修改的文件生成patch |
| git diff Test.java > test.patch | 針對Test.java的修改信息生成patch |
| git format-patch HEAD^ 或 -1 | 生成最近的1次commit的patch |
| git format-patch commitId1...commitId2 | 生成兩個commit間的修改的patch(生成的patch不包含commitId1) |
| git format-patch -1 commitId | 生成單個commit的patch |
| git format-patch commitId | 生成從commitId以來的修改patch(不包含該commitId) |
| git format-patch --root commitId | 生成從根到commitId提交的所有patch |
| git apply --stat 0001-.patch | 查看patch的情況 |
| git apply --check 0001-.patch | 檢查patch是否能夠打上,如果沒有任何輸出,則說明無沖突,可以打上 |
| git apply xxx.patch | 打補丁到項目中 |
| git apply --reject xxx.patch | 強制打補丁 |
統(tǒng)計
查看個人代碼量
git log --author="username" --pretty=tformat: --numstat | awk '{ add +=2; loc +=
2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -
統(tǒng)計個人某個時間段提交的代碼量
git log --author="name" --since=2022-01-01 --until=2022-02-01 --pretty=tformat: --numstat | awk '{ add +=2; loc +=
2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'
統(tǒng)計每個人增刪行數(shù)
git log --format='%aN' | sort -u | while read name; do echo -en "name" --pretty=tformat: --numstat | awk '{ add +=
2; loc +=
2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done
提交統(tǒng)計
git log --oneline | wc -l提交者排名前3
git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 3