1. 程式人生 > >git的使用教程 | 完整版

git的使用教程 | 完整版

git簡介:

git是一個版本控制工具,實現方式為分散式版本庫,區別於集中式等等 在這裡插入圖片描述

git安裝:

git配置

  • 三個配置檔案
    • /etc/gitconfig git全域性配置檔案
    • ~/.gitconfig 當前使用者配置檔案
    • .git/config 當前專案配置檔案
  • 使用git config編輯命令對配置檔案進行編輯
#配置本地全域性使用者資訊
$ git config --global user.name "YourName"
$ git config --global user.email "YourEmail"

#配置你的編輯器
$ git config --global core.editor emacs

#檢視你的配置資訊
$ git config --list $ git --version #或者 $ vim ~/.gitconfig $ vim /etc/gitconfig

git工作流程

認識三個區:工作區,暫存區,版本庫

git命令

建立倉庫

git init

克隆倉庫

git clone

新增快取

git add file //將檔案新增至暫存區 git status -s //檢視檔案寫入狀態 touch file //建立檔案 vim file //改動檔案 在這裡插入圖片描述 A表示提交至快取區 AM表示檔案有新的改動 ??表示檔案沒有提交至快取區 git diff git diff --cached檢視未快取,和已經快取的改動記錄

提交倉庫

git commit //在此之前必須設定使用者及其郵箱

$ git config --global user.name 'runoob'
$ git config --global user.email [email protected]

刪除檔案及移動檔案

$ git rm
$ git mv file1 file2

git強大的分支管理

$ git branch //列出分支
$ git branch branchName //建立分支
$ git checkout branchName //切換分支
$ git merge //合併分支

注:如何解決合併衝突問題 在這裡插入圖片描述

檢視提交版本資訊

git log

新增標籤和備註資訊

git tag

連線遠端 程式碼倉庫

#第一步:ssh連線你的github
$ ssh-keygen -t rsa -C "[email protected]"
#後面的 [email protected] 改為你在 github 上註冊的郵
#箱,之後會要求確認路徑和輸入密碼,我們這使用預設的一路回車就行。成功
#的話會在~/下生成.ssh資料夾,進去,開啟 id_rsa.pub,複製裡面的 #key。
#回到 github 上,進入 Account => Settings(賬戶配置)新增你的ssh_key

#第二步:驗證是否成功連線
$ ssh -T [email protected]
Hi huoying12138! You've successfully authenticated, but GitHub does not provide shell access.
#表示連線成功

#第三步
在Github新建一個倉庫
在你的本地同時也新建一個倉庫
$ mkdir test //新建名為test的倉庫

$ cd test/
$ touch README.md
$ "# 這是一個測試檔案">>README.md //新建一個測試檔案

$ git init
$ git add README.md
$ git commit -m "MyFirstSubmit" //將.md提交至本地master倉庫

//提交到github倉庫
$ git remote add origin [email protected]:huoying12138/GitTest.git //表示用origin別名將你的遠端倉庫相關聯
$ git push -u origin master   //將你的本地master分支推送至origin遠端倉庫,也就是github

成功截圖如下 在這裡插入圖片描述

提取遠端倉庫及合併至本地

git fetch 遠端倉庫名 提取如下:在這裡插入圖片描述

git merge origin/master 如圖表示同步更新成功至本地: 在這裡插入圖片描述 //如果遠端倉庫有更新,需要使用fetcht提取,再使用merge合併,從而更新同步至本地倉庫

本地同步更新至遠端倉庫

git push origin master //origin表示遠端倉庫別名,master表示本地需要同步至遠端倉庫的分支