点击阅读原文

I spent a little bit of time playing with Git today, specifically the way that the ^ (caret) and ~ (tilde) work and thought I'd document it here in case I forget.

The short version

If you want a deeper explanation skip down to "The long version".

ref~ is shorthand for ref~1 and means the commit's first parent. ref~2 means the commit's first parent's first parent. ref~3 means the commit's first parent's first parent's first parent. And so on.

ref^ is shorthand for ref^1 and means the commit's first parent. But where the two differ is that ref^2 means the commit's second parent (remember, commits can have two parents when they are a merge).

The ^ and ~ operators can be combined.

Here's a diagram showing how to reference various commits using HEAD as the starting point.

The long version

I've created a dummy repository with several commits in it.

 
  $ git log --graph --oneline
* 8329384 Seventh commit
* f5717b0 Merge branch 'my_branch'
|\
| * 956c87d Fourth commit on a branch
* | a8fe411 Sixth commit
|/
* c7c2590 Third commit on a branch
* 86362ff Second commit on a branch
* 748855b First commit on a branch
* 1855b25 Fifth commit
* 67cf3a7 Fourth commit
* ea29778 Third commit
* 28c25b1 Second commit
* cd00b76 First commit
 
 

Starting at the bottom, the early commits were made straight onto master.

The commits starting at 748855b and moving up to c7c2590 were made on a branch and merged into master, but no changes had been made on master in the mean time.

The commits a8fe411 and 956c87d were made on separate branches at the same time. They were merged together in commit f5717b0.

Finally, 8329384 was committed straight onto master.

We can use git show to look at individual commits.

You'll already know that HEAD points to the tip of the current branch:

 
  $ git show --oneline HEAD
8329384 Seventh commit
...
 
 

Putting the caret symbol (^) next to a commit means the parent of that commit. So the following will show the parent of HEAD:

 
  $ git show --oneline HEAD^
f5717b0 Merge branch 'my_branch'
...
 

HEAD^ is shorthand for saying HEAD^1, which literally means show me parent 1 of that commit. You can also say HEAD^2 but in this instance it won't make any sense:

  $ git show --oneline HEAD^2
fatal: ambiguous argument 'HEAD^2': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions
 

Because HEAD only has 1 parent.

But f5717b0, the point where the two branches were merged, has two parents, one on master and one on the branch:

 
  $ git show --oneline f5717b0^1
a8fe411 Sixth commit
... $ git show --oneline f5717b0^2
956c87d Fourth commit on a branch
...
 

The tilde symbol (~) works in a similar way. In fact HEAD~ will reference the same commit as HEAD^:

 
  $ git show --oneline HEAD~
f5717b0 Merge branch 'my_branch'
...
 

Again, HEAD~ is shorthand for HEAD~1, but here this means the first ancestor of HEADHEAD~2 is not the second parent of HEAD but the grandparent of HEAD:

 
  $ git show --oneline HEAD~1
f5717b0 Merge branch 'my_branch'
... $ git show --oneline HEAD~2
a8fe411 Sixth commit
... $ git show --oneline HEAD~3
c7c2590 Third commit on a branch
...
 

As you can see, 956c87d Fourth commit on a branch is not visible when using the tilde operator. This is because the tilde operator always presumes you want to view the first parent's parent.

To access the second parent's parent the tilde and caret symbols can be combined:

 
  $ git show --oneline HEAD~1^1
a8fe411 Sixth commit
... $ git show --oneline HEAD~1^2
956c87d Fourth commit on a branch
...
 

In this way you should be able to reference any commit in your repository's history.

[转] Git caret(^) and tilde(~)的更多相关文章

  1. Git - Tutorial [Lars Vogel]

    From: http://www.vogella.com/tutorials/Git/article.html Git - Tutorial Lars Vogel Version 5.6 Copyri ...

  2. Git - Tutorial官方【转】

    转自:http://www.vogella.com/tutorials/Git/article.html#git_rename_branch Lars Vogel Version 5.8 Copyri ...

  3. What's the difference between HEAD^ and HEAD~ in Git?

    https://stackoverflow.com/questions/2221658/whats-the-difference-between-head-and-head-in-git Rules ...

  4. git cherry-pick简介

    本文编辑整理自: http://sg552.iteye.com/blog/1300713 http://web.mit.edu/bitbucket/git-doc/git-cherry-pick.tx ...

  5. git cherry-pick简介(转载)

    转自:http://hubingforever.blog.163.com/blog/static/1710405792012587115533/ 本文编辑整理自: http://sg552.iteye ...

  6. 进入git diff 界面,无法继续输入命令

    在终端,输入 git diff 文件名  命令之后回车,显示如下界面: 在网上查找,说输入q回车即可退出显示,执行,果然有效,输入h可以显示所有命令 命令如下: SUMMARY OF LESS COM ...

  7. Git命令中波浪号~与脱字符^的区别

    0.前言 波浪号~,英文名叫 tilde.脱字符^,英文名叫caret. 这两种符号常见于git reset的情景,简单的项目结构和操作一般不会涉及到两者之间的区别,似乎用哪个都可以.如果遇到比较繁杂 ...

  8. Git for Windows v2.11.0 Release Notes

    homepage faq contribute bugs questions Git for Windows v2.11.0 Release Notes Latest update: December ...

  9. git diff 理解

    0. 理解 git diff 返回信息 1. 命令 $ git diff README.md 2. 返回信息,注解 diff --git a/README.md b/README.md ## 1. 表 ...

随机推荐

  1. mysql基础1:yum安装mysql

    1.下载yum源并安装http://dev.mysql.com/downloads/repo/yum/.wget https://dev.mysql.com/get/mysql57-community ...

  2. TypeError: Cannot assign to read only property 'exports' of object '#<Object>'

    我的项目在mac上运行的很好,结果windows电脑,就一直报这个错误 解决方案: babel增加 @babel/plugin-transform-modules-commonjs 参考文章: htt ...

  3. linux查看磁盘使用情况命令

    第一:统一磁盘整体情况,包括磁盘大小,已使用,可用1.查看当前目录命令 df -h1结果: 统一每个目录下磁盘的整体情况 2.查看指定目录在命令后直接放目录名,比如查看“usr”目录使用情况: df ...

  4. mvc的视图渲染方式

    ModelAndView ModelAndView vm = new ModelAndView(); //封装要显示在试图上的数据 vm.addObject("msg"," ...

  5. C#命名空间大全

    Microsoft.Jscript Jscript语言进行编译和代码生成的Jscript运行库和类. Microsoft.VisualBasic Visual Basic .NET运行库.此运行库与V ...

  6. [安卓基础] 004.运行app

    运行你的app 这篇课程会教你: 1.如何在设备上运行你的app. 2.如何在模拟器上运行你的app. 当然,在学习之前,你还需要知道: 1.如何使用设备. 2.如何使用模拟器. 3.管理你的项目. ...

  7. Java中的集合(十一) 实现Map接口的TreeMap

    Java中的集合(十一) 实现Map接口的TreeMap 一.TreeMap简介(基于JDK1.8) TreeMap是基于红黑树数据结构,是一个key-value的有序集合,该映射根据其键的自然顺序进 ...

  8. 删除节点与插入节点 & innerHTML

    1.测试removeChild()方法: 删除节点dom9.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" &q ...

  9. PowerPC-Link Command File解析

    https://mp.weixin.qq.com/s/CATWma2mv5IPYGtKZLuGDA   以Code Warrior 11生成的flash版本(FLASH.lcf)为例   一. 参考资 ...

  10. 50个SQL语句(MySQL版) 问题十六

    --------------------------表结构-------------------------- student(StuId,StuName,StuAge,StuSex) 学生表 tea ...