Git常用操作命令

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

Git Shell

一、常用操作命令

  1. 查看修改內(nèi)容
git diff test.txt
  1. 查看提交的版本和提交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
  1. 版本回退及撤銷(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
  1. 刪除分支
# 刪除本地分支
git branch -D dev

# 刪除遠(yuǎn)程分支
push origin --delete dev
  1. 合并分支
# dev 分支合并到 master 分支
git merge --no-ff -m "commit description" dev
  1. 查看分支
# 查看當(dāng)前分支
git branch

# 查看所有分支
git branch -a
  1. 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
  1. 遠(yuǎn)程倉(cāng)庫(kù)信息
git remote -v
  1. 關(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
  1. 推送至遠(yuǎn)程倉(cāng)庫(kù)
git push origin master

# 把本地庫(kù)所有內(nèi)容推送到遠(yuǎn)程庫(kù)
git push -u origin master
  1. 修改倉(cāng)庫(kù)地址
git remote set-url origin git@gitlab.xxx.git
  1. 推送本地所有分支記錄到遠(yuǎn)程
git push origin --all
  1. 清除已經(jīng)刪除的遠(yuǎn)程分支記錄
git remote prune origin
  1. 創(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)命令:

  1. 儲(chǔ)藏代碼
git stash

# 加上描述信息
git stash save "xxx"
  1. 查看緩存的列表
git stash list
  1. 查看詳情
git stash show [名]

# 例
git stash show stash@{0}

# 看詳細(xì)差異
git stash show stash@{0} -p
  1. 恢復(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}
  1. 刪除緩存記錄
# 清除單條記錄
git stash drop stash@{0}

# 清除所有
git stash clear
  1. 基于緩存創(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)博客

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 公司一直在使用svn進(jìn)行開(kāi)發(fā),今天得閑又看了看Git管理代碼的常用操作,總結(jié)一下。 Git安裝 查看是否安裝了gi...
    Supremodeamor閱讀 348評(píng)論 0 1
  • 一、在Windows上安裝Git 1. 從Git官網(wǎng)直接下載安裝程序,然后按默認(rèn)選項(xiàng)安裝即可,安裝完后,在開(kāi)始菜單...
    dingFY閱讀 254評(píng)論 0 1
  • 安裝Git 官網(wǎng)安裝指導(dǎo)地址 MAC下使用HomeBrew安裝 如果沒(méi)有HomeBrew,在終端輸入以下命令嘗試安...
    小白進(jìn)城閱讀 410評(píng)論 0 3
  • 1)遠(yuǎn)程倉(cāng)庫(kù)相關(guān)命令 檢出倉(cāng)庫(kù):$ git clone git://github.com/jquery/jquer...
    Jeff_Tsui閱讀 381評(píng)論 0 0
  • 圖中名詞解釋 workspace: 工作區(qū) Index: 暫存區(qū) Repository: 本地倉(cāng)庫(kù) Remote:...
    lmmy123閱讀 250評(píng)論 0 0

友情鏈接更多精彩內(nèi)容