git 快速入門及常見用法

身爲技術人員,都知道Git是幹嗎的。從服務端角度它是代碼倉庫,能夠多人協做、版本控制、高效處理大型或小型項目全部內容;從客戶端講,它可以方便管理本地分支、且與服務端代碼的同步,從拉取、合併、提交等等管理分支都靠它!git

Git輕量、易於學習,若是不用搭建和維護代碼倉庫的話(運維職責),只要掌握幾個git經常使用命令便可在工做中輕鬆應對。緩存

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

主要概念

快速入門,弄明白如下幾個概念便可:運維

  • 工做區(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你的代碼;

常見用法

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

1、checkout切換分支

git checkout <branch>:切換到你須要的分支(dev、hotfix)版本控制

git checkout -b <branch>: 若是沒有分支,加上-b參數表示建立並切換;rest

參考連接:https://git-scm.com/docs/git-checkoutcode

2、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

3、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

相關文章
相關標籤/搜索