Pawan Nogariya
Pawan Nogariya

Reputation: 8960

How to extract latest commit hash for a remote git repository in Windows?

I am trying to read the latest commit for a git remote repository using this command

git ls-remote https://repo.myrepository.com/scm/swc/project.git refs/heads/qa

it works fine and returns me something like this

5261626431661281d788382a1ed6ab1440fd93a8        refs/heads/qa

But I am not able to find online any way to extract only the commit hash from the returned string in command line

I thought it would be very easy to find this information online, but the only answer I am finding everywhere is this

git ls-remote https://repo.myrepository.com/scm/swc/project.git refs/heads/qa | \ cut -f 1

But this does not work in windows command line, it says cut is not a recognized command.

Can anybody please help for windows command line version of it?

Upvotes: 2

Views: 217

Answers (1)

jnovack
jnovack

Reputation: 8827

for /f is how you get can "cut" tokens with a delimiter.

In the following examples, the delims is a Space.

# inside of a batch file
# get first token, delimited by <space>
for /f "tokens=1,* delims= " %%a in (
    'git ls-remote https://repo.myrepository.com/scm/swc/project.git refs/heads/qa'
) do echo %%a
# command line
# get second token, delimited by <space>
for /f "tokens=2,* delims= " %a in ('echo one two') do echo %a

enter image description here

Upvotes: 1

Related Questions