Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between git reset commands

Tags:

git

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

like image 663
Josh Avatar asked Mar 24 '23 14:03

Josh


1 Answers

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

^ refers to the first parent of the commit. ~n refers to the n:th ancestor. So ^^ (parent-of-parent) is equivalent to ~2.

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

The gitrevisions man page has lots of details and examples.

like image 196
Klas Mellbourn Avatar answered Apr 09 '23 04:04

Klas Mellbourn