Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a file from git repository using libgit2

Tags:

c++

c

libgit2

Say you have a file in a git repository:

  • a.txt

What APIs should be used to create a commit that removes that file? For example in this question the file is committed without creating it on disk. Is it now possible to remove that file without using the index (stage area)?

I was expecting a similar flow, maybe creating a git_tree_entry for the tree builder, but that does not seem to be the case. git_reference_list() does not list files, so there's a dead end. Also searching the sources for delete and remove got me no success.

like image 655
Nicu Tofan Avatar asked Oct 04 '22 13:10

Nicu Tofan


1 Answers

Removing a file is similar to adding a file - you remove the index entry to stage the delete, then you can create a commit from the index.

You probably want to use git_index_remove_bypath, which will remove the file from the index and resolve any conflicts that file had.

like image 166
Edward Thomson Avatar answered Oct 13 '22 10:10

Edward Thomson