G:\mygitea\GitLearn\learn01 master $ git checkout 012ae46 Note: switching to '012ae46'. # 目前正处于分离头指针的状态 You are in'detached HEAD' state. # 你可以做一些变更,然后产生commit,你也可以把你产生的commit丢弃掉,这并不会对你任何分支文件造成影响 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
G:\mygitea\GitLearn\learn01 HEAD detached at 012ae46 ± $ git status HEAD detached at 012ae46 # 头指针是基于012xxx commit做的,没有挂靠任何分支 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") G:\mygitea\GitLearn\learn01 HEAD detached at 012ae46 ± $ git add .
G:\mygitea\GitLearn\learn01 HEAD detached at 012ae46 ± $ git commit -m "change background color" [detached HEAD 9fa98d6] change background color 1 file changed, 1 insertion(+), 1 deletion(-)
G:\mygitea\GitLearn\learn01 HEAD detached at 9fa98d6 $ git log commit 9fa98d6eb27abdf05064e5ced0c4400a6ca847b5 (HEAD) Author: Jabari <innocenfox@gmail.com> Date: Tue May 10 19:41:34 2022 +0800
G:\mygitea\GitLearn\learn01 HEAD detached at 9fa98d6 $ git checkout master Warning: you are leaving 1 commit behind, not connected to any of your branches:
9fa98d6 change background color
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> 9fa98d6
Switched to branch 'master' Your branch is ahead of 'origin/master' by 2 commits. (use "git push" to publish your local commits)
G:\mygitea\GitLearn\learn03 $ git log --oneline 41f7a2f (HEAD -> master) Second commit dab1693 Second commit 8653890 First commit
G:\mygitea\GitLearn\learn03 $ git checkout dab1693 Note: switching to 'dab1693'. You are in'detached HEAD' state. You can look around, make experimental...
G:\mygitea\GitLearn\learn03 $ git status HEAD detached at dab1693 nothing to commit, working tree clean
G:\mygitea\GitLearn\learn03 $ vim readme
G:\mygitea\GitLearn\learn03 HEAD detached at dab1693 ± $ git add . && git commit -m"older-x-young" [detached HEAD 8fbb99f] older-x-young 1 file changed, 1 insertion(+)
G:\mygitea\GitLearn\learn03 HEAD detached at 8fbb99f $ git branch older-x-young 8fbb99f
G:\mygitea\GitLearn\learn03 master $ cat readme This is the oldest branch. This is the older branch. This is the young branch.
G:\mygitea\GitLearn\learn03 master $ git diff HEAD HEAD~ diff --git a/readme b/readme index a5a8dc1..b7c8a93 100644 --- a/readme +++ b/readme @@ -1,3 +1,2 @@ This is the oldest branch. This is the older branch. -This is the young branch.
G:\mygitea\GitLearn\learn03 master $ git diff HEAD HEAD~1 diff --git a/readme b/readme index a5a8dc1..b7c8a93 100644 --- a/readme +++ b/readme @@ -1,3 +1,2 @@ This is the oldest branch. This is the older branch. -This is the young branch.
G:\mygitea\GitLearn\learn03 master $ git diff HEAD HEAD~~ diff --git a/readme b/readme index a5a8dc1..fbfe1a7 100644 --- a/readme +++ b/readme @@ -1,3 +1 @@ This is the oldest branch. -This is the older branch. -This is the young branch.
G:\mygitea\GitLearn\learn03 master $ git diff HEAD HEAD~2 diff --git a/readme b/readme index a5a8dc1..fbfe1a7 100644 --- a/readme +++ b/readme @@ -1,3 +1 @@ This is the oldest branch. -This is the older branch. -This is the young branch.
G:\mygitea\GitLearn\learn03 master $ git diff HEAD HEAD~3 fatal: ambiguous argument 'HEAD~3': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git <command> [<revision>...] -- [<file>...]'