III. introduction and basic use of Git

11. Precautions in case of separating head pointer
  the so-called separate header pointer, that is, when git switches to a commit, it is not bound to the branch or tag. At this time, if the file is modified under the commit, and when the commit is submitted, GIT will clean up the commit in the future without saving. Therefore, it is better to bind to a branch or tag for operation when checking the commit. Of course, the advantage of this operation also exists, that is, it will not break Bad original branch environment.

$ git checkout 0bd98cb5d0d969cfc35d   #Switch directly to commit
Note: checking out '0bd98cb5d0d969cfc35d'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 0bd98cb Add second file

$ git status                                        #At this time, the separation head pointer state
HEAD detached at 0bd98cb
nothing to commit, working tree clean

$ echo "Update second file " >> second.txt   #In the state of separated header pointer, modify the file and submit it

$ git commit -am"Update second file"
[detached HEAD 71faae4] Update second file
 1 file changed, 1 insertion(+)

 $ git checkout master
Warning: you are leaving 1 commit behind, not connected to
any of your branches:

  71faae4 Update second file

If you want to keep it by creating a new branch, this may be a good time
to do so with:

 git branch <new-branch-name> 71faae4

Switched to branch 'master'

$ gitk    #Check the commit under the separate header pointer, and you can see that git is not saved

$ git branch newsecond 71faae4     #Bind the modified commit to a new branch

$ gitk     #Check the commit at this time. You can see that git has saved the commit just now

12. Further understanding of HEAD and branch
  whether the HEAD is in a branch or tag, it will eventually be on a commit. At the same time, using HEAD can refer to the current commit.

$ cat .git/refs/heads/temp
7376bc5b2ebc3e13d4c4552ebdef348a17cd4eef

$ git cat-file -t 7376bc5b2
commit

13. How to delete unnecessary branches

$ git branch
  master
  newsecond
* temp

$ git branch -d newsecond
error: The branch 'newsecond' is not fully merged.
If you are sure you want to delete it, run 'git branch -D newsecond'.

$ git branch -D newsecond
Deleted branch newsecond (was 71faae4).

$ git branch
  master
* temp

14. How to modify the latest commit message

$ git log -n1      #Before modification
commit 7376bc5b2ebc3e13d4c4552ebdef348a17cd4eef (HEAD -> temp)
Author: Jone <764651475@qq.com>
Date:   Thu Mar 14 17:03:07 2019 +0800

    Update fourth file

$ git commit --amend                  #Use this command to enter the vim editing interface and modify it directly
[temp 097d397] Update fourth file.txt
 Date: Thu Mar 14 17:03:07 2019 +0800
 1 file changed, 1 insertion(+)

$ git log -n1            #After modification
commit 097d397f672da771da95cf9baf49057047846617 (HEAD -> temp)
Author: Jone <764651475@qq.com>
Date:   Thu Mar 14 17:03:07 2019 +0800

    Update fourth file.txt

15. How to modify the old commit message

$ git log -n3     #To modify the message to import the second commit
commit 097d397f672da771da95cf9baf49057047846617 (HEAD -> temp)
Author: Jone <764651475@qq.com>
Date:   Thu Mar 14 17:03:07 2019 +0800

    Update fourth file.txt

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

$ git rebase -i b843c287804d2b588616    #Select the base information of the penultimate commit, that is, import the third commit
[detached HEAD bfd373a] Add fouth file.txt
 Date: Thu Mar 14 17:01:46 2019 +0800
 1 file changed, 1 insertion(+)
 create mode 100644 fourth.txt
Successfully rebased and updated refs/heads/temp.

The first picture is the information to be modified. Just change the pick to r

The second figure shows the modified commit information

$ git log -n3     #View the modified message
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

Tags: Programming git vim

Posted on Sun, 01 Dec 2019 18:56:37 -0800 by Worqy