点击阅读原文

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. oracle关于rownum的使用【oracle】

    转自:https://blog.csdn.net/qiuzhi__ke/article/details/78892822 关于rownum是怎么产生的(网上有不少的文章,下面是摘录): rownum是 ...

  2. Jquery学习2---倒计时

    以下代码是mvc4.0代码,其功能是让页面上的数字3,变2,变1 然后跳转页面 @{ ViewBag.Title = "LoginOut"; } <html> < ...

  3. poj1966枚举源汇点 求最小点割DInic

    Cable TV Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4854   Accepted: 2241 ...

  4. Poj 2109 k^n = p.

    Poj2109(1)和Poj2109(2)这两种解答都是有漏洞的,就是解不一定存在. 当然这种漏洞的存在取决于出题人是否假设输入的n,p必须默认有kn = p这样的关系存在. 这道题可以详细看http ...

  5. Poj2965 冰箱的开关

    #include<iostream> using namespace std; int flag; int step; ][]; ] = { }; ] = { }; void turn(i ...

  6. 《机器学习_08_代价敏感学习_添加sample_weight支持》

    简介 这一节主要是为模型打补丁,在这之前笔者已经介绍并实现了几种典型的机器学习模型,比如线性回归.logistic回归.最大熵.感知机.svm等,但目前它们都有一个共性,那就是构造的损失函数对每个样本 ...

  7. switch下返回各类的数值

    定义一个变量,在每个case下赋值,最后return public static int orderDishes(int choice) { int price = 0; switch (choice ...

  8. 前端自动化构建之gulp

    前言 之前学完html的基础后就去学js框架了,每次都是用脚手架搭好的文件,在无形中体验了一波前端自动化带来的方便.然后前一段时间才开始学习前端自动化. 基本介绍 gulp说得简单一点就是一个自动化把 ...

  9. 货车运输 noip2013 luogu P1967 (最大生成树+倍增LCA)

    luogu题目传送门! 首先,题目让我们求每个货车的最大运输量,翻译一下就是求路径上边权最小的边. 利用一下贪心思想可知,所有货车肯定都会尽量往大的边走. 进一步翻译,即为有一些小边货车根本不会走,或 ...

  10. nginx配置https及Android客户端访问自签名证书

    前一篇随笔通过keytool生成keystore并为tomcat配置https,这篇随笔记录如何给nginx配置https.如果nginx已配置https,则tomcat就不需要再配置https了.通 ...