Git是目前世界上最先進(jìn)的分布式版本控制系統(tǒng)(沒(méi)有之一)。在日常開(kāi)發(fā)中使用到的git操作命令挺多的,在此記錄一下,以備忘卻之需。

Git Shell
一、常用操作命令
- 查看修改內(nèi)容
git diff test.txt
- 查看提交的版本和提交ID
git log
# 以版本號(hào)的形式查看當(dāng)前提交版本
git log --pretty=oneline
# 查看簡(jiǎn)寫(xiě)的提交記錄
git log --oneline
# 查看提交后的版本ID
git reflog
# 查看分支合并情況
git log --graph --pretty=oneline --abbrev-commit
- 版本回退及撤銷(xiāo)提交、修改
# 返回上一版本
git reset --hard HEAD^
# 滾動(dòng)到指定ID版本
git reset --hard [ID]
# 未 git add 撤銷(xiāo)修改操作
git checkout -- test.js # 撤銷(xiāo)指定文件的修改
git checkout . # 撤銷(xiāo)全部修改
# 已 git add 撤銷(xiāo)
git reset HEAD demo.txt
git checkout -- demo.txt
- 刪除分支
# 刪除本地分支
git branch -D dev
# 刪除遠(yuǎn)程分支
push origin --delete dev
- 合并分支
# dev 分支合并到 master 分支
git merge --no-ff -m "commit description" dev
- 查看分支
# 查看當(dāng)前分支
git branch
# 查看所有分支
git branch -a
- tag操作
# 查看所有 tag
git tag
# 打 tag
git tag v1.0.0
# 顯示指定版本詳細(xì)信息
git show v1.0.0
# 為指定 tag 添加文字說(shuō)明
git tag -a v0.3 -m "desc" ID
# 刪除本地 tag
git tag -d v1.0.0
# 推送tag至遠(yuǎn)程庫(kù)
git push origin v1.0.0
# 一次性推送全部本地tab
git push origin --tags
# 刪除遠(yuǎn)程的 tag
git push origin :refs/tags/v1.0.0
- 遠(yuǎn)程倉(cāng)庫(kù)信息
git remote -v
- 關(guān)聯(lián)遠(yuǎn)程倉(cāng)庫(kù)
# 刪除 origin
git remote rm origin
# 關(guān)聯(lián)遠(yuǎn)程
git remote add origin git@gitee.xxx.git
- 推送至遠(yuǎn)程倉(cāng)庫(kù)
git push origin master
# 把本地庫(kù)所有內(nèi)容推送到遠(yuǎn)程庫(kù)
git push -u origin master
- 修改倉(cāng)庫(kù)地址
git remote set-url origin git@gitlab.xxx.git
- 推送本地所有分支記錄到遠(yuǎn)程
git push origin --all
- 清除已經(jīng)刪除的遠(yuǎn)程分支記錄
git remote prune origin
- 創(chuàng)建SSH KEY
ssh-keygen -t rsa -C "yourName@163.com"
二、進(jìn)階用法
場(chǎng)景:
當(dāng)你開(kāi)發(fā)一個(gè)需求,你的工作進(jìn)行到一半,這時(shí)有一個(gè)緊急的線(xiàn)上 bug 需要修復(fù)。但是又不想使用 git commit 來(lái)保存當(dāng)前的代碼,怎么辦?
Git 還提供了一個(gè) stash 功能,可以把當(dāng)前工作現(xiàn)場(chǎng) 儲(chǔ)藏 起來(lái),等以后恢復(fù)現(xiàn)場(chǎng)后繼續(xù)工作。
相關(guān)命令:
- 儲(chǔ)藏代碼
git stash
# 加上描述信息
git stash save "xxx"
- 查看緩存的列表
git stash list
- 查看詳情
git stash show [名]
# 例
git stash show stash@{0}
# 看詳細(xì)差異
git stash show stash@{0} -p
- 恢復(fù)儲(chǔ)藏的代碼
# 恢復(fù)后,stash 內(nèi)容并不刪除
git stash apply
# 恢復(fù)指定記錄
git stash apply stash@{0}
# 恢復(fù)最新儲(chǔ)藏的內(nèi)容后,并刪除堆中的記錄
git stash pop
# 恢復(fù)指定記錄,并刪除
git stash pop stash@{1}
- 刪除緩存記錄
# 清除單條記錄
git stash drop stash@{0}
# 清除所有
git stash clear
- 基于緩存創(chuàng)建分支
# 語(yǔ)法
git stash branch <branchname> [<stash>]
# 基于最新緩存創(chuàng)建分支
git stash branch new-branch
# 等價(jià)于
git checkout -b new-branch
git stash apply
# 基于指定緩存創(chuàng)建分支
git stash branch new-branch stash@{2}
Git 相關(guān)系列
歡迎訪(fǎng)問(wèn):天問(wèn)博客