Skip to content
KindDragon edited this page Mar 14, 2013 · 14 revisions

git-branch

Listing branches

Displaying local branches while highlighting the current branch with an asterisk

Git

$ git branch

LibGit2Sharp

using (var repo = new Repository("path/to/your/repo"))
{
    foreach(Branch b in repo.Branches.Where(b => !b.IsRemote))
    {
        Console.WriteLine(string.Format("{0}{1}", b.IsCurrentRepositoryHead ? "*" : " ", b.Name));
    }
}

Displaying local branches that contains specified commit

Git

$ git branch --contains <commit>

LibGit2Sharp

using (var repo = new Repository("path/to/your/repo"))
{
    const string commitSha = "5b5b025afb0b4c913b4c338a42934a3863bf3644";
    foreach(Branch b in ListBranchesContaininingCommit(repo, commitSha))
    {
        Console.WriteLine(b.Name);
    }
}

private IEnumerable<Branch> ListBranchesContaininingCommit(Repository repo, string commitSha)
{
    foreach (var branch in repo.Branches)
    {
        var commits = repo.Commits.QueryBy(new Filter { Since = branch }).Where(c => c.Sha == commitSha);

        if (commits.Count() == 0)
        {
            continue;
        }

        yield return branch;
    }
}

Creating a branch

To be done

Renaming/moving a branch

The new branch doesn't conflict

To be done

Overwriting an existing branch

To be done

Resetting a branch to another startpoint

To be done

Deleting a branch irrespective of its merged status

To be done

Clone this wiki locally