IV. introduction and basic use of Git

16. Organize consecutive multiple commit into one

commit ce587039661c88fd508035fd103a012e33c057ac (HEAD -> temp)
Author: Jone <764651475@qq.com>
Date:   Thu Mar 14 17:03:07 2019 +0800

    Update fourth file.txt

commit bfd373ab1dd5b2d578bac9cacd4d89d0066c51af
Author: Jone <764651475@qq.com>
Date:   Thu Mar 14 17:01:46 2019 +0800

    Add fouth file.txt

commit b843c287804d2b5886167740f9e6c0d327540ee1 (tag: 03tag)
Author: Jone <764651475@qq.com>
Date:   Thu Mar 14 17:00:21 2019 +0800

    Add third file

commit 0bd98cb5d0d969cfc35d8c5a16d33b5924cbc6b0
Author: Jone <764651475@qq.com>
Date:   Thu Mar 14 16:59:25 2019 +0800

    Add second file

In the above commit, if you want to synthesize the first three commit into one, you can use the following operations:

$ git rebase -i 0bd98cb5d0d969        #Merge the first three 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                                      #Combined results
commit b0fc95597b4be1e1c11f94bb77931f0338b581bf (HEAD -> temp)
Author: Jone <764651475@qq.com>
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 <764651475@qq.com>
Date:   Thu Mar 14 16:59:25 2019 +0800

    Add second file

commit c8588e43dd1053684632871fb8aec1945ee6a6ab
Author: Jone <764651475@qq.com>
Date:   Thu Mar 14 16:36:00 2019 +0800

    Add first file

Operation after entering the editing interface:


17. Organize the several commit ments in the interval into one

$ git log
commit 5d63d9384d28a5bf4786bc5639fc3dbc58cc2fc8 (HEAD -> master)
Author: Jone <764651475@qq.com>
Date:   Fri Mar 15 17:46:32 2019 +0800

    move first.txt to first.md

commit 7376bc5b2ebc3e13d4c4552ebdef348a17cd4eef
Author: Jone <764651475@qq.com>
Date:   Thu Mar 14 17:03:07 2019 +0800

    Update fourth file

commit 1d63ec82259b237f58e7525ccf856a03fb880fcd
Author: Jone <764651475@qq.com>
Date:   Thu Mar 14 17:01:46 2019 +0800

    Add fouth file

commit b843c287804d2b5886167740f9e6c0d327540ee1 (tag: 03tag)
Author: Jone <764651475@qq.com>
Date:   Thu Mar 14 17:00:21 2019 +0800

    Add third file

commit 0bd98cb5d0d969cfc35d8c5a16d33b5924cbc6b0
Author: Jone <764651475@qq.com>
Date:   Thu Mar 14 16:59:25 2019 +0800

    Add second file

According to the above log, synthesize the first / second / fourth commit into one. The operation is as follows:

$ 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 <764651475@qq.com>
Date:   Thu Mar 14 17:01:46 2019 +0800

    Add fouth file

commit e0326fb0984785866419d9125a8a7427f6a8a765
Author: Jone <764651475@qq.com>
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 <764651475@qq.com>
Date:   Thu Mar 14 16:59:25 2019 +0800

    Add second file

18. Compare the difference between the files contained in the staging area and the HEAD

$ echo "Update" >> fourth.txt     #Modify file

$ git add fourth.txt                     #Add files to staging area

$ git diff --cached                    #Compare the difference between HEAD and staging area
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. Compare the differences between the files in the workspace and the staging area

$ vim fourth.txt    #Modify workspace file

$ git diff               #Compare workspace and staging files
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. Restore the temporary storage area to the same as the HEAD

$ git diff --cached      #Compare file differences
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      #Recover staging area as HEAD
Unstaged changes after reset:
M       fourth.txt

$ git diff --cached      #compare

21. How to restore the files in the workspace to be the same as the staging area

$ git status                       #Currently, there are two files in the staging area that do not have a commit
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   first.md
        modified:   fourth.txt

$ vim first.md             #Modify workspace file

$ 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                #Recovering workspace files is the same as staging files

$ git diff                            #Comparative differences

22. How to cancel the change of some files in the staging area (restore some files in the staging area to be consistent with the HEAD)

$ git status                  #current state
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     #Restore the first? MD file to be consistent with the 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. Eliminate recent submissions

$ git log    #Current commit information
commit 25efd88365c4b9c31634a8bb06d25fe23109eb22 (HEAD -> master)
Author: Jone <764651475@qq.com>
Date:   Thu Mar 14 17:01:46 2019 +0800

    Add fouth file

commit e0326fb0984785866419d9125a8a7427f6a8a765
Author: Jone <764651475@qq.com>
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 <764651475@qq.com>
Date:   Thu Mar 14 16:59:25 2019 +0800

    Add second file

commit c8588e43dd1053684632871fb8aec1945ee6a6ab

$ git reset --hard  e0326fb0984785866419d    #Back to the last commit
HEAD is now at e0326fb Merge three commits

$ git log
commit e0326fb0984785866419d9125a8a7427f6a8a765 (HEAD -> master)
Author: Jone <764651475@qq.com>
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 <764651475@qq.com>
Date:   Thu Mar 14 16:59:25 2019 +0800

    Add second file

commit c8588e43dd1053684632871fb8aec1945ee6a6ab
Author: Jone <764651475@qq.com>
Date:   Thu Mar 14 16:36:00 2019 +0800

    Add first file

24. Differences in specified documents submitted by different parties

$ git log --all --graph  -n2
* commit e0326fb0984785866419d9125a8a7427f6a8a765 (HEAD -> master)
| Author: Jone <764651475@qq.com>
| 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 <764651475@qq.com>
|   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     #Compare the differences between the two branches
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    #You can also use the commit comparison corresponding to the branch, or you can compare the specified file
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. Correct method of deleting files

$ 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. How to deal with the emergency tasks temporarily added in the development (the method of temporarily switching other branches and saving the current environment)

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

        deleted:    first.md

$ git stash               #Saves the current state to the stack
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      #You can use the apply command to restore the environment, but there will be a backup in the 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     #You can also use the pop command to restore the environment, but at this time, the stash backup is no longer retained in the 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. How to specify files that do not need Git management

nxf42573@NXW53034 MINGW64 /d/git_learning (master)
$ echo "Hello word!" > read.txt      #New 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    #Create a new. gitignore file. Specify that git does not manage the read.txt file

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. How to backup Git warehouse to local
Commonly used transmission protocols in git:

Differences between dumb protocol and intelligent protocol:

Backup features:

$ pwd
/d/bak

$ git clone --bare /d/git_learning/.git    ya.git    #Dumb protocol backup
Cloning into bare repository 'ya.git'...
done.

nxf42573@NXW53034 MINGW64 /d/bak
$ git clone --bare file:///D / git learning /. Git zhining.git intelligent protocol
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? Adding a remote warehouse

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

$ git checkout -b Jone                        #Add new branch
Switched to a new branch 'Jone'

$ git branch
* Jone
  master
  temp

$ git push zhineng     #Push the branch to a remote warehouse
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'.

Tags: Programming git vim

Posted on Mon, 02 Dec 2019 13:35:57 -0500 by pstevereynolds