[转] Git caret(^) and tilde(~)
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.
|
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:
|
Putting the caret symbol (^) next to a commit means the parent of that commit. So the following will show the parent of HEAD:
|
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:
|
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:
|
The tilde symbol (~) works in a similar way. In fact HEAD~ will reference the same commit as HEAD^:
|
Again, HEAD~ is shorthand for HEAD~1, but here this means the first ancestor of HEAD – HEAD~2 is not the second parent of HEAD but the grandparent of HEAD:
|
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:
|
In this way you should be able to reference any commit in your repository's history.
[转] Git caret(^) and tilde(~)的更多相关文章
- Git - Tutorial [Lars Vogel]
From: http://www.vogella.com/tutorials/Git/article.html Git - Tutorial Lars Vogel Version 5.6 Copyri ...
- Git - Tutorial官方【转】
转自:http://www.vogella.com/tutorials/Git/article.html#git_rename_branch Lars Vogel Version 5.8 Copyri ...
- 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 ...
- git cherry-pick简介
本文编辑整理自: http://sg552.iteye.com/blog/1300713 http://web.mit.edu/bitbucket/git-doc/git-cherry-pick.tx ...
- git cherry-pick简介(转载)
转自:http://hubingforever.blog.163.com/blog/static/1710405792012587115533/ 本文编辑整理自: http://sg552.iteye ...
- 进入git diff 界面,无法继续输入命令
在终端,输入 git diff 文件名 命令之后回车,显示如下界面: 在网上查找,说输入q回车即可退出显示,执行,果然有效,输入h可以显示所有命令 命令如下: SUMMARY OF LESS COM ...
- Git命令中波浪号~与脱字符^的区别
0.前言 波浪号~,英文名叫 tilde.脱字符^,英文名叫caret. 这两种符号常见于git reset的情景,简单的项目结构和操作一般不会涉及到两者之间的区别,似乎用哪个都可以.如果遇到比较繁杂 ...
- Git for Windows v2.11.0 Release Notes
homepage faq contribute bugs questions Git for Windows v2.11.0 Release Notes Latest update: December ...
- git diff 理解
0. 理解 git diff 返回信息 1. 命令 $ git diff README.md 2. 返回信息,注解 diff --git a/README.md b/README.md ## 1. 表 ...
随机推荐
- 关于MYSQL 和INNODB的逻辑关系图。最好的理解是一点点动手做,观察,记录,思考。
每隔0.1秒就刷一次MYSQL文件的变化,并闪动标示出来,以观察SQL执行时,MYSQL的处理顺序. watch -n 0.1 -d stat /var/lib/mysql/ib_logfile0 / ...
- 朱刘算法 有向图定根的最小生成树poj3164
关于为什么不能用Prim求解此类问题,如下 Prim可以看成是维护两个顶点集或者看成维护一颗不断生成的树(感觉前一种说法好一点) 倘若是有向图有三个顶点1.2.3 边的情况如下 1->2: ...
- POJ2991
题目链接:https://vjudge.net/problem/POJ-2991 知识准备: 1.向量旋转公式:向量(x,y)逆时针旋转角度A,则旋转后的向量为(x*cos A-y*sin A, x* ...
- AspectJ JoinPoint及ProceedingJoinPoint 简要api文档
AspectJ使用org.aspectj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强时,使用org.aspectj.lang.ProceedingJoinPoint表示连接点 ...
- JS最通俗易懂简易轮播实现
轮播图作为前端比较简易的动画,使用非常频繁,这里记录以便使用 此轮播图为最简易自动播放,非无缝,无按钮,无缩略图和序号 想看全套轮播图可以查看我的分类轮播图全套 html 布局 <div sty ...
- [JavaWeb基础] 003.JAVA访问Mysql数据库
上面两篇讲解了简单的JSP + Servlet的搭建和请求,那么后面我们肯定要用到数据交互,也就是操纵数据库的数据,包括对数字的增加,删除,修改,查询.我们就用简单的MySql来做例子 我们需要引入驱 ...
- Java IO(二)File
Java IO(二)File 一.概述 在 Java 中,File 类是 java.io 包中唯一代表磁盘文件本身的对象,也就是说,如果希望在程序中操作文件和目录,则都可以通过 File 类来完成.F ...
- 慕零的黑夜-头条-第一期(必问)[导读:]1.CSDN必问赏金流向何方 2.CSDN必问偷偷做的手脚 3.CSDN必问靠谱吗 4.关于钱于回答的平衡问题:一美元拍卖骗局 作者:qq3461896724
本期是关于CSDN 必问 (biwen.csdn.net)的内容,欢迎评论 文末,文中插入有 小姐姐 img(附py代码,1.49G) + coding资料 哟~~~ 你看到这篇很可能是在提问.推荐加 ...
- Library source does not match the bytecode for class 最佳解决方案
首先分析问题 打完的jar包,编译的后class跟java文件不一致,原因是重新打包后还是引用之前的java文件,不能重新加载新生成的jar. 解决方案 方案一 IDEA 工具,点击File > ...
- 14 . Python3之MysSQL
数据库概念 数据库: 按照数据结构来组织.存储.管理数据的仓库` 诞生 计算机的发明是为了做科学计算的,而科学计算需要大量的输入和输出. 早期,可以使用打孔卡片的孔.灯泡的亮灭表示数据输入,输出. 后 ...