# How to change branch name using \`LibGit2\`?

**URL:** <https://discourse.julialang.org/t/how-to-change-branch-name-using-libgit2/84353>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 17, 2022, 11:16am UTC](https://discourse.julialang.org/t/how-to-change-branch-name-using-libgit2/84353 "2022-07-17T11:16:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [July 17, 2022, 11:16am UTC](https://discourse.julialang.org/t/how-to-change-branch-name-using-libgit2/84353/1 "2022-07-17T11:16:42Z")

</div>

_sorry, not sure which category suits best_

I am using `LibGit2` to create a new git repository inside a package. I am doing something like:

```julia
        repo = LibGit2.init(path)
        sig = LibGit2.Signature("DrWatson", "no@mail", round(Int, time()), 0)
        LibGit2.commit(repo, "Initial commit"; author=sig, committer=sig)

```

to initialize the repository on a given `path`. Now, I want to ensure the default branch of this repository is always `main` because of some subsequent steps.

How do I do it without expecting `git` to be accessable from the command line, i.e., without doing `run(git -m ...)`?

Doing

```julia
default = LibGit2.branch(repo)
ERROR: GitError(Code:EUNBORNBRANCH, Class:Reference, reference 'refs/heads/master' not found)

```

already errors. So I guess a secondary question is _how do I even get the name of the default branch using `LibGit2`?_

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [July 17, 2022, 12:02pm UTC](https://discourse.julialang.org/t/how-to-change-branch-name-using-libgit2/84353/2 "2022-07-17T12:02:24Z")

</div>

> [@Datseris](#):
>
> How do I do it without expecting `git` to be accessable from the command line, i.e., without doing `run(git -m ...)`?

One option is to use Git.jl which uses JLLs to ship command line git. I don’t know how to do it with LibGit2.

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [July 17, 2022, 12:18pm UTC](https://discourse.julialang.org/t/how-to-change-branch-name-using-libgit2/84353/3 "2022-07-17T12:18:52Z")

</div>

How do you handle paths in Git.jl? One reason I decided to go with LibGit2.jl is because it creates this “git repo” object, and what is my `pwd()` doesn’t matter. I suspect that with Git.jl I would have to change path to the `path` I create the `init(path)`, right?

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [July 17, 2022, 12:22pm UTC](https://discourse.julialang.org/t/how-to-change-branch-name-using-libgit2/84353/4 "2022-07-17T12:22:02Z")

</div>

> [@ericphanson](#):
>
> which uses JLLs to ship command line git

Isn’t this unecessary? I thought it is guaranteed that there is git installed if Julia is installed, because my understanding of the package system is that it does not work without git. That’s what LibGit2 does, it interfaces that git installation I thought.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [July 17, 2022, 12:57pm UTC](https://discourse.julialang.org/t/how-to-change-branch-name-using-libgit2/84353/5 "2022-07-17T12:57:07Z")

</div>

LibGit2 is a git implementation, not an interface to one, IIUC. My point is you can use Git.jl to use a command-line one, so you can use usual commands like the first example from the [readme](https://github.com/JuliaVersionControl/Git.jl#examples):

```julia
julia> using Git

julia> run(`$(git()) clone https://github.com/JuliaRegistries/General`)

```

By using Git.jl you don’t have to assume the user has command-line git installed, but you can use the familiar command line interface.
