[转] 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. 表 ...
随机推荐
- Windows系统下pthread环境配置
记录下win7系统,vc6.0++编译器下配置POSIX多线程环境的步骤. 配置 下载地址 ftp://sourceware.org/pub/pthreads-win32/ 我下载的版本是 fpthr ...
- tomcat关于配置servlet的url-pattern的问题详解
目录 1 servlet url-pattern的匹配问题 1.1 精确匹配 1.2 路径匹配 1.3 后缀匹配 注意:路径和后缀匹配无法同时设置 2 url-pattern中/和/*的区别 3 ur ...
- pdf去水印,pdf解密,pdf转MarkDown
pdf去水印,在转Markdown文件 首先我们要有版权的敬畏之心,这里只是给大家介绍一下思路,请合理使用! 1.pdf去水印 下载:悦书PDF阅读器,注意免费免费!!!!(后期就不知道了,目前是免费 ...
- [CSS工具推荐]0001.推荐 10 个超棒的 CSS3 代码生成工具
引言:新的在线工具和 WebApp 帮助开发者快速地创建网站而不用写代码.前端开发已经在框架和代码库方面有了很大的进展. 现在许多开发者已经忘记了代码生成器在构建网站时的价值.下面的资源是完全免费的 ...
- pandas DataFrame 的横向纵向拼接组合
concat 与其说是连接,更准确的说是拼接.就是把两个表直接合在一起.于是有一个突出的问题,是横向拼接还是纵向拼接,所以concat 函数的关键参数是axis . 函数的具体参数是: concat( ...
- Linux目录遍历opendir()
头文件:#include<dirent.h> DIR *opendir(const char *dirname); 打开目录 struct dirent *readdir(DIR *dir ...
- uni-app之uni.showToast()image路径问题
uni-app的API中,showToast的icon值只有success,loading,none三种显示,失败没有图标.如果失败时需要显示图标,就要用到自定义图标 image 了. uni.sho ...
- 使用VUE开发用户后台时的动态路由问题、按钮权限问题以及其他页面处理问题
如今前后端分离是大势所趋,笔者虽然是做后台的,但也不得不学学前端的流行框架VUE -_-||| . 为了学习VUE,笔者搭建了一个简单的用户后台,以此来了解VUE的开发思路(注:本项目不用于实际开发, ...
- Beta冲刺 —— 5.29
这个作业属于哪个课程 软件工程 这个作业要求在哪里 Beta冲刺 这个作业的目标 Beta冲刺 作业正文 正文 github链接 项目地址 其他参考文献 无 一.会议内容 1.展示了每个人当天的成果. ...
- Rocket - debug - Periphery
https://mp.weixin.qq.com/s/uGxn-Xec0LkwdaSsCtQBvw 简单介绍Periphery的实现. 1. ExportDebugDMI/ExportDebugJTA ...