1. 程式人生 > 實用技巧 >git教程

git教程

目錄
  • Git和SVN的區別
  • git教程:理論基礎
    • git的三棵樹:
      • 工作區域(工作目錄):
      • 暫存區域:
      • git倉庫:
    • git的工作流程:
    • git回滾快照的工作流程:
    • git管理的檔案三狀態:
  • git教程:入門操作
    • 初次使用Git的前的配置
      • 1、在命令列模式中輸入以下命令:
      • 2、然後檢視配置資訊:
      • 3、進入git的專案目錄:
      • 4、初始化git:
    • 檢視快照:
    • 刪除兩個無用commit
    • Git刪除版本庫中的一個commit
  • git教程:git分支
    • git建立分支:
    • git刪除分支:
    • git切換分支:
    • git合併分支:
    • git建立分支的方式:指標指向分支的開始點
  • git教程:幾種狀態
    • 正常狀態
    • 有檔案被刪除的狀態
    • 有新檔案的狀態
    • 檔案內容被修改的狀態
    • 檔案被重新命名時的狀態
  • git教程:版本對比
    • git diff表示版本對比
    • git diff 快照ID1 快照ID2
    • git diff 快照ID
    • git diff --cached 快照ID
  • How to push your projects to github
    • create a new repository on the command line
    • push an existing repository from the command line
    • github url of https
    • github url of ssh

Git和SVN的區別

SVN記錄的是每一次版本變動的內容
Git是將每個版本獨立儲存

git教程:理論基礎

三棵樹
工作流程
三狀態

git的三棵樹:

工作區域(工作目錄):

平時存放專案的地方。

暫存區域:

臨時存放你的改動。是一個檔案,存放即將提交到倉庫的一個列表資訊。

git倉庫:

最終安全存放所有版本資訊的位置。

git的工作流程:

  • 1.在工作目錄中新增、修改檔案。
  • 2.將需要進行版本管理的檔案放入暫存區域。
  • 3.將暫存區域的檔案提交到git倉庫。

git回滾快照的工作流程:

  • 1.移動HEAD的指向(--soft)
  • 2.將快照回滾到暫存區域(--mixed,預設)
  • 3.將暫存區域還原到工作目錄(--hard)

git管理的檔案三狀態:

  • 已修改(modified)
  • 已暫存(staged)
  • 已提交(committed)

git教程:入門操作

初次使用Git的前的配置

(已安裝Git軟體,並在安裝時自動配置環境變數path):

1、在命令列模式中輸入以下命令:

git config --global user.name  "Tsui"
git config --global user.email  "[email protected]"

2、然後檢視配置資訊:

git config --list

3、進入git的專案目錄:

C:\Users\Administrator>cd /d D:\workspace\Git_Myproject
D:\workspace\Git_Myproject>

4、初始化git:

D:\workspace\Git_Myproject>git init
Reinitialized existing Git repository in D:/workspace/Git_Myproject/.git/

git status:檢視git的當前工作狀態

檢視快照:

git log
git log --oneline
//--oneline:快照的精簡版本
git log --help
//git log的幫助文件,會在瀏覽器中開啟
git log --graph --all
//--graph表示圖形化顯示log
//--all表示顯示所有的分支
D:\workspace\Git_Myproject>git log --oneline --graph --all
* 5b6b147 (HEAD -> master) modify a file cshcesh.txt v0.3
| * bef58a0 (feature) add a file testspool.txt featurev0.2
| * 220fa33 add a file test.txt featurev0.1
|/
* 1346315 add a file cshcesh.txt v0.2
* 62a8bde add two file v0.1

快照ID不用全部輸入,前5個就可以
新新增的檔案不在暫存區域,則為未跟蹤狀態。
新新增的檔案在暫存區域,則為跟蹤狀態。

  • 場景1:當你改亂了工作區某個檔案的內容,想直接丟棄工作區的修改時,用命令 git checkout -- file 。(懂)
  • 場景2:當你不但改亂了工作區某個檔案的內容,還新增到了暫存區時,想丟棄修改,分兩步,第一步用命令 git reset HEAD file ,就回到了場景1,第二步按場景1操作。(懂)
  • 場景3:已經提交了不合適的修改到版本庫時,想要撤銷本次提交,參考 版本回退(https://www.liaoxuefeng.com/wiki/896043488029600/897013573512192)一節,不過前提是沒有推送到遠端庫(不懂)

刪除兩個無用commit

  • 1、首先修改倉庫(本地和暫存區域不變),回退到前面的版本
    git reset --soft HEAD~3
    
  • 2、然後commit,中間的版本相當於刪除了
    git commit -m "delete 2 commits"
    

Git刪除版本庫中的一個commit

參考連結:https://www.jianshu.com/p/ee8fb047b085

  • 在使用git的時候,本來不想提交一個檔案的,但是一不小心就commit了,所以就瞭解了一下怎麼刪除該commit,當你此時還沒有push的時候,只需要一條命令:
    git reset --hard HEAD~1
    
  • 直接取消上一次 commit用git reset HEAD~1; 如果加上引數--hard 則不保留當前更改
    如果你已經push了,那麼使用如下命令:
    //回滾到你想回滾的commit
    git reset --hard <commit_id> 
    //重新push到你的遠端倉庫
    git push origin HEAD --force
    

git教程:git分支

git建立分支:

D:\workspace\Git_Myproject>git branch feature
//建立一個分支,分支名是feature
D:\workspace\Git_Myproject>git log --decorate
//--decorate標記讓git log顯示指向這個提交的所有引用(比如說分支、標籤等)
commit 13463151f6863d6c2b1e9a9a90bc886ad13c7383 (HEAD -> master, feature)
//預設分支是master,即主分支
//另一個分支是feature
//HEAD現在指向的是master分支
Author: Tsui <[email protected]>
Date:   Sun Apr 28 22:10:40 2019 +0800
    add a file cshcesh.txt v0.2
commit 62a8bdeee21268fd7d3285c18e1b8e476ece16a3
Author: Tsui <[email protected]>
Date:   Fri Apr 26 15:21:56 2019 +0800
    add two file v0.1

git刪除分支:

git branch -d或 --d 分支名
//--d是全稱,--d是精簡的表示,兩個命令一樣

D:\workspace\Git_Myproject>git branch -d feature2
Deleted branch feature2 (was 678c20e).
D:\workspace\Git_Myproject>git log --all --graph --oneline
* 678c20e (HEAD -> master) add a file feature2.txt feature2v0.1
*   de4c5bf Merge branch 'feature'
|\
| * bef58a0 add a file testspool.txt featurev0.2
| * 220fa33 add a file test.txt featurev0.1
* | 5b6b147 modify a file cshcesh.txt v0.3
|/
* 1346315 add a file cshcesh.txt v0.2
* 62a8bde add two file v0.1

git切換分支:

(從分支切換到主分支,分支檔案也顯示了)

D:\workspace\Git_Myproject>git checkout feature
//git checkout -b feature2:表示建立並切換分支
Switched to branch 'feature'
D:\workspace\Git_Myproject>git log
commit 13463151f6863d6c2b1e9a9a90bc886ad13c7383 (HEAD -> feature, master)
Author: Tsui <tsuish@qq.com>
Date:   Sun Apr 28 22:10:40 2019 +0800
    add a file cshcesh.txt v0.2
commit 62a8bdeee21268fd7d3285c18e1b8e476ece16a3
Author: Tsui <tsuish@qq.com>
Date:   Fri Apr 26 15:21:56 2019 +0800
    add two file v0.1

git合併分支:

git merge 分支名

D:\workspace\Git_Myproject>git merge feature2
Updating de4c5bf..678c20e
Fast-forward
feature2.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 feature2.txt

git建立分支的方式:指標指向分支的開始點

圖解如下:



git教程:幾種狀態

正常狀態

D:\workspace\Git_Myproject>git status
On branch master
nothing to commit, working tree clean

有檔案被刪除的狀態

(從工作區域刪除了,暫存區域不變,倉庫不變):

D:\workspace\Git_Myproject>git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
        deleted:    README.md
no changes added to commit (use "git add" and/or "git commit -a")

有兩個選擇:
選擇1:刪除版本庫中對應檔案(分三步)
選擇2:從版本庫中恢復檔案

  • 選擇1:(第一步:將檔案從暫存區域刪除)
D:\workspace\Git_Myproject>git rm README.md
//git rm -f README.md表示暴力刪除(force remove)
//應用場景:刪除工作區域和暫存區域內容不一樣的檔案
//加-f會同時把工作區域和暫存區域的檔案刪除(這一點和不加一樣)
//git rm --cached README.md表示:
//只刪除暫存區域的檔案,保留工作區域的檔案
rm 'README.md'
  • 選擇1:(第二步【會在log中新建快照】:將暫存區域檔案提交到倉庫)
D:\workspace\Git_Myproject>git commit -m "remove README.md"
//-m表示提交暫存區域檔案到倉庫時的註釋內容
[master 0eb2b88] remove README.md
1 file changed, 1 deletion(-)
delete mode 100644 README.md
  • 選擇1:(第三步:將倉庫中新建的快照刪除)
D:\workspace\Git_Myproject>git reset --soft 快照ID
  • 選擇2:
D:\workspace\Git_Myproject>git checkout -- README.md
D:\workspace\Git_Myproject>git status
On branch master
nothing to commit, working tree clean

有新檔案的狀態

(工作區域新增檔案,未在暫存區域,未提交到倉庫):

D:\workspace\Git_Myproject>git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        README.md
nothing added to commit but untracked files present (use "git add" to track)

將新檔案存放到倉庫,分兩步

  • 第一步:新增檔案到暫存區域(工作區域新增檔案,暫存區域新增檔案,未提交到倉庫):
D:\workspace\Git_Myproject>git add README.md
//也可以git add *
D:\workspace\Git_Myproject>git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
        new file:   README.md
  • 第二步:將暫存區域檔案提交到倉庫:
D:\workspace\Git_Myproject>git commit -m "add README.md 2019-4-25"
//-m表示提交暫存區域檔案到倉庫時的註釋內容
[master 1c0e390] add README.md 2019-4-25
1 file changed, 1 insertion(+)
create mode 100644 README.md

檔案內容被修改的狀態

(工作區域檔案修改,暫存區域不變,倉庫不變):

D:\workspace\Git_Myproject>git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
        modified:   README.md
no changes added to commit (use "git add" and/or "git commit -a")

將修改後的檔案新增到倉庫,分兩步

  • 第一步:將修改後的檔案新增到暫存區域:
D:\workspace\Git_Myproject>git add README.md
D:\workspace\Git_Myproject>git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
        modified:   README.md
  • 第二步:將暫存區域檔案提交到倉庫:
D:\workspace\Git_Myproject>git commit -m "modify: README.md 2019-4-25 v0.1"
//-m表示提交暫存區域檔案到倉庫時的註釋內容
[master 2b09814] modify: README.md 2019-4-25 v0.1
1 file changed, 1 insertion(+), 1 deletion(-)

檔案被重新命名時的狀態

(工作區域檔案修改,暫存區域不變,倉庫不變):
git顯示原檔案被刪除,添加了一個新檔案。

D:\workspace\Git_Myproject>git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
        deleted:    cesh.txt
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        cshcesh.txt
no changes added to commit (use "git add" and/or "git commit -a")

重新命名檔名的方法,分三步:
第一步:在工作區域把檔名改回去,git狀態正常。
第二步:用git mv命令修改工作區域和暫存區域檔名
第三步:提交暫存區域檔案到倉庫中

D:\workspace\Git_Myproject>git mv cesh.txt cshcesh.txt
//git mv 舊檔名 新檔名 等價於以下兩步:
//    git rm 舊檔名
//    git add 新檔名
D:\workspace\Git_Myproject>git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
        renamed:    cesh.txt -> cshcesh.txt
D:\workspace\Git_Myproject>
D:\workspace\Git_Myproject>git commit -m "rename cesh.txt -> cshcesh.txt v0.4"
[master 879bee4] rename cesh.txt -> cshcesh.txt v0.4
1 file changed, 0 insertions(+), 0 deletions(-)
rename cesh.txt => cshcesh.txt (100%)

git教程:版本對比

git diff表示版本對比

倉庫為舊-,工作區域為新+

git diff 快照ID1 快照ID2

表示比較兩個快照
快照順序有影響,前面的為舊-,後面的為新+

git diff 快照ID

表示比較當前工作區域和倉庫中的快照。
倉庫為舊-,工作區域為新+

git diff --cached 快照ID

表示比較暫存區域和倉庫中的快照。
暫存區域為舊-,倉庫為新+

不加快照ID表示當前HEAD指向的快照

D:\workspace\Git_Myproject>git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
        modified:   README.md
        modified:   game.py
no changes added to commit (use "git add" and/or "git commit -a")
D:\workspace\Git_Myproject>git diff
//git diff命令:檢視檔案改動的詳情
diff --git a/README.md b/README.md
//表示對比的是工作區域和暫存區域的檔案README.md
index 197049a..e30bfa5 100644
--- a/README.md
//---表示是舊檔案,即存放在暫存區域的檔案
+++ b/README.md
//+++表示是新檔案,即存放在工作區域的檔案
@@ -1 +1,2 @@
//@@開頭@@結尾,中間的-表示舊檔案,+表示新檔案,
//-1表示舊檔案從第一行開始顯示
//+1,2表示新檔案從第一行開始顯示,一共顯示兩行
//-1後沒有數字,表示舊檔案已經完全包含在新檔案中了
+1233311111111111111111111111
//+號後面的內容表示是新檔案所特有的
<E6><96><87><E5><AD><97><E6><B8><B8><E6><88><8F>
\ No newline at end of file
//這兩行表示是新舊檔案共有的內容
//\ No newline at end of file表示:檔案不是以換行符結束
diff --git a/game.py b/game.py
index e69de29..8ffc73b 100644
--- a/game.py
+++ b/game.py
@@ -0,0 +1 @@
+123133333333333333
\ No newline at end of file
D:\workspace\Git_Myproject>

How to push your projects to github

create a new repository on the command line

echo "# springdemo" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/BitTsui/springdemo.git
git push -u origin master

push an existing repository from the command line

git remote add origin https://github.com/BitTsui/springdemo.git
git push -u origin master

github url of https

https://github.com/BitTsui/springdemo.git

github url of ssh

git@github.com:BitTsui/springdemo.git