[转] 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. 表 ...
随机推荐
- zz MySQL redo log及recover过程浅析
原作地址:http://www.cnblogs.com/liuhao/p/3714012.html 写在前面:作者水平有限,欢迎不吝赐教,一切以最新源码为准. InnoDB redo log 首先介绍 ...
- jsp获取当前日期
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <jsp: ...
- POJ-2488 国际象棋马的走法 (深度优先搜索和回溯)
#include <stdio.h> #define MAX 27 void dfs(int i, int j); int dx[8] = {-1, 1, -2, 2, -2, 2, -1 ...
- ajax 请求PHP返回json格式的处理
php返回代码格式 public function json(){ if (request()->isAjax()){ $data = [ 'code'=>'1', 'msg'=>' ...
- WordPress批量更换域名
UPDATE wp_options SET option_value = replace( option_value, 'http://www.old.com', 'http://www.new.co ...
- npm audit fix 报错
found 2504 vulnerabilities (1360 low, 1109 moderate, 29 high, 6 critical) run `npm audit fix` to fi ...
- Flexible 应用
Flexibl.js 为我们做了一项工作,媒体查询工作,节约了许多操作 举个例子,移动端的页面设计稿是750px,我们自己换算rem单位,比如我想把屏幕划分为15等份,我就750/15=50,然后用所 ...
- 【JUC】CountDownLatch和Java枚举的使用例子
public enum CountryEnum { ONE(1,"春"), TWO(2,"夏"), THREE(3,"秋"), FOUR(4 ...
- ngnix随笔二
ngnix配置文件 1.rpm -ql nginx /etc/logrotate.d/nginx /etc/nginx /etc/nginx/conf.d /etc/nginx/conf.d/defa ...
- pyinstaller打包pyqt5,从入坑到填坑,详解
以上省略pyinstaller安装步骤,直入主题.先分享我的心路历程. 1.pyinstaller -F -i 1.ico UI_Main.py (先在CMD中 cd到 py文件对应的路径) 第一步打 ...