Skip to content
nulltoken edited this page Nov 8, 2013 · 14 revisions

git-branch

Listing branches

Displaying local branches while highlighting the current branch with an asterisk

Git

$ git branch 

LibGit2Sharp

using(varrepo=newRepository("path/to/your/repo")){foreach(Branchbinrepo.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(varrepo=newRepository("path/to/your/repo")){conststringcommitSha="5b5b025afb0b4c913b4c338a42934a3863bf3644";foreach(BranchbinListBranchesContainingCommit(repo,commitSha)){Console.WriteLine(b.Name);}}privateIEnumerable<Branch>ListBranchesContainingCommit(Repositoryrepo,stringcommitSha){foreach(varbranchinrepo.Branches){varcommits=repo.Commits.QueryBy(newFilter{Since=branch}).Where(c =>c.Sha==commitSha);if(!commits.Any())continue;yieldreturnbranch;}}

Creating a branch pointing at the current HEAD

Git

$ git branch develop 

LibGit2Sharp

using(varrepo=newRepository("path/to/your/repo")){repo.CreateBranch("develop");}

Creating a branch pointing at a specific revision

Git

$ git branch other HEAD~1 

LibGit2Sharp

using(varrepo=newRepository("path/to/your/repo")){repo.CreateBranch("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

Clone this wiki locally