I am using the Rust git2 crate to clone Git repositories like this
use git2::Repository;
fn main() {
let repo = Repository::clone(
"https://github.com/rossmacarthur/dotfiles",
"dotfiles"
).expect("failed to clone repository");
repo.checkout("mybranch"); // need something like this.
}
I want to be able to checkout a branch or a commit or a tag.
I have looked at the following documentation but am still not sure which method to use
- https://docs.rs/git2/0.8.0/git2/struct.Repository.html#method.checkout_head
- https://docs.rs/git2/0.8.0/git2/struct.Repository.html#method.checkout_tree
- https://docs.rs/git2/0.8.0/git2/struct.Repository.html#method.checkout_index
I am able to do the following but it only changes the files
let object = repo
.revparse_single("mybranch")
.expect("failed to find identifier");
repo.checkout_tree(&object, None)
.expect(&format!("failed to checkout '{:?}'", object));
And if I do a reset it changes the HEAD but not the current branch
repo.reset(&object, git2::ResetType::Soft, None)
.expect(&format!("failed to checkout '{:?}'", object));