Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between `git branch -f <branch_name> <hash>` and `git checkout <branch_name>; git reset --hard <hash>` under a clean working tree?

Up until now, I have always used git checkout <branch_name>; git reset --hard <hash> to move a branch back to an earlier commit.

Then I came across this question, but the answers and comments do not explain in great detail the differences between them.

Assuming I have a clean working tree, what internal differences are there between

git branch -f <branch_name> <hash>

and

git checkout <branch_name>
git reset --hard <hash>

and do such differences, if any, have any subtle implications for advanced usage?

like image 853
merlin2011 Avatar asked Jan 26 '15 12:01

merlin2011


People also ask

What is the difference between git reset and git reset -- hard?

reset --soft : History changed, HEAD changed, Working directory is not changed. reset --mixed : History changed, HEAD changed, Working directory changed with unstaged data. reset --hard : History changed, HEAD changed, Working directory is changed with lost data. It is always safe to go with Git --soft.

What is the difference between reset and reset git?

So, in short, we can say that “git reset” is a command, whereas “git reset –hard” is its variation that is used when you want to wipe out all the traces of your last commit.

What does git reset -- hard origin branch do?

git reset --hard origin/master works only as a full wipe out if you are in a local branch. If you are in the master branch instead, and if you have made changes, you can only drop all of the files that you made or changed. You cannot drop the folders that you added.

What is the difference between git rm -- cached and git reset?

git rm —cached file will remove the file from the stage. That is, when you commit the file will be removed. git reset HEAD — file will simply reset file in the staging area to the state where it was on the HEAD commit, i.e. will undo any changes you did to it since last commiting.


1 Answers

The main difference is that git branch -f <branchname> <commitref> moves <branchname> to point the specified commit without touching HEAD, the index or the working copy, while git checkout <branchname> && git reset --hard <commitref> modifies all three.

If you want to quickly rearrange branches without moving HEAD or modifying your current working tree, then git branch -f is a good way to do it. It will also work if you have uncommitted changes, which isn't always possible if you use git checkout.

Another difference is related to performance, but it's only relevant for very large projects.
In those cases, modifying your working tree with git checkout and git reset --hard could potentially be an expensive operation with lots of disk I/O. On the other hand, with git branch -f only a single file will be written on disk, i.e. the one that contains the <commithash> referenced by <branchname>.

like image 102
Enrico Campidoglio Avatar answered Oct 10 '22 02:10

Enrico Campidoglio