Windows batch file - start git bash and run git commands

偶尔善良 提交于 2020-05-28 07:18:31

问题


I am on windows-10. I want to create a batch-file to automate:

  1. Starting Git Bash in project folder
  2. Running the following Git commands:

    eval $(ssh-agent -s) 
    ssh-add ~/.ssh/github_rsa
    git remote add origin git@github.com:git_user_name/git_repository_name.git
    

My current batch file below successfully starts Git Bash in the project folder, but I have not found anything which helps me learn how to run the git commands.

@echo off
cd /d C:\project_folder
start "" "%PROGRAMFILES%\Git\bin\sh.exe" --login -i <== edited per suggestion

回答1:


With direction from comment from bk2204 about "invoking sh.exe -c" I found that the following worked.

I concatenated all 3 commands into one string and put this string after start "" "%PROGRAMFILES%\Git\bin\sh.exe" -c and also added ; bash to the end of the command string which keeps the Git bash command window open. I wanted window open so I could continue to use it eg by entering manually git add ., git commit, push origin master, etc

start "" "%PROGRAMFILES%\Git\bin\sh.exe" -c "eval $(ssh-agent -s) && 
    ssh-add ~/.ssh/github_rsa && 
    git remote add origin git@github.com:git_user_name/git_repository_name.git; bash" 


来源:https://stackoverflow.com/questions/61735998/windows-batch-file-start-git-bash-and-run-git-commands

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!