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 switching back to a branch.
If you want to create a new branch to retain commits you create, you may do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at 78be697 Add style.css
$ git log --all --graph --oneline * fee6bac (main) Modify style.css on branch main | * 97b461a (tmp) Add test to branch tmp |/ * a0b98ea Move readme.md to readme * 63ca733 Add refering projects * f77ad61 (tag: js01) Add js * 78be697 (HEAD) Add style.css * f51e117 Add index _ log * 97ad728 (origin/main, origin/HEAD) Initial commit
$ git status HEAD detached at 78be697 Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: styles/style.css
no changes added to commit (use "git add" and/or "git commit -a") $ git add -u $ git commit -m "Background to yellow" $ git log --all --graph --oneline * a5a8831 (HEAD) Background to yellow | * fee6bac (main) Modify style.css on branch main | | * 97b461a (tmp) Add test to branch tmp | |/ | * a0b98ea Move readme.md to readme | * 63ca733 Add refering projects | * f77ad61 (tag: js01) Add js |/ * 78be697 Add style.css * f51e117 Add index _ log * 97ad728 (origin/main, origin/HEAD) Initial commit
$ git branch * (HEAD detached from 78be697) main tmp
-d –delete Delete a branch. The branch must be fully merged in its upstream branch, or in HEAD if no upstream was set with –track or –set-upstream-to. -D Shortcut for –delete –force
怎么修改最新commit的message
1 2 3 4 5 6 7 8 9 10 11 12 13
$ git log --all --graph --oneline -n2 * a5a8831 (fix_css) Background to yellow | * fee6bac (HEAD -> main) Modify style.css on branch main
$ git log --all --graph --oneline -n2 * 7f04fb2 (HEAD -> main) Modify style.css on branch main amend | * a5a8831 (fix_css) Background to yellow
怎么修改老旧的commit的message
1 2 3 4 5 6
$ git log --all --graph --oneline -n6 * fb1ece1 (HEAD -> main) Add div class style * 167491f Add green style.css * f51e117 Add index _ log * 97ad728 (origin/main, origin/HEAD) Initial commit
如果我想变更167491f Add green style.css这一次commit的message,从“Add green style.css”变更为“Add red style.css”,可以使用它的父commitf51e117做rebase操作: (tips:如果该commit没有父commit,即是第一个commit,我们可以对选用它的commitID做rebase操作,然后在下面的弹出内容里,把该commit添加进去即可,然后再选择对应的commands)
$ git rebase -i f51e117 # -i 交互模式 # 弹出以下内容 pick 167491f Add green style.css pick fb1ece1 Add div class style
# Rebase 97ad728..63ca733 onto 97ad728 (4 commands) # # Commands: # p, pick <commit> = use commit # r, reword <commit> = use commit, but edit the commit message # e, edit <commit> = use commit, but stop for amending # s, squash <commit> = use commit, but meld into previous commit # f, fixup [-C | -c] <commit> = like "squash" but keep only the previous # commit's log message, unless -C is used, in which case # keep only this commit's message; -c is same as -C but # opens the editor # x, exec <command> = run command (the rest of the line) using shell # b, break = stop here (continue rebase later with 'git rebase --continue') # d, drop <commit> = remove commit # l, label <label> = label current HEAD with a name # t, reset <label> = reset HEAD to a label # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>] # . create a merge commit using the original merge commit's # . message (or the oneline, if no original merge commit was # . specified); use -c <commit> to reword the commit message # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted.
# Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Date: Wed Sep 7 11:20:39 2022 +0800 # # interactive rebase in progress; onto 63ca733 # Last commanddone (1 commanddone): # reword a0b98ea Move readme.md to readme # Next command to do (1 remaining command): # pick 7f04fb2 Modify style.css on branch main amend # You are currently editing a commit while rebasing branch 'main' on '63ca733'. # # Changes to be committed: # renamed: README.md -> README # # Untracked files: # .gitignore #
message修改完之后,再次保存退出,将自动弹出:
1 2 3 4 5 6
$ git rebase -i f51e117 [detached HEAD 68c45d1] Add red style.css Date: Tue Sep 6 10:59:30 2022 +0800 1 file changed, 50 insertions(+) create mode 100644 styles/style.css Successfully rebased and updated refs/heads/main.
我们再次查看log:
1 2 3 4 5 6
git log --all --graph --oneline -n6 * 7db15bd (HEAD -> main) Add div class style * 68c45d1 Add red style.css * f51e117 Add index _ log * 97ad728 (origin/main, origin/HEAD) Initial commit
对比开始之前的log,已经完成了message的修改,只不过commit的ID发生了变更。 (注意,当存在多个分支时,修改多个分支共同路径上的commit的message,将从该commit的父commit开始发生裂变,当前分支该commit的message发生了修改,其他分支该commit的message保持不变) 修改f51e117 Add index _ log前:
1 2 3 4 5 6 7 8
* fd46298 (HEAD -> b1) Add style to b1 | * 72c72b5 (main) Add li to index |/ * 7db15bd Add div class style * 68c45d1 Add red style.css * f51e117 Add index _ log * 97ad728 (origin/main, origin/HEAD) Initial commit
修改f51e117 Add index _ log后:
1 2 3 4 5 6 7 8 9 10
* 6f4263f (HEAD -> b1) Add style to b1 * 29ec5c3 Add div class style * f3f7003 Add red style.css * 8128f86 Add index _ log after init | * 72c72b5 (main) Add li to index | * 7db15bd Add div class style | * 68c45d1 Add red style.css | * f51e117 Add index _ log |/ * 97ad728 (origin/main, origin/HEAD) Initial commit
git rebase变基操作应该只在自己开发的分支上进行,所以通过git rebase -i commitID 修改旧的commit的message应该仅限于自己分支,不要对团队集成路径上/共同路径上的commit的message做变更,以免出现不可预期的trouble.
怎么把多个连续的commit合并成一个commit
1 2 3 4 5 6 7 8
$ git log --all --graph --oneline * a3d26dc (HEAD -> main) Add 3 div to index * bac9977 Add 2 div to index * 2fab0c5 Add 1 div to index * 7db15bd Add div class style * 68c45d1 Add red style.css * f51e117 Add index _ log * 97ad728 (origin/main, origin/HEAD) Initial commit
最近的三次comimt都是对index文件的操作,我们可以考虑把它们合并一下:
1
$ git rebase -i 7db15bd
弹出页面:
1 2 3 4 5
pick 2fab0c5 Add 1 div to index pick bac9977 Add 2 div to index pick a3d26dc Add 3 div to index
# Rebase 7db15bd..a3d26dc onto 7db15bd (3 commands)
修改为:
1 2 3 4 5
pick 2fab0c5 Add 1 div to index s bac9977 Add 2 div to index s a3d26dc Add 3 div to index
# Rebase 7db15bd..a3d26dc onto 7db15bd (3 commands)
# This is a combination of 3 commits. # This is the 1st commit message:
Add 1 div to index
# This is the commit message #2:
Add 2 div to index
# This is the commit message #3:
Add 3 div to index
# Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Date: Fri Sep 9 14:13:08 2022 +0800 # # interactive rebase in progress; onto 7db15bd # Last commands done (3 commands done): # squash bac9977 Add 2 div to index # squash a3d26dc Add 3 div to index # No commands remaining. # You are currently rebasing branch 'main' on '7db15bd'. # # Changes to be committed: # modified: index.html
在# This is a combination of 3 commits.下面添加合并成一个commit的message:
# This is a combination of 3 commits. Add total 3 div to index,a combination commit message # This is the 1st commit message:
Add 1 div to index
# This is the commit message #2:
Add 2 div to index
# This is the commit message #3:
Add 3 div to index
# Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Date: Fri Sep 9 14:13:08 2022 +0800 # # interactive rebase in progress; onto 7db15bd # Last commands done (3 commands done): # squash bac9977 Add 2 div to index # squash a3d26dc Add 3 div to index # No commands remaining. # You are currently rebasing branch 'main' on '7db15bd'.
保存退出:
1 2 3 4 5
$ git rebase -i 7db15bd [detached HEAD 35a3dea] Add total 3 div to index,a combination commit message Date: Fri Sep 9 14:13:08 2022 +0800 1 file changed, 15 insertions(+) Successfully rebased and updated refs/heads/main.
查看log,完成commit合并:
1 2 3 4 5 6
$ git log --all --graph --oneline * 35a3dea (HEAD -> main) Add total 3 div to index,a combination commit message * 7db15bd Add div class style * 68c45d1 Add red style.css * f51e117 Add index _ log * 97ad728 (origin/main, origin/HEAD) Initial commit
怎么把多个不连续的commit合并成一个commit
1 2 3 4 5 6 7 8
$ git log --all --graph --oneline * 6e38539 (HEAD -> main) Add new annotation to index.html * b7e7353 Add gree style.css * 35a3dea Add total 3 div to index,a combination commit message * 7db15bd Add div class style * 68c45d1 Add red style.css * f51e117 Add index _ log * 97ad728 (origin/main, origin/HEAD) Initial commit
b7e7353 Add gree style.css和68c45d1 Add red style.css都是对style.css的修改,考虑合并成一个commit:
1
$ git rebase -i f51e117
弹出:
1 2 3 4 5
pick 68c45d1 Add red style.css pick 7db15bd Add div class style pick 35a3dea Add total 3 div to index,a combination commit message pick b7e7353 Add gree style.css pick 6e38539 Add new annotation to index.html
修改为:
1 2 3 4 5
pick 68c45d1 Add red style.css s b7e7353 Add gree style.css pick 7db15bd Add div class style pick 35a3dea Add total 3 div to index,a combination commit message pick 6e38539 Add new annotation to index.html
保存退出(对于不连续的commit做合并,大概率会遇到冲突,如果有冲突,则解决完文件冲突后使用git add -u 和git rebase --continue),弹出:
# This is a combination of 2 commits. # 在此处添加合并后的message Add some color style.css,combination message of 2 commits # This is the 1st commit message:
Add red style.css
# This is the commit message #2:
Add gree style.css
# Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Date: Tue Sep 6 10:59:30 2022 +0800 # # interactive rebase in progress; onto f51e117 # Last commands done (2 commands done): # pick 68c45d1 Add red style.css # squash b7e7353 Add gree style.css # Next commands to do (3 remaining commands): # pick 7db15bd Add div class style # pick 35a3dea Add total 3 div to index,a combination commit message # You are currently rebasing branch 'main' on 'f51e117'.
查看合并结果:
1 2 3 4 5 6 7
$ git log --all --graph --oneline * d3a8e50 (HEAD -> main) Add new annotation to index.html * ad7fa4c Add total 3 div to index,a combination commit message * 6a384dd Add div class style * ebd0755 Add some color style.css,combination message of 2 commits * f51e117 Add index _ log * 97ad728 (origin/main, origin/HEAD) Initial commit