# Git Takes Sha Instead Of Remote Branch

Was debugging a weird production issue. the version deploy was absolutely wrong.

## Premise:

When our CI build the product it creates a branch. with the build number. in our CI it was a running integer but in the [repo](https://github.com/NahumLitvin/GitTakesShaInsteadOfRemoteBranch) I created for the example it will be [2c4f227](https://github.com/NahumLitvin/GitTakesShaInsteadOfRemoteBranch/tree/2c4f227)

by coincidence it was starting the same way as a random commit on the main branch.

### on the deploy stage we clone the repo (on main branch by default)

> git clone git@github.com:NahumLitvin/GitTakesShaInsteadOfRemoteBranch.git

> git checkout 2c4f227

### and thats the result I got:

> _Note: checking out '2c4f227'._

> _You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout._

> _If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:_

> _git checkout -b <new-branch-name>_

> _HEAD is now at _**_2c4f227_**_ Initial commit_

## **git checked out the commit** **_2c4f227 instead of the branch _**2c4f227!

## Solution:

> git checkout origin/2c4f227

will checkout the correct branch

another more concise way is to use the new [git switch](https://git-scm.com/docs/git-switch) command available from git 2.29.0

## git switch

> git switch 2c4f227
warning: refname '2c4f227' is ambiguous.
Switched to branch '2c4f227'
Your branch is up to date with 'origin/2c4f227'.

[**git switch**](https://git-scm.com/docs/git-switch) will _'Switch to a specified branch. The working tree and the index are updated to match the branch. All new commits will be added to the tip of this branch.'
_**when: **
[**git checkout**](https://git-scm.com/docs/git-checkout) _'Updates files in the working tree to match the version in the index or the specified tree. If no pathspec was given, _**_git checkout_**_ will also update __HEAD to set the specified branch as the current branch.'_

## additional reading

see this stack overflow Q&A for a better explanation: [https://stackoverflow.com/questions/57123031/git-checkout-commit-hash-vs-git-checkout-branch/](https://stackoverflow.com/questions/57123031/git-checkout-commit-hash-vs-git-checkout-branch/)

![](https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a905ab866559)