11、分离头指针情况下的注意事项
所谓分离头指针,即在git切换到某一commit时,没有绑定在分支或者tag上,此时如果在该commit下进行了文件修改,并且提交commit时,git在日后清理该提交而不保存,因此在进行commit查看时,最好绑定在某一branch或者tag上操作,当然这种操作的好处也存在,即不会破坏原有的branch环境。
$ git checkout 0bd98cb5d0d969cfc35d #直接切换到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
HEAD is now at 0bd98cb Add second file
$ git status #此时状态,分离头指针状态
HEAD detached at 0bd98cb
nothing to commit, working tree clean
$ echo "Update second file " >> second.txt #在分离头指针状态下,修改文件并提交
$ 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 71faae4
Switched to branch 'master'
$ gitk #查看在分离头指针下的提交,可以发现git没有保存
$ git branch newsecond 71faae4 #将修改的commit绑定到一个新的分支
$ gitk #查看此时提交,可以发现git已经保存刚才的commit
12、进一步理解HEAD和branch
HEAD无论是在某一分支或者tag其,最终都会是在某一commit上。同时使用HEAD可以指代当前的commit。
$ cat .git/refs/heads/temp
7376bc5b2ebc3e13d4c4552ebdef348a17cd4eef
$ git cat-file -t 7376bc5b2
commit
13、怎么删除不需要的分支
$ 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、怎么修改最新commit的message
$ git log -n1 #修改前
commit 7376bc5b2ebc3e13d4c4552ebdef348a17cd4eef (HEAD -> temp)
Author: Jone <764651475@qq.com>
Date: Thu Mar 14 17:03:07 2019 +0800
Update fourth file
$ git commit --amend #使用该命令进入vim编辑界面直接修改即可
[temp 097d397] Update fourth file.txt
Date: Thu Mar 14 17:03:07 2019 +0800
1 file changed, 1 insertion(+)
$ git log -n1 #修改后
commit 097d397f672da771da95cf9baf49057047846617 (HEAD -> temp)
Author: Jone <764651475@qq.com>
Date: Thu Mar 14 17:03:07 2019 +0800
Update fourth file.txt
15、怎么修改老旧commit的message
$ git log -n3 #要修改导入第二个commit的message
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 #选择倒数第二个commit的 base信息,即导入第三个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.
第一张图为需要修改的信息,将pick改为r即可
第二张图为修改后的commit 信息
$ git log -n3 #查看修改后的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
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。