git 快速入門及常用命令

鋼鐵俠的知識庫發表於2020-09-14

身為技術人員,都知道Git是幹嘛的。從服務端角度它是程式碼倉庫,可以多人協作、版本控制、高效處理大型或小型專案所有內容;從客戶端講,它能夠方便管理本地分支、且與服務端程式碼的同步,從拉取、合併、提交等等管理分支都靠它!

Git輕量、易於學習,如果不用搭建和維護程式碼倉庫的話(運維職責),只要掌握幾個git常用命令即可在工作中輕鬆應對。

下面簡單介紹幾個概念,同時列出工作中常用命令:

主要概念

快速入門,弄明白以下幾個概念即可:

  • 工作區(Working Directory):就是你在電腦裡能看到的目錄,或克隆(clone)下來的目錄;
  • 版本庫(Repository):工作區裡面有一個隱藏目錄.git,這個不是工作區,而是Git的版本庫;
  • 暫存區(stage):版本庫中有一個叫stage的暫存區,git add可以把要提交的內容放到暫存區;
  • 主分支(master):版本庫還有一個叫master的主分支,git commit把暫存區所有內容提交到當前分支;

主要用法

工作中,一般我們提交程式碼只要四步:

  • 第一步,git pull 拉取程式碼,提交程式碼前確保和服務端倉庫一致,避免衝突;
  • 第二步,git add ./your_file.txt 把檔案新增進去,實際就是從工作區提交到暫存區;
  • 第三步,git commit -m 'first commit'提交更改,再把暫存區所有內容提交到當前分支(預設master);
  • 第四步,git push [remoteName]推送到遠端倉庫,也就是推到服務端,這樣別人就能拉取pull你的程式碼;

常見問題

平時工作也就用到上面四個步驟,當然了凡事有例外,下面說幾個例外的處理辦法:

一、checkout切換分支

git checkout <branch>:切換到你需要的分支(dev、hotfix)

git checkout -b <branch>: 如果沒有分支,加上-b參數列示建立並切換;

參考連結:https://git-scm.com/docs/git-checkout

二、git提交後撤銷問題

撤銷得分三種情況:

  • 第一,已經修改檔案但未執行git add的撤銷方法;
    我故意在.gitignore檔案修改之後且沒有git add,直接通過git checkout -- <file>撤銷;
    但是此命令不會撤銷新建的檔案,因為新建檔案還沒加入到Git管理系統中,
    所以git是未知的,自己手動刪除就好了。
λ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean

D:\learning\git\work (master -> origin)
λ git status
On branch master
Your branch is up-to-date with 'origin/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:   .gitignore

no changes added to commit (use "git add" and/or "git commit -a")

D:\learning\git\work (master -> origin)
λ git checkout -- .gitignore

D:\learning\git\work (master -> origin)
λ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean

擴充套件:
命令git checkout -- .gitignore意思就是,把.gitignore檔案在工作區的修改全部撤銷,這裡有兩種情況:
一種是.gitignore自修改後還沒有被放到暫存區,現在撤銷修改就回到和版本庫一模一樣的狀態;
一種是.gitignore已經新增到暫存區,又作了修改,現在,撤銷修改就回到新增到暫存區後的狀態。
總之,就是讓這個檔案回到最近一次git commit或git add時的狀態。
參考連結:https://www.liaoxuefeng.com/wiki/896043488029600/897889638509536

  • 第二,已經修改檔案且git add的撤銷方法
    需要先執行git reset .gitignore撤銷到未git add狀態,再執行第一步即可。
λ git add .gitignore

D:\learning\git\work (master -> origin)
λ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   .gitignore


D:\learning\git\work (master -> origin)
λ git reset .gitignore
Unstaged changes after reset:
M       .gitignore

D:\learning\git\work (master -> origin)
λ git status
On branch master
Your branch is up-to-date with 'origin/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:   .gitignore

no changes added to commit (use "git add" and/or "git commit -a")

D:\learning\git\work (master -> origin)
λ git checkout -- .gitignore

D:\learning\git\work (master -> origin)
λ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
  • 第三,Git已經commit如何撤銷:
    通過git reset --hard commitid直接回到未修改狀態。
λ git add .gitignore
λ git commit -m "test"
#(省略無用部分)
λ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean

D:\learning\git\work (master -> origin)
λ git log
commit b7de9378f39834dbc8304d4a8d30f39a4003c673 (HEAD -> master)
Author: test <test@163.com>
Date:   Mon Sep 14 02:59:02 2020 +0800

    test

commit b3ed1078e543cdb26b984dac584df9db7553d506 (origin/master, origin/HEAD)
Author: test <test@163.com>
Date:   Mon Sep 14 02:39:54 2020 +0800

    09142020
D:\learning\git\work (master -> origin)
λ git reset --hard b3ed1078e543cdb26b984dac584df9db7553d506
HEAD is now at b3ed107 09142020

D:\learning\git\work (master -> origin)
λ git log
commit b3ed1078e543cdb26b984dac584df9db7553d506 (HEAD -> master, origin/master, origin/HEAD)
Author: test <test@163.com>
Date:   Mon Sep 14 02:39:54 2020 +0800

    09142020
    
D:\learning\git\work (master -> origin)
λ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean

三、git stash儲存和恢復工作區內容

git stash可以將你已經修改,但不想提交(git push)的程式碼臨時儲存到堆疊中,也就是迴歸到你git pull時的狀態。然後就能隨意切換分支救火,完成後切換回來再git push pop即可恢復之前的修改內容。stash不僅可以恢復到原先開發的分支,也可以恢復到其他任意指定的分支上(可跨分支)。

  • git stash
    儲存當前工作進度,會把暫存區和工作區的改動儲存起來。
    執行完之後再git status,會發現當前是一個乾淨的工作區,沒有任何改動。
    使用git stash save 'message...'可以新增一些註釋
  • git stash list
    顯示儲存進度的列表。也就意味著,git stash命令可以多次執行。

  • git stash pop
    恢復最新的進度到工作區。git預設會把工作區和暫存區的改動都恢復到工作區。

  • git stash pop stash@{stash_id}
    恢復指定的進度到工作區。stash_id通過git stash list命令得到的
    通過git stash pop命令恢復進度後,會刪除當前進度。

  • git stash drop stash@{stash_id}
    可以使用git stash drop命令,後面可以跟stash_id
    或使用git stash clear命令,刪除所有快取的stash

  • git stash show
    檢視堆疊中最新儲存的stash和當前目錄的差異

舉個例子

  1. 修改.gitignore檔案,新建test.txt檔案。再執行stash發現新增檔案不會被儲存;
  2. git add之後再stash發現工作區是乾淨的;
  3. 說明沒有在git版本控制(git add)中的檔案,不能被git stash 儲存;
  4. 最後通過git stash pop恢復。
λ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   .gitignore

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        test.txt

no changes added to commit (use "git add" and/or "git commit -a")
D:\learning\git\timed_tasks (master -> origin)
λ git stash
Saved working directory and index state WIP on master: 542a055 create .gitignore

D:\learning\git\timed_tasks (master -> origin)
λ git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        test.txt

nothing added to commit but untracked files present (use "git add" to track)

D:\learning\git\timed_tasks (master -> origin)
λ git add test.txt

D:\learning\git\timed_tasks (master -> origin)
λ git stash
Saved working directory and index state WIP on master: 542a055 create .gitignore

D:\learning\git\timed_tasks (master -> origin)
λ git stash list
stash@{0}: WIP on master: 542a055 create .gitignore
stash@{1}: WIP on master: 542a055 create .gitignore
stash@{2}: WIP on (no branch): 542a055 create .gitignore

D:\learning\git\timed_tasks (master -> origin)
λ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

D:\learning\git\timed_tasks (master -> origin)
λ git stash show
 test.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)

D:\learning\git\timed_tasks (master -> origin)
λ git stash pop
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   test.txt

Dropped refs/stash@{0} (b69da2894d5e7f511be18277c5a0cd4582fbf453)

D:\learning\git\timed_tasks (master -> origin)
λ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   test.txt

Tip:如果你修改的所有檔案都不想要了怎麼辦?可通過git stash清空,懂吧?

λ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   test.txt

D:\learning\git\timed_tasks (master -> origin)
λ git stash
Saved working directory and index state WIP on master: 542a055 create .gitignore

D:\learning\git\timed_tasks (master -> origin)
λ git stash clear

D:\learning\git\timed_tasks (master -> origin)
λ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

----by 鋼鐵 648403020@qq.com

參考資料:
Git官方文件:https://git-scm.com/docs
廖雪峰Git教程:https://www.liaoxuefeng.com/wiki/896043488029600