点击阅读原文

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. nodejs server启动写法

    http://www.phpstudy.net/c.php/18720.html node不利用框架怎么实现对静态HTML.css.js的服务? 初学nodeJS,在使用nodejs构建静态文件服务器 ...

  2. SpringBoot之整合MongoDB

    MongoDB官网安装:https://www.mongodb.com/download-center/community MongoDB客户端工具(Mongo Management Studio)安 ...

  3. 201771010128王玉兰《面向对象程序设计(Java)》第八周学习总结

    第一部分:理论知识部分总结 (1)接口:接口不是类,而是对类胡一组需求描述,由常量肯一组抽象方法组成. a:接口中不包括变量和有具体实现的方法 b:只要类实现了接口,则该类要遵从接口描述的统 一格式进 ...

  4. Hills And Valleys 杭电多校第五场

    题意:长度为n的序列,有一次翻转区间的机会,问最长不减序列 题解:如果没有翻转区间的机会,有两个做法. 一是dp[i]表示以i结尾的最长序列 dp[i]=max(dp[i],dp[j]+1)  (j& ...

  5. LightOJ1030 Discovering Gold

    题目链接:https://vjudge.net/problem/LightOJ-1030 知识点: 概率与期望 解题思路: 设某一个点 \(i\) 能到达的点的个数为 \(x\),其上有金 \(g\) ...

  6. hide handkerchief(hdu2104)

    思考:这种找手绢就是,在判断是否互质.用辗转相除法(用来求最大公约数:a)进行判断.r=a%b;a=b;b=r;循环限制条件:除数b=0是结束除法.如果这时被除数a=1,则表示两个互质. #inclu ...

  7. A+B Coming(hdu1720)

    思考:十六进制的输入->%x,定义时用int.要变成十进制输出,直接在输出时用->%d. #include<stdio.h> int main() { int A,B; cha ...

  8. Xftp远程连接出现“无法显示文件夹”的问题补充

    网上有很多朋友出现相同的问题,各位热心网友都给出了自己的解决方案,其中大多数网友给出的解决方案都是:将Xftp更换成“被动连接模式”.但是很不幸的是,本人通过这种方式并没有得到有效的解决,网上的各大方 ...

  9. Spring boot Sample 004之spring-boot-configuration-yaml

    一.环境 1.1.Idea 2020.1 1.2.JDK 1.8 二.目的 通过yaml文件配置spring boot 属性文件 三.步骤 3.1.点击File -> New Project - ...

  10. Java并发编程 (四) 线程安全性

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.线程安全性-原子性-atomic-1 1.线程安全性 定义: 当某个线程访问某个类时,不管运行时环境 ...