matrinox
matrinox

Reputation: 472

Git: If given a commit SHA, can I fetch the previous one?

My purpose here is to write a script to rebase right before the commit provided so that it's easy to fix-up a commit by providing the SHA (long term thinking of allowing the user to provide a regexp but you get the point).

Any creative solutions? Using bash here.

Upvotes: 0

Views: 35

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 81022

From the gitrevisions man page:

^, e.g. HEAD^, v1.5.1^0

A suffix ^ to a revision parameter means the first parent of that commit object. ^ means the th parent (i.e. ^ is equivalent to ^1). As a special rule, ^0 means the commit itself and is used when is the object name of a tag object that refers to a commit object.

~, e.g. master~3

A suffix ~ to a revision parameter means the commit object that is the th generation ancestor of the named commit object, following only the first parents. I.e. ~3 is equivalent to ^^^ which is equivalent to ^1^1^1. See below for an illustration of the usage of this form.

So both rev^ and rev~ are the (first) parent revision of a commit. As @Jubobs says in his comment however any given revision can have more than one parent.

Upvotes: 2

Related Questions