The difference between git reset commands

I used git reset --soft HEAD^ and git reset --soft HEAD~1 when I want to return my last commit to the staging area. I used them interchangeably, but I was wondering if there were any subtle differences? If not, can you explain the syntactic difference? Is ^ just an alias for ~1 ?

+4
source share
6 answers

HEAD^ and HEAD~1 refer to the same commit.

^ refers to the first commit parent. ~n refers to n: th ancestor. Therefore ^^ (parent parent element) is equivalent to ~2 .

The main subtlety that I can think of is the presence of several parents for the current commit (i.e. this is a merge commit). In this case, both HEAD^ and HEAD^2 valid and refer to different commits. HEAD~1 refers to HEAD^ but not HEAD^2

The gitrevisions man page contains many details and examples.

+3
source

Even if HEAD^ and HEAD~1 do the same, they mean slightly different things. HEAD^ is short for HEAD^1 , which refers to the commit of the first parent, and HEAD^2 will refer to the second parent of the commit. HEAD~ is an abbreviation for HEAD~1 , which refers to the first parent commit element and HEAD~2 refers to the first parent parent commit element. These two syntaxes are interchangeable if HEAD not a merge commit, in which case it can have more than one parent (previous commit on the main branch and previous commit on the branch that was merged with the master).

+2
source

The difference is that HEAD~1 used for linear history, while HEAD^ can follow commits with multiple parents.

+1
source

HEAD ^ and HEAD ~ 1 are one and the same.

From my notes:

ref ^ means commit before ref. You can use multiple characters. Example: HEAD ^^^

ref ~ n means the nth latch before ref. Example: HEAD ~ 3

There are more complex forms that allow you to deal with a case where a base link (like HEAD) is the result of a merge.

Actually, it can become very complicated. See https://www.kernel.org/pub/software/scm/git/docs/gitrevisions.html

+1
source

No, HEAD^ and HEAD~1 (as well as HEAD~ or HEAD^1 ) refer to the same commit.

Adding ^ or ~ after something that describes the commit will change the reference to the first parent of the commit described by the previous part of the argument.

The difference occurs when a number follows a character. If the number is not specified, it works the same as if 1 were used.

The number following ^ selects which parent of the merger should be used (to some extent, as the choice of father or mother). The first parent will be the commit, which was checked when the merge commit was created, other parents will be declared using the git merge command.

The number following ~ refers to the number of generations that return (after the parent’s first relationship at each step), 1 to the immediate parent, 2 to the parent, etc.

These notations can also be encoded, for example HEAD~3^2~5 , although it is usually simpler to simply use the SHA1 identifier to refer to commits that need this type of notation.

0
source

If you have already made your local repo (i.e. git commit -m), you can undo this last commit by running git reset --soft HEAD ~ 1 If you already made your changes (i.e. Using git add. ), You can cancel the stage by doing git reset --mixed HEAD and git reset --hard erases everything (even local changes). ~ After the head tells you how many commits go on top.

0
source

Source: https://habr.com/ru/post/1486122/


All Articles