-1

I just did a git commit, then did git checkout <previous commit>, then did git checkout <most recent commit>. It then said I was in a detached HEAD state. I think I read somewhere that instead of doing git checkout <most recent commit> I should do git checkout <branch name> instead of git checkout <most recent commit>.

What's the difference between these?

gkeenley
  • 6,088
  • 8
  • 54
  • 129

1 Answers1

0

git checkout <most recent commit> will reset your branch to the commit you have specified.

while

git checkout <branch name> switch to branch by updating the index and the files in the working tree, and by pointing HEAD at the branch.

You should also note this difference , git checkout <commit-hash> carries your index with itself so you don't lose your modifications. while git checkout branch is not possible without committing the working directory so you cannot carry your modifications to the new branch/head

Billions.A.Joel
  • 207
  • 1
  • 9
  • That's not right: regardless of whether you use detached-HEAD mode or branch mode with `git checkout`, you can or cannot carry uncommitted changes based on whether Git is going to have to replace such files during the checkout, not based on whether you're entering or exiting detached-HEAD mode. – torek Feb 20 '22 at 02:07