0
我想將我的克隆存儲庫切換到某個指定版本。當我運行這個代碼時,它不能按我的需要工作。它意識到我有多少提交背後或高於主,但它並沒有真正切換項目。如何切換到指定版本
例如,如果我的版本1.0包含一些txt文檔,而版本1.1沒有這個txt文檔。主指向版本1.1
首先我克隆整個存儲庫,(目標文件夾不包含txt文檔)。
然後,我執行此代碼,我希望txt文檔出現在目標文件夾中。
當我試試這個:
Download a specific tag with Git
它的工作,我想我的代碼做同樣的,
git_libgit2_init();
const char * REPO_PATH = path.c_str();
git_repository * repo = nullptr;
git_repository_open(&repo, REPO_PATH);
git_reference *ref;
git_reference_lookup(&ref, repo, "refs/heads/master"); // "refs/remotes/origin/HEAD"
git_reference *new_ref;
git_reference_lookup(&new_ref, repo, tag.c_str());
git_revwalk *walker;
git_revwalk_new(&walker, repo);
git_revwalk_push_ref(walker, tag.c_str());
git_oid id;
git_revwalk_next(&id, walker);
git_reference_set_target(&new_ref, ref, &id, NULL);
if (0 != git_repository_set_head_detached(repo, &id)) cerr << "problem occured while detaching head" << endl;
git_revwalk_free(walker);
git_repository_free(repo);
git_libgit2_shutdown();
我也嘗試過這樣的事情,但這種失敗在git_annotated_commit_from_ref( )
還有我的第二個實現:
git_libgit2_init();
const char * REPO_PATH = path.c_str();
git_repository * repo = nullptr;
git_repository_open(&repo, REPO_PATH);
git_reference *ref;
git_reference_lookup(&ref, repo, tag.c_str());
git_annotated_commit *out;
if (0 != git_annotated_commit_from_ref(&out,repo,ref)) cerr << "error creating annotated commit" << endl;
if (0 != git_repository_set_head_detached_from_annotated(repo, out)) cerr << "problem occured while detaching head" << endl;
git_repository_free(repo);
git_libgit2_shutdown();