Git Clone Failing in Windows Batch Script - windows

I executed this command "C:\Program Files (x86)\Git\bin\git.exe" clone ssh://<user>#<org>:<path to repo> in Git bash terminal and in normal command prompt.
When executed in Git Bash terminal, the clone works fine, but while running it in command prompt, it gives following error.
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I need to run git clone through windows batch script, that's why
i need to make it run through command line. How to do that work ?
Note :-
1. Add public key to remote(gerrit) that was generated using ssh-keygen.

Your SSH identity is not found while running from command line. Make sure that %HOME%\.ssh directory contains your keys.

Related

Why can't I `source` ssh-agent and ssh-add with in bash script?

When I tried git push, I get the following error.
ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I know that I should specify my own key here, so I type the following in bash window, and it works fine.
ssh-agent bash
ssh-add ~/.ssh/id_rsa_lrz
git push
However, I'd like make things easy, so I have a set_env.sh like this
ssh-agent bash
ssh-add ~/.ssh/id_rsa_lrz
And I type in my bash window like this
. set_env.sh
git push
However, I get the ERROR: Repository not found. error again, but why?
This is because when you run :
. set_env.sh
You will be inside the bash session setup by ssh-agent bash where ssh-add ~/.ssh/id_rsa_lrz has not run yet.
What you need is putting in set_env.sh
eval "$(/usr/bin/ssh-agent)"
ssh-add ~/.ssh/id_rsa_lrz

How to execute git-clone command from shell script (linux)

Need to execute git clone command from shell script
I tried to use the git clone command and it is working
I am able to execute the below command from the git-bash console to checkout the developer1_branch from my project
git clone --branch developer1_branch https://project_ip/project.git /tmp/$(date +%Y%m%d)
This command will ask for credentials and it will clone the project to /tmp folder with today's date.
Expected results:
Need to run the same command from the shell script.
To automate the script to read the credentials from a creds.properties file which will have user=userid & password=Password123 as properties?
Please let me know how this can be achieved?

How do I git clone from a Windows machine over ssh?

I can do ssh windowsmachine from Linux to access a Windows machine, and from there I can git init --bare foo.git, telling me Initialized empty Git repository in C:/Users/unhammer/foo.git/
but how do I clone that from the unix side?
$ git clone ssh://windowsmachine:foo.git
Cloning into 'foo'...
fatal: No path specified. See 'man git-pull' for valid url syntax
$ git clone ssh://windowsmachine:C:\\Users\\unhammer\\foo.git
Cloning into '\Users\unhammer\foo'...
fatal: No path specified. See 'man git-pull' for valid url syntax
$ git clone ssh://windowsmachine:/foo.git
Cloning into 'foo'...
fatal: ''/foo.git'' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
and similar messages for /C:/Users/unhammer/foo.git and
/C/Users/unhammer/foo.git and /Users/unhammer/foo.git.
Note the double single-quotes:
fatal: ''/Users/unhammer/foo.git'' does not appear to be a git repository
This doesn't happen when I try to git clone linuxmachine:/some/path/that/does/not/exist.git, then git uses single single-quotes. (Maybe that's the issue, git-for-windows or something applying extra quotes?)
The easiest solution is to change the default Windows OpenSSH shell to bash. You can do this easily from powershell on the Windows machine:
powershell
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Program Files\Git\bin\bash.exe" -PropertyType String -Force
You may have to restart OpenSSH on the Windows machine and/or kill existing ssh connections from the client before it takes effect. Now you can clone without any extra -u option:
git clone ssh://windowsmachine/c/Users/unhammer/foo.git
(and if you previously specified uploadpack/receivepack, remove them from .git/config)
Following #phd's link to https://stackoverflow.com/a/8050564/7976758 I found a reference to https://stackoverflow.com/a/2323826/69663 with a workaround to the quoting issue. Here's what I did:
On Windows, open Git Bash and, in my homedir:
echo 'git-receive-pack "$#"' >grp.sh
echo 'git-upload-pack "$#"' >gup.sh
On Linux, clone specifying upload-pack:
git clone -u '"C:/Program Files/Git/bin/bash.exe" gup.sh' ssh://windowsmachine/c/Users/unhammer/foo.git
cd foo
git config remote.origin.uploadpack '"C:\Program Files\Git\bin\bash.exe" gup.sh'
git config remote.origin.receivepack '"C:\Program Files\Git\bin\bash.exe" grp.sh'
So the repo path is the one that Git Bash shows (/c/Users/$username/$repo), no colon anywhere.
https://github.com/PowerShell/Win32-OpenSSH/issues/1082 seems related.

Git Portable 64 Bash not working in Windows Server 2016 Task Scheduler

I need help trying to figure out why Git repository commands are not not executing when run in a script in Windows Server 2016 Task scheduler. All works OK when I execute them in a command console.
In the Windows Server 2016 Task Scheduler, my Action "Start a Program" is: C:\Apps\repo.scripts\UpdateMyRepo.bat
The UpdateMyRepo.bat cmd code is:
SET HOME=C:\Users\Repo
REM change to MyRepo git working repository
C:
cd \MyRepo
REM execute my script bash script to update my repository
C:\Apps\Git\bin\bash.exe --login -i -c "/c/Apps/repo.scripts/UpdateMyRepo.sh"
The UpdateMyRepo.sh bash code is
#!/c/Apps/Git/bin/bash.exe -x
export HOME=/c/Users/Repo
cd /c/MyRepo
# write a log entry so we know we are in the repository folder
ls -al > /c/Apps/repo.scripts/myrepofolder.log
# write the git --version to a log file so we know git is working
/c/Apps/Git/bin/git.exe --version > /c/Apps/repo.scripts/version.log
# write the git status to a log file so we know git repository commands work
/c/Apps/Git/bin/git.exe status > /c/Apps/repo.scripts/status.log
# write a done log entry and quit the bash shell
echo done > /c/Apps/repo.scripts/done.log
exit
Everything works in Windows 2016 Task Scheduler except the git status command. The git status writes an empty blank status.log file. Actually, it seems like any other commands, like git add, git commit, git push, etc., that act on the repository yield blank output.
If I execute the command manually when logged in as the Repo user and double clicking on the C:\Apps\repo.scripts\UpdateMyRepo.bat in Windows file explorer or running in a console, all works perfectly and the repository git status is written to the status.log. I get the "null" results when executing the task from the Task Scheduler either manually or on trigger.
Please help me figure out how to run git repository commands in Windows Server 2016 task scheduler. I have already tried too many variations of commands, scripts, and permissions to list each that did not work here.
Platform Details:
Windows Server 2016 Standard, all current updates
Git Portable 64bit, 2.17.1.windows.2
Repository files are stored on in the operating system and task scheduler's local hard drive, not in a network share
update: When I run the Git repository tasks in the WS2016 Task Scheduler and figured out how to log some of the output, I get the following error:
fatal: this operation must be run in a work tree
The repository folder is NOT a "bare" repository. So I am suspecting that the WS2016 Task Scheduler is applying additional permission constraints that do not apply to the user account assigned in the task. I get this same error if I try to run the tasks in an Admin Console instead of a normal user console.
First, most of Git commands writes their output to stderr, not stdout
So you need to redirect stderr to stdout, using for instance &> instead of >.
Second, to be sure all your Git command will be executed in the right working tree/repo, you can add environment variables for the repository location
export GIT_WORK_TREE=/c/path/to/my/repo
export GIT_DIR=/c/path/to/my/repo/.git

SSH remote command executing a script

I have two hosts, hosts A and B. A has a script (generate) that compiles my thesis:
#!/bin/sh
pdflatex Thesis.tex
When running this command on host A (console window) it works perfectly.
I am basically trying to connect from host B to A and run the generation command as an ssh remote command. All the keys are properly set. When I run the command, I get the following:
hostB> ssh user#hostA exec ~/Thesis/generate
This is pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian)
entering extended mode
! I can't find file `Thesis.tex'.
<*> Thesis.tex
I tried adjusting the script so that it considers the directory:
pdflatex ~/Thesis/Thesis.tex
But because the Thesis.tex inputs some others files (images), I get an error message.
I presume the problem is some sort of enviroment that doesn't exist in remote commands. How do I fix this?
ssh will run your command in your home directory. You probably wanted to run it in your ~/Thesis directory.
Just cd first and it should be fine:
ssh user#hostA 'cd ~/Thesis && ./generate'

Resources