git常用命令彙總

2021-08-01 11:22:15 字數 4379 閱讀 6348

git是由linux kernel創立者 linux torvalds 開發的(因為bitkeeper的問題),屬於dvcs(分布式版本控制)

github 是乙個開源專案社群,始於2007(08上線),github 裡面的專案可以通過標準的 git 命令進行訪問和操作

pro git v2書本傳送門

而我這篇博文只是介紹一些常用的命令及技巧心得

git config --global user.name crperlin  #git的使用者名稱

git config --global user.email [email protected] #git的登入賬號

git config --global core.editor vim #設定預設編輯器

git config --global merge.tool vimdiff #設定預設的對比檔案差異工具

git config --global color.status auto #顯示顏色資訊

git config --global color.branch auto #顯示顏色資訊

git config --global color.interactive auto #顯示顏色資訊

git config --global color.diff auto #顯示顏色資訊

git config --global push.default ****** #簡化提交

git config --list#檢視配置的資訊

git config --global http.sslverify false 跳過ssl認證

git help config#獲取幫助資訊

配置這貨的好處就是可以省去提交的時候每次都輸入賬號密碼;減少重複工作!

不管是github還是一些基於gitlab的託管社群,配置這個都是大同小異,在個人賬戶那裡找到ssh-keygen

ssh-keygen -t rsa -c [email protected] #生成金鑰,也可以通過圖形工具生成,看個人喜好

ssh -t [email protected] #測試鏈結github是否成功,其他社群網域名稱不同罷了

git init    #初始化

git status #獲取狀態,很實用的乙個功能,對檔案做了操作都能看到各種提示資訊

git add # . 或 * 代表全部新增

git commit -m "注釋文字"

#新增注釋

git remote add origin [email protected]:crperlin/s-next.git #本地鏈結遠端倉庫

git push -u origin master #推送到主分支

一般的git託管社群都提供兩種,一種是基於https,一種是基於ssh

前者需要賬號密碼提交,後者可以配置ssh-keygen

git clone [email protected]:crperlin/s-next.git    #轉殖到本地,倉庫名就是資料夾的名字

git clone xx.git 《自定義資料夾名字》 #不需要帶括號,只是區別

git add *#跟蹤新檔案

git rm -f * #強制刪除所有檔案

git rm --cached * #取消跟蹤

git mv file_from file_to #重新命名跟蹤檔案,與linux一致

git log#檢視提交記錄

git commit#提交更新

git commit -m "注釋文字"

#新增注釋

git commit -a

#跳過使用暫存區域,把所有已經跟蹤過的檔案暫存起來一併提交

git commit --amend #修改最後一次提交

git reset head *#取消已經暫存的檔案

git checkout -- file#取消對檔案的修改(從暫存區去除file)

git checkout branch|tag|commit -- file_name#從倉庫取出file覆蓋當前分支

git checkout -- .#從暫存區去除檔案覆蓋工作區

git branch  #列出本地分支

git branch -r #列出遠端分支

git branch -a

#列出所有分支

git branch -v #檢視各個分支最後乙個提交物件的資訊

git branch --merge #檢視已經合併到當前分支的分支

git branch --no-merge #檢視為合併到當前分支的分支

git branch test #新建test分支

git checkout test #切換到test分支

git checkout -b test #新建+切換到test分支

git checkout -b test dev#基於dev新建test分支,並切換

git branch -d test #刪除test分支

git branch -d test #強制刪除test分支

git merge test #將test分支合併到當前分支

git rebase master #將master分之上超前的提交,變基到當前分支

git fetch origin branch #獲取遠端上指定分支

git merge origin branch #合併遠端上指定分支

git push origin branch #推送到遠端上指定分支

git push origin localbranch:serverbranch #推送到遠端上指定分支

git checkout -b test origin/dev #基於遠端dev新建test分支

git push origin :server#刪除遠端分支[推送空分支,目前等同於刪除]

git tag#列出現有標籤  

git tag v1.0.0

#新建標籤

git tag -a v0.1 -m '注釋文字'

#新建帶注釋標籤

git tag v2.0

9fceb02 #給指定的指向新增版本

git show #顯示指定版本的詳細資訊

git checkout tagname#切換到標籤

git push origin v1.5

#推送分支到源上

git push origin --tags#一次性推送所有分支

git tag -d v0.1

#刪除標籤

git push origin :refs/tags/v0.1

#刪除遠端標籤

一. 到配置檔案配置,最傻瓜化的寫法,等值賦予

[alias]

logs = log --color --graph --pretty=format:'%cred%h%creset -%c(yellow)%d%creset %s %cgreen(%cr) %c(bold blue)<%an>%creset' --abbrev-commit

st = status

ci = commit

br = branch

co = checkout

df = diff

lg = log -p

二. 命令列追加

git config --global alias.co checkout

git config --global alias.ci commit

git config --global alias.br branch

...

git grep '查詢文字'

#對全域性的字串查詢

git grep '查詢文字' v1.0.0

#針對版本的字串查詢

git blame #誰,在什麼時間,修改了檔案的什麼內容
#放棄工作目錄下的所有修改:

git reset --hard head

#移除快取區的所有檔案(i.e. 撤銷上次git add):

git reset head

#放棄某個檔案的所有本地修改:

git checkout head #重置乙個提交(通過建立乙個截然不同的新提交)

git revert #將head重置到指定的版本,並拋棄該版本之後的所有修改:

git reset --keep

git常用命令彙總

針對常用的git命令,進行彙總記錄,以便及時查詢 可以定乙個指令碼,檢查語法錯誤的,或是借用第三方工作進行檢查,然後將該指令碼放到專案的 git hook pre commit 位置,也可以放到乙個位置軟鏈到多個專案 git hook pre commit 中,這時,在提交時,會檢查語法規範,提高 ...

git 常用命令彙總

1 獲取git的版本 git version 2 初始化倉庫 git init 初始化乙個本地倉庫 git version 獲取git的版本 git config global user.name yourname git config global user.email youremail 第3 ...

git常用命令彙總

1.git add 新增到檔案到版本庫,也可以新增檔案 git add 或者 git add 2.git reset head 取消新增到版本庫 3.git commit m commit message 提交修改,當版本庫內容有修改時才可以修改 即使用了git add命令 提交可選的額外資訊 使用...