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

进入编辑界面后的操作:
四、Git入门与基本使用(4)_第1张图片
四、Git入门与基本使用(4)_第2张图片
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入门与基本使用(4)_第3张图片

$ 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 ..." to unstage)

        modified:   first.md
        modified:   fourth.txt

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

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

        modified:   first.md
        modified:   fourth.txt

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." 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 ..." 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 ..." to unstage)

        modified:   fourth.txt

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." 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 ..." to unstage)

        deleted:    first.md

26、开发中临时加塞了紧急任务怎么处理(临时切换其他分支,保存当前环境的方法)

$ git status       #查看当前状态
On branch master
Changes to be committed:
  (use "git reset HEAD ..." 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 ..." to update what will be committed)
  (use "git checkout -- ..." 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 ..." to update what will be committed)
  (use "git checkout -- ..." 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 ..." 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 ..." to include in what will be committed)

        .gitignore

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

28、如何将Git仓库备份到本地
git中常用的传输协议:
四、Git入门与基本使用(4)_第4张图片

哑协议和智能协议的区别:
四、Git入门与基本使用(4)_第5张图片
备份特点:
四、Git入门与基本使用(4)_第6张图片

$ 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'.