Click here to Skip to main content
15,905,238 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to perform 'git pull' or 'git pull origin master' in a specific folder but it;s not working when I trying to do it like this:

Any suggestions?

What I have tried:

C#
public static void gitPull ()
{
    System.Diagnostics.Process process = new System.Diagnostics.Process();
    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
    startInfo.UseShellExecute = false;
    startInfo.RedirectStandardOutput = true;
    startInfo.FileName = "CMD.exe";
    startInfo.Arguments = @"/c d:\GIT\proj1\git pull origin master";
    process.StartInfo = startInfo;
    process.Start();
    string output = process.StandardOutput.ReadToEnd();
    Console.WriteLine(output);
    process.WaitForExit();
}

EDIT: Added from solution 1:
When i'm in d:\GIT\proj1\ and use git pull it is working. But how to move into proj1 folder and perform git pull command in one?

When I trying to do this by a method above a I get:
'd:\GIT\proj1\' is not recognized as an internal or external command,
operable program or batch file.
fatal: 'origin' 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.
Posted
Updated 12-Nov-20 23:40pm
v6
Comments
Pete O'Hanlon 13-Nov-20 4:48am    
You said you the code doesn't work but that doesn't really tell us anything. Are you getting errors? If so, what errors are you seeing?

The beauty about process commands is that you can try them out without having to run the application. What do you get when you run the d:\GIT\proj1\git pull master command?
aksimoN 13-Nov-20 5:13am    
When i'm in
d:\GIT\proj1\

and use: git pull

it is working.

But how to move into proj1 folder and perform git pull command in one?

When I trying to do this by a method above a I get:

'd:\GIT\proj1\git' is not recognized as an internal or external command,
operable program or batch file.
Pete O'Hanlon 13-Nov-20 5:25am    
Richard's answer below shows what you need to do. The problem you had in your code is the process expects d:\GIT\proj1\GIT to be a runnable command whereas what you are actually trying to do is run GIT (which is in another directory) inside the d:\GIT\proj1 folder.

1 solution

Try:
C#
public static void gitPull()
{
    var startInfo = new System.Diagnostics.ProcessStartInfo
    {
        UseShellExecute = false,
        RedirectStandardOutput = true,
        FileName = "CMD.exe",
        Arguments = @"/c cd d:\GIT\proj1\ & git pull master",
    });
    
    var process = System.Diagnostics.Process.Start(startInfo);
    string output = process.StandardOutput.ReadToEnd();
    Console.WriteLine(output);
    process.WaitForExit();
}
 
Share this answer
 
Comments
aksimoN 13-Nov-20 5:38am    
I had to improve my command but still it doesn't work:
Hide   Copy Code
'd:\GIT\proj1\ ' is not recognized as an internal or external command,operable program or batch file.
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.Please make sure you have the correct access rightsand the repository exists.


ps. in cmd works both : 'git pull' and 'git pull origin master'
Richard Deeming 13-Nov-20 6:20am    
How about:
var startInfo = new System.Diagnostics.ProcessStartInfo
{
    UseShellExecute = false,
    RedirectStandardOutput = true,
    FileName = "git.exe",
    Arguments = "pull master",
    WorkingDirectory = @"d:\GIT\proj1\",
});
aksimoN 13-Nov-20 6:48am    
Yup, its working but it nesesery to add repository in git command so:
Arguments = "pull origin master"
Pete O'Hanlon 13-Nov-20 6:04am    
Got my 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900