Git命令一覽

Git

準備工作

安裝

  • Ubuntu

    • sudo apt-get install git
  • Windows

    • 下載、安裝

      • https://git-scm.com/downloads

全局配置

  • git config --global user.name “Your Name”
  • git config --global user.email “[email protected]

創建版本庫

  • 初始化一個Git倉庫

    • git init
  • 添加文件到Git倉庫

    • git add
    • git commit -m “message”

版本控制

版本回退

  • 查看提交歷史

    • git log
  • 在版本的歷史之間穿梭

    • git reset --hard commit_id
  • 重返未來

    • 查看歷史命令

      • git reflog

工作區、暫存區

撤銷修改

  • 改亂了工作區某文件,想直接丟棄工作區的修改

    • git checkout – file
  • 改亂了工作區某文件,還添加到了暫存區時,想丟棄修改

    • git reset HEAD
    • git checkout – file
  • 已經提交了不合適的修改到版本庫時,想要撤銷本次提交

刪除文件

  • 刪除工作區文件

    • rm
  • 刪除暫存區文件

    • git rm
  • 提交刪除

    • git commit -m “remove file”

遠程倉庫

添加遠程github庫

  • 在github上建立一個庫

  • 關聯遠程庫

  • 首次 推送

    • git push -u origin master
  • 常規推送

    • git push origin master

從遠程庫克隆

  • ssh

  • https

    • git clone https://github.com/account-name/repo-name.git

使用GitHub

  • 在GitHub上,可以任意Fork開源倉庫
  • 自己擁有Fork後的倉庫的讀寫權限
  • 可以推送pull request給官方倉庫來貢獻代碼

使用gitee遠程庫

  • 在碼雲上建立一個庫

  • 查看遠程庫信息

    • git remote -v
  • 刪除已有的GitHub遠程庫

    • git remote rm origin
  • 關聯碼雲的遠程庫

同時關聯github和碼雲

分支管理

創建、合併

  • 查看分支

    • git branch
  • 創建分支

    • git branch
  • 切換分支

    • git checkout 或者git switch
  • 創建+切換

    • git checkout -b 或者git switch -c
  • 合併某分支到當前分支

    • git merge
  • 刪除分支

    • git branch -d

解決衝突

  • 衝突:master分支和feature1分支各自都對同一個文件分別有新的提交
  • 在後提交的分支上手動修改衝突文件
  • 再git add 、git commit

分支管理策略

  • 再merge分支時,生成一個commit

    • git merge --no-ff -m “merge with no-ff” dev
  • master分支、發佈新版本、不幹活

  • dev分支幹活

    • 完事後merge到master
  • 團隊合作時,每個人都往dev分支上合併

bug分支

  • 修復bug時,創建新的bug分支進行修復,然後合併,最後刪除

  • 當手頭工作沒有完成時,先把工作現場git stash

  • 修復bug後,再git stash pop

  • 在master分支上修復的bug,想要合併到當前dev分支

    • git cherry-pick

feature分支

  • 開發一個新feature,最好新建一個分支

  • 丟棄未合併過的分支

    • git branch -D

多人協作

  • 從本地推送分支,使用git push origin branch-name,如果推送失敗,先用git pull抓取遠程的新提交

  • 在本地創建和遠程分支對應的分支

    • git checkout -b branch-name origin/branch-name
  • 建立本地分支和遠程分支的關聯

    • git branch --set-upstream branch-name origin/branch-name
  • 從遠程抓取分支,使用git pull,如果有衝突,要先處理衝突。

rebase

  • rebase操作可以把本地未push的分叉提交歷史整理成直線
  • 使得查看歷史提交的變化時更容易

標籤管理

創建標籤

  • 新建一個標籤,默認爲HEAD

    • git tag
  • 爲指定commit id新建標籤

    • git tag
  • 指定標籤信息

    • git tag -a -m “blablabla…”
  • 查看說明文字

    • git show
  • 查看所有標籤

    • git tag
  • 查看指定標籤

    • git tag

操作標籤

  • 推送一個本地標籤

    • git push origin
  • 推送全部未推送過的本地標籤

    • git push origin --tags
  • 刪除一個本地標籤

    • git tag -d
  • 刪除一個遠程標籤

    • git push origin :refs/tags/

自定義

忽略特殊文件

配置別名

alias realname
lg log --color --graph --pretty=format:’%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset’ --abbrev-commit
st status
co checkout
ci commit
br branch
unstage reset HEAD
last log -1

搭建git服務器

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章