Skip to content
Norbert Rozmus edited this page Apr 5, 2017 · 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.FriendlyName));
    }
}

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 ListBranchesContainingCommit(repo, commitSha))
    {
        Console.WriteLine(b.Name);
    }
}

private IEnumerable<Branch> ListBranchesContainingCommit(Repository repo, string commitSha)
{
    var localHeads = repo.Refs.Where(r => r.IsLocalBranch);

    var commit = repo.Lookup<Commit>(commitSha);
    var localHeadsContainingTheCommit = repo.Refs.ReachableFrom(localHeads, new[] { commit });

    return localHeadsContainingTheCommit
        .Select(branchRef => repo.Branches[branchRef.CanonicalName]);
}

Creating a branch pointing at the current HEAD

Git

$ git branch develop

LibGit2Sharp

using (var repo = new Repository("path/to/your/repo"))
{
    repo.CreateBranch("develop");   // Or repo.Branches.Add("develop", "HEAD");
}

Creating a branch pointing at a specific revision

Git

$ git branch other HEAD~1

LibGit2Sharp

using (var repo = new Repository("path/to/your/repo"))
{
    repo.CreateBranch("other", "HEAD~1");   // Or repo.Branches.Add("other", "HEAD~1");
}

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

Deleting a local branch

using (var repo = new Repository("path/to/your/repo"))
{
    repo.Branches.Remove("BranchToDelete");
}

Edit branch description

Git

$ git branch --edit-description

LibGit2Sharp

var description = "branch description"
using (var repo = new Repository("path/to/your/repo"))
{
    var key = string.Format("branch.{0}.description", branch);
    repo.Config.Set(key, description.Replace(Environment.NewLine, string.Empty)); // set description
    repo.Config.Unset(key); // remove description
}
Clone this wiki locally
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy