Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Going back to previous commit in SourceTree

I am new to Git, and I was trying to revert back to a previous commit in SourceTree. I right clicked on the commit I was going to revert to and then clicked checkout. It gave me a prompt saying that my working copy would become a detached head. What does this mean and is this something I should avoid?

like image 561
Nikhil Sridhar Avatar asked Mar 01 '17 05:03

Nikhil Sridhar


2 Answers

As Per Git-Tower's Article : What's a "detached HEAD" in Git?

Understanding how "checkout" works

With the "git checkout" command, you determine which revision of your project you want to work on. Git then places all of that revision's files in your working copy folder.

Normally, you use a branch name to communicate with "git checkout"

$ git checkout development

However, you can also provide the SHA1 hash of a specific commit instead:

$ git checkout 56a4e5c08
Note: checking out '56a4e5c08'.

You are in 'detached HEAD' state...

This exact state - when a specific commit is checked out instead of a branch - is what's called a detached HEAD.

enter image description here

The problem with a detached HEAD

The HEAD pointer in Git determines your current working revision (and thereby the files that are placed in your project's working directory). Normally, when checking out a proper branch name, Git automatically moves the HEAD pointer along when you create a new commit. You are automatically on the newest commit of the chosen branch.

When you instead choose to check out a commit hash, Git won't do this for you. The consequence is that when you make changes and commit them, these changes do NOT belong to any branch. This means they can easily get lost once you check out a different revision or branch: not being recorded in the context of a branch, you lack the possibility to access that state easily (unless you have a brilliant memory and can remember the commit hash of that new commit...).

Summary : From SourceTree, Kindly Checkout to Particular Branch Instead of Checking out to Particular Commit.

like image 136
LuFFy Avatar answered Sep 20 '22 23:09

LuFFy


December 9, 2020.

I am using Sourcetree. If you would like go your previous commit, you can just double click on previous commit. After that, if you want you can create a new branch and merge with your old branch.

enter image description here

like image 29
DevPolarBear Avatar answered Sep 18 '22 23:09

DevPolarBear