1. 程式人生 > >四、Git入門與基本使用(4)

四、Git入門與基本使用(4)

沒有 object mod second osi type mark apply save

16、把連續的多個commit整理成1個

commit ce587039661c88fd508035fd103a012e33c057ac (HEAD -> temp)
Author: Jone <[email protected]>
Date:   Thu Mar 14 17:03:07 2019 +0800

    Update fourth file.txt

commit bfd373ab1dd5b2d578bac9cacd4d89d0066c51af
Author: Jone <[email protected]>
Date:   Thu Mar 14 17:01:46 2019 +0800

    Add fouth file.txt

commit b843c287804d2b5886167740f9e6c0d327540ee1 (tag: 03tag)
Author: Jone <[email protected]>
Date:   Thu Mar 14 17:00:21 2019 +0800

    Add third file

commit 0bd98cb5d0d969cfc35d8c5a16d33b5924cbc6b0
Author: Jone <[email protected]>
Date:   Thu Mar 14 16:59:25 2019 +0800

    Add second file

在上面的commit中如果想將前三個commit合成一個,可以使用如下操作:

$ git rebase -i 0bd98cb5d0d969        #合並前三個commit.
[detached HEAD b0fc955] Merge three commits
 Date: Thu Mar 14 17:00:21 2019 +0800
 2 files changed, 3 insertions(+)
 create mode 100644 fourth.txt
 create mode 100644 third.txt
Successfully rebased and updated refs/heads/temp.

$ git log                                      #合並後的結果
commit b0fc95597b4be1e1c11f94bb77931f0338b581bf (HEAD -> temp)
Author: Jone <[email protected]>
Date:   Thu Mar 14 17:00:21 2019 +0800

    Merge three commits

    Add third file

    Add fouth file.txt

    Update fourth file.txt

commit 0bd98cb5d0d969cfc35d8c5a16d33b5924cbc6b0
Author: Jone <[email protected]>
Date:   Thu Mar 14 16:59:25 2019 +0800

    Add second file

commit c8588e43dd1053684632871fb8aec1945ee6a6ab
Author: Jone <[email protected]>
Date:   Thu Mar 14 16:36:00 2019 +0800

    Add first file

進入編輯界面後的操作:
技術分享圖片
技術分享圖片
17、把間隔的幾個commit整理成1個

$ git log
commit 5d63d9384d28a5bf4786bc5639fc3dbc58cc2fc8 (HEAD -> master)
Author: Jone <[email protected]>
Date:   Fri Mar 15 17:46:32 2019 +0800

    move first.txt to first.md

commit 7376bc5b2ebc3e13d4c4552ebdef348a17cd4eef
Author: Jone <[email protected]>
Date:   Thu Mar 14 17:03:07 2019 +0800

    Update fourth file

commit 1d63ec82259b237f58e7525ccf856a03fb880fcd
Author: Jone <[email protected]>
Date:   Thu Mar 14 17:01:46 2019 +0800

    Add fouth file

commit b843c287804d2b5886167740f9e6c0d327540ee1 (tag: 03tag)
Author: Jone <[email protected]>
Date:   Thu Mar 14 17:00:21 2019 +0800

    Add third file

commit 0bd98cb5d0d969cfc35d8c5a16d33b5924cbc6b0
Author: Jone <[email protected]>
Date:   Thu Mar 14 16:59:25 2019 +0800

    Add second file

根據以上log,將第一/二/四次的commit合成一個,操作如下:
技術分享圖片

$ git rm fourth.txt
fourth.txt: needs merge
rm ‘fourth.txt‘

nxf42573@NXW53034 MINGW64 /d/git_learning (master|REBASE-i 2/4)
$ git rebase --continue
[detached HEAD e0326fb] Merge three commits
 Date: Thu Mar 14 17:00:21 2019 +0800
 2 files changed, 1 insertion(+)
 rename first.txt => first.md (100%)
 create mode 100644 third.txt
Successfully rebased and updated refs/heads/master.
$ git log
commit 25efd88365c4b9c31634a8bb06d25fe23109eb22 (HEAD -> master)
Author: Jone <[email protected]>
Date:   Thu Mar 14 17:01:46 2019 +0800

    Add fouth file

commit e0326fb0984785866419d9125a8a7427f6a8a765
Author: Jone <[email protected]>
Date:   Thu Mar 14 17:00:21 2019 +0800

    Merge three commits

    Add third file

    move first.txt to first.md

commit 0bd98cb5d0d969cfc35d8c5a16d33b5924cbc6b0
Author: Jone <[email protected]>
Date:   Thu Mar 14 16:59:25 2019 +0800

    Add second file

18、比較暫存區和HEAD所含文件的差異

$ echo "Update" >> fourth.txt     #修改文件

$ git add fourth.txt                     #添加文件到暫存區

$ git diff --cached                    #比較HEAD與暫存區之間的差異
diff --git a/fourth.txt b/fourth.txt
index 02f7874..50d23e7 100644
--- a/fourth.txt
+++ b/fourth.txt
@@ -1 +1,2 @@
 fourth file
+Update

19、比較工作區和暫存區所含文件的差異

$ vim fourth.txt    #修改工作區文件

$ git diff               #比較工作區與暫存區文件
diff --git a/fourth.txt b/fourth.txt
index 50d23e7..bcd337e 100644
--- a/fourth.txt
+++ b/fourth.txt
@@ -1,2 +1,3 @@
 fourth file
 Update
+Changed

20、讓暫存區恢復成和HEAD的一樣

$ git diff --cached      #比較文件差異
diff --git a/fourth.txt b/fourth.txt
index 02f7874..50d23e7 100644
--- a/fourth.txt
+++ b/fourth.txt
@@ -1 +1,2 @@
 fourth file
+Update

$ git reset HEAD      #恢復暫存區為HEAD
Unstaged changes after reset:
M       fourth.txt

$ git diff --cached      #比較

21、如何讓工作區的文件恢復為和暫存區一樣

$ git status                       #當前狀態,暫存區有兩個文件沒有commit
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   first.md
        modified:   fourth.txt

$ vim first.md             #修改工作區文件

$ git status                          
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   first.md
        modified:   fourth.txt

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:   first.md

$ git checkout -- first.md                #恢復工作區文件和暫存區文件一樣

$ git diff                            #比較差異

22、怎樣取消暫存區部分文件的更改(恢復暫存區部分文件和HEAD一致)

$ git status                  #當前狀態
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   first.md
        modified:   fourth.txt

$ git reset HEAD first.md     #恢復first_md文件與HEAD一致
Unstaged changes after reset:
M       first.md

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   fourth.txt

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:   first.md

23、消除最近的幾次提交

$ git log    #當前commit信息
commit 25efd88365c4b9c31634a8bb06d25fe23109eb22 (HEAD -> master)
Author: Jone <[email protected]>
Date:   Thu Mar 14 17:01:46 2019 +0800

    Add fouth file

commit e0326fb0984785866419d9125a8a7427f6a8a765
Author: Jone <[email protected]>
Date:   Thu Mar 14 17:00:21 2019 +0800

    Merge three commits

    Add third file

    move first.txt to first.md

commit 0bd98cb5d0d969cfc35d8c5a16d33b5924cbc6b0
Author: Jone <[email protected]>
Date:   Thu Mar 14 16:59:25 2019 +0800

    Add second file

commit c8588e43dd1053684632871fb8aec1945ee6a6ab

$ git reset --hard  e0326fb0984785866419d    #回退到倒數第二次的commit
HEAD is now at e0326fb Merge three commits

$ git log
commit e0326fb0984785866419d9125a8a7427f6a8a765 (HEAD -> master)
Author: Jone <[email protected]>
Date:   Thu Mar 14 17:00:21 2019 +0800

    Merge three commits

    Add third file

    move first.txt to first.md

commit 0bd98cb5d0d969cfc35d8c5a16d33b5924cbc6b0
Author: Jone <[email protected]>
Date:   Thu Mar 14 16:59:25 2019 +0800

    Add second file

commit c8588e43dd1053684632871fb8aec1945ee6a6ab
Author: Jone <[email protected]>
Date:   Thu Mar 14 16:36:00 2019 +0800

    Add first file

24、不同提交的指定文件的差異

$ git log --all --graph  -n2
* commit e0326fb0984785866419d9125a8a7427f6a8a765 (HEAD -> master)
| Author: Jone <[email protected]>
| Date:   Thu Mar 14 17:00:21 2019 +0800
|
|     Merge three commits
|
|     Add third file
|
|     move first.txt to first.md
|
| * commit b0fc95597b4be1e1c11f94bb77931f0338b581bf (temp)
|/  Author: Jone <[email protected]>
|   Date:   Thu Mar 14 17:00:21 2019 +0800
|
|       Merge three commits
|
|       Add third file
|
|       Add fouth file.txt
|
|       Update fourth file.txt

$ git diff master temp     #比較兩個分支的不同
diff --git a/first.md b/first.txt
similarity index 100%
rename from first.md
rename to first.txt
diff --git a/fourth.txt b/fourth.txt
new file mode 100644
index 0000000..0dbe3fc
--- /dev/null
+++ b/fourth.txt
@@ -0,0 +1,2 @@
+fourth file
+Update the file

$ git diff e0326fb0984785 b0fc95597b4be1e    #也可以使用分支對應的commit比較,也可以比較指定文件
diff --git a/first.md b/first.txt
similarity index 100%
rename from first.md
rename to first.txt
diff --git a/fourth.txt b/fourth.txt
new file mode 100644
index 0000000..0dbe3fc
--- /dev/null
+++ b/fourth.txt
@@ -0,0 +1,2 @@
+fourth file
+Update the file

25、正確刪除文件的方法

$ git rm first.md
rm ‘first.md‘

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    first.md

26、開發中臨時加塞了緊急任務怎麽處理(臨時切換其他分支,保存當前環境的方法)

$ git status       #查看當前狀態
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    first.md

$ git stash               #將當前狀態保存到堆棧中
Saved working directory and index state WIP on master: e0326fb Merge three commits

$ git status                
On branch master
nothing to commit, working tree clean

$ git stash apply      #可以使用apply命令恢復環境,但是此時stash中會有備份
Removing first.md
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:    first.md

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

$ git stash list
stash@{0}: WIP on master: e0326fb Merge three commits

$ git stash pop     #也可以使用pop命令恢復環境,但是此時stash中不再保留stash備份
Removing first.md
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:    first.md

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (e404b853cc36e4816c2cb262a41b15f24a030aa7)

$ git stash list

27、如何指定不需要Git管理的文件

nxf42573@NXW53034 MINGW64 /d/git_learning (master)
$ echo "Hello word!" > read.txt      #新建read.txt

nxf42573@NXW53034 MINGW64 /d/git_learning (master)
$ git status                           
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        read.txt

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

$ echo "read.txt" > .gitignore    #新建.gitignore文件,指定git不管理read.txt文件

nxf42573@NXW53034 MINGW64 /d/git_learning (master)
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .gitignore

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

28、如何將Git倉庫備份到本地
git中常用的傳輸協議:
技術分享圖片

啞協議和智能協議的區別:
技術分享圖片
備份特點:
技術分享圖片

$ pwd
/d/bak

$ git clone --bare /d/git_learning/.git    ya.git    #啞協議備份
Cloning into bare repository ‘ya.git‘...
done.

nxf42573@NXW53034 MINGW64 /d/bak
$ git clone --bare file:///d/git_learning/.git    zhineng.git  #智能協議
Cloning into bare repository ‘zhineng.git‘...
remote: Counting objects: 14, done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 14 (delta 5), reused 0 (delta 0)
Receiving objects: 100% (14/14), done.
Resolving deltas: 100% (5/5), done.

$ pwd
/d/git_learning

$ git remote add zhineng file:///d/git_learning/.git    #添加遠程倉庫

$ git remote -v
zhineng file:///d/git_learning/.git (fetch)
zhineng file:///d/git_learning/.git (push)

$ git checkout -b Jone                        #添加新分支
Switched to a new branch ‘Jone‘

$ git branch
* Jone
  master
  temp

$ git push zhineng     #將該分支推送到遠程倉庫
fatal: The current branch Jone has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream zhineng Jone

$  git push --set-upstream zhineng Jone
Everything up-to-date
Branch ‘Jone‘ set up to track remote branch ‘Jone‘ from ‘zhineng‘.

四、Git入門與基本使用(4)