By checking out to one of the commits in the history you are moving your git into so called 'detached state', which looks like is not what you want. Use this single command to create a new branch on one of the commits from the history:
git checkout -b <new_branch_name> <SHA1>
Answer from Andrejs Cainikovs on Stack OverflowBy checking out to one of the commits in the history you are moving your git into so called 'detached state', which looks like is not what you want. Use this single command to create a new branch on one of the commits from the history:
git checkout -b <new_branch_name> <SHA1>
If you checkout a commit sha directly, it puts you into a "detached head" state, which basically just means that the current sha that your working copy has checked out, doesn't have a branch pointing at it.
If you haven't made any commits yet, you can leave detached head state by simply checking out whichever branch you were on before checking out the commit sha:
git checkout <branch>
If you did make commits while you were in the detached head state, you can save your work by simply attaching a branch before or while you leave detached head state:
# Checkout a new branch at current detached head state:
git checkout -b newBranch
You can read more about detached head state at the official Linux Kernel Git docs for checkout.
How do we understand the command: "git checkout commit_id ." - Stack Overflow
How to switch to specific git commit?
git checkout with commit ID? - Stack Overflow
Is this the way to checkout a repo to a specific commit?
Can I push changes from a specific commit checkout?
Can I make changes after checking out a specific commit?
What does 'git reset --hard' do?
Hi, i want to inspect the old version of my code. I need to see the list of commits and chose the one, i want to switch to.
I can't achieve this with default source control tool in VS code. Seems like I'm only able to switch to the latest commit of specified branch> When i press bottom left corner "branch selector", i see only branches and not commits:
branch selector in vsCodeI was able to achieve this by using gitLens plugin, but it is overkill, requires gitkraken and other stuff and not free. Here is the list of commits and i can choose commit to which i need to switch:
gitLens list of commits
Is there any way to achieve this without extensions?
Is there any minimal extensions for this?
Check if the commit exists first using:
$ git show <commit_id>
$ git checkout <commit_id>
Another way would be too:
$ git log --oneline | grep <commit_id>
If it exists then you can checkout to it. If you got that ID from remote branch, gitlab, github, make sure you have that branch locally as well.
I was given the wrong commit ID, for some reason even though I was specific in the repository I was working on the commit ID I was given was for another repository.
Several times I queried this and several times I was told, it works!
Now its cleared up, there is no longer an issue. I would have deleted this but since there is already an answer I couldn't.
I asked someone what version of a repo they used for an analysis and they gave me a hash for a commit.
Is this the correct way to access the version they used?
-
git clone https://github.com/foo.git
-
cd foo
-
git checkout -b bar <hash she gave me>
This should give me the repo in the state of the commit hash she gave me correct?