Git常用指令基礎

添加

git add . #將本地倉庫的所有文件放入緩存區
git add <file name> #將本地倉庫的某個文件放入緩存區
git add <file name> <file name> #將多個文件放入緩存區 中間空格分隔
git add <dir name> #將某個目錄放入緩存區

刪除

git rm
git checkout

如果我們突然不想把我們add到緩存區的數據提交了。想撤回。

方式一:保留源文件 git rm --cached

# 先將test.txt放入緩存
$ git add test.txt
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory

# 清楚緩存中的test.txt
$ git rm --cached test.txt
rm 'test.txt'

$ git commit -m "肯定報錯"
On branch master


Untracked files:
        test.txt
# 再提交的時候,因爲緩存里根本沒有任何東西,所有先報 nothing added to commit(沒有任何東西能提交)
# 然後報錯,工作區有沒有跟蹤的文件
nothing added to commit but untracked files present

方式二:連帶源文件刪除 git rm

$ cat test.txt
aaa

$ git add test.txt

# 刪除
$ git rm test.txt
rm 'test.txt'

# 本地的文件也刪除了
$ ll
total 0

# 提交後,顯示將倉庫中的test.txt刪除了(如果倉庫中沒有test.txt,會提示nothing to commit, working tree clean)
$ git commit -m "test.txt?"
[master efbffdf] test.txt?
 1 file changed, 1 deletion(-)
 delete mode 100644 test.txt

方式三:

直接刪除本地的源文件,然後再提交一次。覆蓋上一次提交。

狀態查看

git diff #看本地倉庫與緩存區的不同
git diff --cached #看緩存區與上一次提交的不同
git status #查看狀態

比如我更改本地test.txt文件

$ git diff
diff --git a/test.txt b/test.txt
index 7284ab4..2964494 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
-aaaa
\ No newline at end of file
+aaaa
+111
\ No newline at end of file

這裏的意思就是 原來我們文件是隻有一行aaaa 我們在下面加了一行111(可能因爲是windows 換行符不識別顯示這個 \ No newline at end of file)

查看下狀態

$ git status
On branch master 
Changes not staged for commit: #文件有更改,但是還沒有add)
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   test.txt

no changes added to commit (use "git add" and/or "git commit -a")
$ git diff --cached  #因爲更改還在本地(工作區),沒到緩存,緩存區和上一次提交沒任何不同

$ git add test.txt #將test.txt添加到緩存區

$ git diff #這樣本地與緩存就沒區別了

$ git diff  --cached  #但是緩存和上一次提交就有區別了
diff --git a/test.txt b/test.txt
index 7284ab4..2964494 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
-aaaa
\ No newline at end of file
+aaaa
+111
\ No newline at end of file

$ git status  #再看狀態就變成了還沒提交(commit)的更改
On branch master

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   test.txt

git status還能體現一些別的信息

$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits. # 我們本地有兩次提交沒同步到遠端
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   test.txt

版本回退

git reset --hard HEAD/版本號

如果我們提交以後又後悔了,想撤銷之前的提交(添加了111這行)

方式一:用版本號

$ git reflog #查看日誌
8543ea8 (HEAD -> master) HEAD@{0}: commit: add 222  #多加了一次提交,爲了演示
80c4274 HEAD@{1}: commit: add 111

$ cat test.txt  #當前內容
aaaa
111
222

$ git reset --hard 80c4274  # 撤回到前一個版本
HEAD is now at 80c4274 add 111

$ cat test.txt  #查看內容  撤銷了222
aaaa
111

方式二:

$ git reset --hard HEAD^    #回退到上一版本
HEAD is now at 481099b Merge branch 'master' of https://github.com/Zbeginner/offer

$ cat test.txt    #查看一下111撤回了
aaaa

撤銷錯了?沒事~根據版本號還能回去,這就是所謂的版本控制吧

$ git reset --hard 8543ea8                                                   HEAD is now at 8543ea8 add 222

$ cat test
cat: test: No such file or directory

$ cat test.txt
aaaa
111
222

日誌查看

git log
$ git log
commit 8543ea8bfab0786cc1998e35500466cbe1284869 (HEAD -> master)
Author: zzy <[email protected]>
Date:   Sun Sep 29 14:32:54 2019 +0800

    add 222

commit 80c4274545a680ac87005b03af4efe63210b32b2
Author: zzy <[email protected]>
Date:   Sun Sep 29 14:32:32 2019 +0800

    add 111
#看的不太方便的話,可以這是在一行顯示
$ git log --pretty=oneline
8543ea8bfab0786cc1998e35500466cbe1284869 (HEAD -> master) add 222
80c4274545a680ac87005b03af4efe63210b32b2 add 111

分支控制

正常開發中,我們會有多個分支。而master分支顯然是不應該經常被修改的。所有,我們就需要在不同的分支下提交,然後將多個分支不同的內容合併到master分支上。

查看分支

Administrator@zzy MINGW64 /d/Github/offer (master) #括號裏顯示的是當前所在的分支
$ git branch
  dev
* master

創建分支

git branch 分支名

$ git branch test # 創建一個test分支

$ git branch  # 查看分支
  dev  # 這是我之前創建的
* master
  test # 這是我剛創建的

分支切換

git checkout 分支名

$ git checkout test
Switched to branch 'test'

Administrator@zzy MINGW64 /d/Github/offer (test) # 從這裏可以看到我們切換到了test分支

分支合併

# 現在test分支上新建一個文件
Administrator@zzy MINGW64 /d/Github/offer (test)
$ touch test

Administrator@zzy MINGW64 /d/Github/offer (test)
$ echo test
test

Administrator@zzy MINGW64 /d/Github/offer (test)
$ echo aaa>test

Administrator@zzy MINGW64 /d/Github/offer (test)
$ cat test
aaa

# 在test上提交
Administrator@zzy MINGW64 /d/Github/offer (test)
$ git add test

Administrator@zzy MINGW64 /d/Github/offer (test)
$ git commit -m "add test in test"
[test 78e4bf3] add test in test
 1 file changed, 1 insertion(+)
 create mode 100644 test

# 切換到master分支上
Administrator@zzy MINGW64 /d/Github/offer (test)
$ git checkout master
Switched to branch 'master'

# 查看一下顯然是沒有我們在test上創建的文件
Administrator@zzy MINGW64 /d/Github/offer (master)
$ ll
total 0

# git  merge 分支名    將test分支合併到當前分區
Administrator@zzy MINGW64 /d/Github/offer (master)
$ git merge test
Updating 9a88164..78e4bf3
Fast-forward
 test | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 test

# 再查看一下  有我們在test分支上的提交
Administrator@zzy MINGW64 /d/Github/offer (master)
$ ll
total 1
-rw-r--r-- 1 Administrator 197121  5 10月  4 09:44  test

Administrator@zzy MINGW64 /d/Github/offer (master)
$ cat test
aaa
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章