工作中最常用的git命令

本篇主要匯總工作中常用的一些git指令和面試問題,持續(xù)更新

  1. 初始化倉庫
git init
  1. 克隆一個(gè)項(xiàng)目代碼
git clone [url]
  1. 新建一個(gè)分支,并切換到該分支
git checkout -b [branch]

處理bug時(shí)可以先創(chuàng)建一個(gè)分支,然后add - commit - merge,最后刪除該分支

  1. 查看當(dāng)前分支
git branch

git branch命令會(huì)列出所有分支,當(dāng)前分支前面會(huì)標(biāo)一個(gè)*號(hào)

  master
  system_test
* yunfei
  1. 查看修改的文件
git status 
  1. 添加當(dāng)前目錄的所有文件到暫存區(qū)
git add .
  1. 提交暫存區(qū)到倉庫區(qū)
 git commit -m [message]
  1. 推送前獲取遠(yuǎn)程倉庫更新,并與本地分支合并
git pull [remote] [branch]
  1. 推送本地指定分支到遠(yuǎn)程倉庫
git push origin dbase_bugfix 就可以了

# 推送分支并創(chuàng)建track
git push --set-upstream origin dbase_bugfix
  1. 刪除本地分支
git branch -d [branch-name]

# 強(qiáng)制刪除
git branch -D dev

先切換到別的分支才能刪除

  1. 刪除遠(yuǎn)程分支
git push origin --delete [branch-name]

git branch -dr [remote/branch]
  1. 將文件存至?xí)捍鎱^(qū)
# 保存到緩存區(qū)
git stash save '本次暫存的標(biāo)識(shí)名字'

# 查看暫存區(qū)列表
git stash list

# 查看最近暫存區(qū)保存的內(nèi)容
git stash show
git stash show stash@{index}

# 刪除暫存區(qū)指定記錄
git stash drop stash@{index}

# 清空緩存區(qū)
git stash clear
  1. 從草稿箱還原文件
# 恢復(fù)最近暫存的內(nèi)容
git stash pop

# 恢復(fù)指定草稿(pop會(huì)刪除暫存區(qū)內(nèi)容)
git stash pop stash@{index} 
git stash apply stash@{0} 
  1. 查看分支差異
# --stat參數(shù)不顯示具體差異代碼
git diff master yunfei [--stat]
  1. 刪除不存在的遠(yuǎn)程跟蹤分支并更新
git fetch -p
  1. 合并代碼
git merge [branch]

# 取消合并
git merge --abort

# 顯示分支合并圖
git log --graph

# 查看最近3次的提交和涉及的文件
git log -3 --stat

# 查看某次commit hashID的提交詳情
git show hashID
  1. 拆分A分支數(shù)據(jù)到B分支
git checkout A
git log (獲取A分支提交的commitID)
git checkout B
git cherry-pick commitID
git cherry-pick commit1..commit100 (可進(jìn)行連續(xù)拆分)
  1. 修改默認(rèn)配置
# 設(shè)置大小寫敏感
git config core.ignorecase false 

# 設(shè)置默認(rèn)用戶名
git config --global user.name "YOUR NAME"

# 設(shè)置默認(rèn)郵箱
git config --global user.email "YOUR EMAIL ADDRESS"

全局配置保存在:$Home/.gitconfig
本地倉庫配置保存在:.git/config

  1. 追溯指定文件的提交歷史
 git blame composer.json
  1. 撤銷提交(--hard為強(qiáng)制提交,可以不加)
git reset --hard HEAD

# 后退一步
git reset --hard HEAD^

# 后退N步
git reset --hard HEAD~2

# 撤銷指定記錄提交
git log
git reset --hard commit_id (哈希索引值)

# 撤銷指定文件提交
git reset HEAD README.MD (指定文件名)

git reflog (查看非明細(xì))
git reset HEAD@{1}  (注意數(shù)字1是需要撤銷到的版本)

英文狀態(tài)下按q退出git log

  1. 忽略提交文件

根目錄創(chuàng)建一個(gè)名為 .gitignore 的文件,并提交到git

# 此為注釋 – 將被 Git 忽略
*.a       # 忽略所有 .a 結(jié)尾的文件
!lib.a    # 但 lib.a 除外
/TODO     # 僅僅忽略項(xiàng)目根目錄下的TODO文件,不包括 subdir/TODO
build/    # 忽略 build/ 目錄下的所有文件
doc/*.txt # 會(huì)忽略 doc/notes.txt 但不包括 doc/server/arch.txt
  1. 忽略指定文件(丟棄工作區(qū)的修改)
git checkout -- application/api/controller/v1/stock/Tools.php

用于版本庫里的版本替換工作區(qū)的版本,無論工作區(qū)是修改還是刪除

  1. 刪除文件
git rm README.MD
git commit -m "remove README.MD"
  1. 常見問題
atal: It seems that there is already a rebase-apply directory, and
I wonder if you are in the middle of another rebase.  If that is the
case, please try
    git rebase (--continue | --abort | --skip)
If that is not the case, please
    rm -fr ".git/rebase-apply"
and run me again.  I am stopping in case you still have something
valuable there.

解決:

git status
git rebase --abort
最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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