3

Hi I want to git clone into my current project folder in c# without using any external nuget packages.

I am looking at System.Diagnostics but am not sure how to implement this.

Any advice?

`

ProcessStartInfo gitInfo = new ProcessStartInfo();
gitInfo.CreateNoWindow = true;

Process gitProcess = new Process();
gitInfo.Arguments = "git clone";
gitInfo.WorkingDirectory = ?

`

6
  • Does this answer your question? Run git commands from a C# function Commented Oct 27, 2022 at 2:51
  • @Chetan that works but how can I clone it into the current directory rather than currentDirectory/bin/Releases/ ? Commented Oct 27, 2022 at 5:47
  • Can you share the code which is now working for you? Commented Oct 27, 2022 at 6:19
  • 2 possibilities: 1.Git clone accept a directory as argument where to clone to 2. You could fill the WorkingDirectory property but git clone command will create a sub folder and will clone into it (except if you pass '.' as path argument to the command. See 1.) Commented Oct 27, 2022 at 6:55
  • @Philippe how can I set git commands and WorkingDirectory for a Process? I a currently doing Process.start("git", "git clone") Commented Oct 27, 2022 at 11:51

1 Answer 1

2

The 3 options:

  1. Don't let git create the repo folder (by specifying it in the command line):
    var process = new Process
    {
        StartInfo = new ProcessStartInfo()
        {
            FileName = "git",
            Arguments = "clone https://repo/url c:/target/path/for/repo",
        }
    };
    process.Start();

equivalent also to:

    Process.start("git", "clone https://repo/url c:/target/path/for/repo");

But if you want to specify the working directory, you have to use ProcessStartInfo class.

1.bis. Don't let git create the repo folder (by using the current working directory . so you have to fill the WorkingDirectory property):

    var process = new Process
    {
        StartInfo = new ProcessStartInfo()
        {
            FileName = "git",
            Arguments = "clone https://repo/url .",
            WorkingDirectory = "c:/target/path/for/repo",
        }
    };
    process.Start();
  1. Let git create the repo folder in the working directory folder provided:
    var process = new Process
    {
        StartInfo = new ProcessStartInfo()
        {
            FileName = "git",
            Arguments = "clone https://repo/url",
            WorkingDirectory = "c:/target/path/for/repo",
        }
    };
    process.Start();
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this but I'm now facing the issue here when my URL is ssh://[email protected]/repo and I also tried [email protected]/repo stackoverflow.com/questions/65061142/…
@Sona Difficult to say because there are typos and you don't provide the real data. The uri should be something like [email protected]/account/repo_name. But try to find the exact command line in a shell running successfully before putting it in your source code...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.