Skip to content
Radium Zheng edited this page Dec 7, 2023 · 8 revisions

The following examples assume you already have a repository in place to work with. If not, see git-init.

Make a commit to a non-bare repository

If you want to commit to a repository that is checked out to the file system, you can update the file on the file system and use the repository's Commit method.

using(varrepo=newRepository("path/to/your/repo")){// Write content to file systemvarcontent="Commit this!";File.WriteAllText(Path.Combine(repo.Info.WorkingDirectory,"fileToCommit.txt"),content);// Stage the filerepo.Index.Add("fileToCommit.txt");repo.Index.Write();// Create the committer's signature and commitSignatureauthor=newSignature("James","@jugglingnutcase",DateTime.Now);Signaturecommitter=author;// Commit to the repositoryCommitcommit=repo.Commit("Here's a commit i made!",author,committer);}

Make a commit to a bare repository

using(Repositoryrepo=newRepository("path-to-bare-repo")){stringcontent="Hello commit!";// Create a blob from the content streambyte[]contentBytes=System.Text.Encoding.UTF8.GetBytes(content);MemoryStreamms=newMemoryStream(contentBytes);BlobnewBlob=repo.ObjectDatabase.CreateBlob(ms);// Put the blob in a treeTreeDefinitiontd=newTreeDefinition();td.Add("filePath.txt",newBlob,Mode.NonExecutableFile);Treetree=repo.ObjectDatabase.CreateTree(td);// Committer and authorSignaturecommitter=newSignature("James","@jugglingnutcase",DateTime.Now);Signatureauthor=committer;// Create binary stream from the textCommitcommit=repo.ObjectDatabase.CreateCommit(author,committer,"i'm a commit message :)",tree,repo.Commits,prettifyMessage:false);// Update the HEAD reference to point to the latest commitrepo.Refs.UpdateTarget(repo.Refs.Head,commit.Id);}

Clone this wiki locally