跟踪和暂存文件之间的编码差异

时间:2013-04-02 14:53:50

标签: indexing libgit2

使用libgit2 API,“添加”文件以进行跟踪或将修改后的文件添加到暂存区域之间有什么区别吗?

这是我目前用于暂存已修改的跟踪文件的代码:

int giterror = git_repository_index( &index, open_repo );
if( giterror != 0 )
{
    return giterror;
}

//  Refresh the index from disk to load the entries that may already be staged
giterror = git_index_read( index );
if( giterror != 0 )
{
    git_index_free( index );

    return giterror;
}


giterror = git_index_add_bypath( index, relativeFilePath );
if( giterror != 0 )
{
    git_index_free( index );

    return giterror;
}


// write updated index to disk - aka staging area
giterror = git_index_write( index );
if( giterror != 0 )
{
    git_index_free( index );

    return giterror;
}


// write the index of changes to a tree
git_oid rootTreetOID;
giterror = git_index_write_tree( &rootTreetOID, index );
if( giterror != 0 )
{
    git_index_free( index );

    return giterror;
}

我应该使用相同的代码将未跟踪的文件添加到索引吗?

1 个答案:

答案 0 :(得分:1)

是的,你应该。

git_index_add_bypath()文档指出,当人们愿意“从磁盘上的文件添加或更新索引条目”时,应该使用此方法。

此方法可以

  • 将未跟踪的文件添加到索引
  • 分阶段修改文件