Git Functionalities using c# Console Application - Commit new file / edit existing file to git

Anand N 1 Reputation point
2022-12-21T10:22:21.15+00:00

Git Functionalities using c# Console Application - Commit new file / edit existing file to git

How to read from git repository.
Commit a new file to git repository.
Commit a existing file with changes to git repository.

Can any one provide necessary c# code using Microsoft.TeamFoundation.SourceControl.WebApi .

Developer technologies C#
{count} votes

3 answers

Sort by: Most helpful
  1. Ricardo Mauro 1 Reputation point
    2022-12-21T18:58:31.207+00:00

    You can use this lib https://github.com/libgit2/libgit2sharp

    wiki of libgit2ssharp: https://github.com/libgit2/libgit2sharp/wiki

    I have use it in the past and it's pretty good.

    0 comments No comments

  2. Anand N 1 Reputation point
    2022-12-22T07:07:21.037+00:00

    Hi rmaurodev,

    This contains only related to committing from local repository to git repository,
    I was looking for writing directly to git repository.

    while trying

    using (var repo = new Repository("path/to/your/repo"))
    {
    Remote remote = repo.Network.Remotes["origin"];
    var options = new PushOptions();
    options.CredentialsProvider = (_url, _user, _cred) =>
    new UsernamePasswordCredentials { Username = "USERNAME", Password = "PASSWORD" };
    repo.Network.Push(remote, @"refs/heads/master", options);
    }

    not able to identify what value to provide on "origin" and @"refs/heads/master"

    0 comments No comments

  3. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-12-23T18:26:04.443+00:00

    The api would use the same steps

    Stage the file to local repo
    https://github.com/libgit2/libgit2sharp/wiki/git-add

    Commit the file
    https://github.com/libgit2/libgit2sharp/wiki/git-commit

    Push to remote ( your code)

    You may need to pull first to get your local in sync

    You also need to know the branch names to create the proper refs

    https://git-scm.com/book/en/v2/Git-Internals-Git-References

    In the past the main branch was often named “master” but this is not common anymore.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.