# Cannot push Git commits by LibGit2

**URL:** https://discourse.julialang.org/t/cannot-push-git-commits-by-libgit2/104076
**Category:** General Usage
**Created:** [September 20, 2023, 6:24pm UTC](https://discourse.julialang.org/t/cannot-push-git-commits-by-libgit2/104076 "2023-09-20T18:24:20Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)
#### Post date: [September 20, 2023, 6:24pm UTC](https://discourse.julialang.org/t/cannot-push-git-commits-by-libgit2/104076/1 "2023-09-20T18:24:20Z")

</div>

I am trying to write a script that commits changes in files and pushes them to the upstream using `LibGit2`, but `push()` is not working.

I am able to create a `LibGit2.GitRepo`, add changes to it, and commit the changes by

```julia
repo = LibGit2.GitRepo(repo_path)
LibGit2.add!(repo, ".")
LibGit2.commit(repo, "Commit message")

```

`git log` in the terminal confirms that the commit is correctly created with the commit message.

Then, if I attempt to push the commit by

```julia
LibGit2.push(repo)

```

I get the following error:

```julia-repl
SSH host verification: the identity of the server `<URL of my organization's GitHub Enterprise>:22` does not match its known hosts record. Someone could be trying to man-in-the-middle your connection. It is also possible that the server has changed its key, in which case you should check with the server administrator and if they confirm that the key has been changed, update your known hosts file.
ERROR: GitError(Code:ERROR, Class:Net, user cancelled hostkey check)
Stacktrace:
 [1] macro expansion
   @ ~/pkg/julia/julia-1.9/usr/share/julia/stdlib/v1.9/LibGit2/src/error.jl:111 [inlined]
 [2] push(rmt::LibGit2.GitRemote, refspecs::Vector{AbstractString}; force::Bool, options::LibGit2.PushOptions)
   @ LibGit2 ~/pkg/julia/julia-1.9/usr/share/julia/stdlib/v1.9/LibGit2/src/remote.jl:324
 [3] push
   @ ~/pkg/julia/julia-1.9/usr/share/julia/stdlib/v1.9/LibGit2/src/remote.jl:321 [inlined]
 [4] push(repo::GitRepo; remote::String, remoteurl::String, refspecs::Vector{AbstractString}, force::Bool, credentials::Nothing, callbacks::Dict{Symbol, Tuple{Ptr{Nothing}, Any}})
   @ LibGit2 ~/pkg/julia/julia-1.9/usr/share/julia/stdlib/v1.9/LibGit2/src/LibGit2.jl:349
 [5] push(repo::GitRepo)
   @ LibGit2 ~/pkg/julia/julia-1.9/usr/share/julia/stdlib/v1.9/LibGit2/src/LibGit2.jl:325
[...]

```

This is odd, because I can `git push` from the terminal without any issue.

I’m wondering if anyone knows a solution to this problem. The Julia version is 1.9.3.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [September 20, 2023, 6:43pm UTC](https://discourse.julialang.org/t/cannot-push-git-commits-by-libgit2/104076/2 "2023-09-20T18:43:48Z")

</div>

Just a guess:  
There is a `known_hosts` file in `...\share\julia\stdlib\v1.9\LibGit2\test\`  
You may search for it in your `HOME\.julia` folder.  
Perhaps there is a wrong or old server key in this file for your remote github/lab server. You can delete the line and try again.  
Typically, the file `known_hosts` is in `HOME\.ssh\known_hosts` and stores the public keys of your remote servers you are connecting via ssh.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [September 20, 2023, 7:37pm UTC](https://discourse.julialang.org/t/cannot-push-git-commits-by-libgit2/104076/3 "2023-09-20T19:37:31Z")

</div>

I don’t know the answer but generally I would recommend using an external `git` rather than `LibGit2`. If you need it to work on computers where you don’t know if `git` is installed you can use the `Git` package.

---

<div class="post-metadata">

### Author: ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)
#### Post date: [September 21, 2023, 2:21pm UTC](https://discourse.julialang.org/t/cannot-push-git-commits-by-libgit2/104076/4 "2023-09-21T14:21:38Z")

</div>

@oheil, I am able to find a `known_hosts` file at `~/pkg/julia/julia-1.9/stdlib/LibGit2/test/known_hosts`. But I guess this is only the file used during test, as the name of the directory suggests. Even if I replace it with `.ssh/known_hosts`, I get the same error.

@GunnarFarneback, I’m wary of using the `Git` package because it hasn’t been updated for last two years. `LibGit2` is already included in Julia and is supposed to provide the functions I need, so it will be nice to achieve what I want using `LibGit2`.

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [September 21, 2023, 2:32pm UTC](https://discourse.julialang.org/t/cannot-push-git-commits-by-libgit2/104076/5 "2023-09-21T14:32:28Z")

</div>

> [@wsshin](#):
>
> I’m wary of using the `Git` package because it hasn’t been updated for last two years.

Funny, the last commit I see in [GitHub - JuliaVersionControl/Git.jl: Use command-line Git in your Julia packages](https://github.com/JuliaVersionControl/Git.jl) is from last February.

> [@wsshin](#):
>
> `LibGit2` is already included in Julia and is supposed to provide the functions I need

“supposed” being the key word here: libgit2 is not a fully-equivalent replacement of CLI git.

---

<div class="post-metadata">

### Author: ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)
#### Post date: [September 21, 2023, 3:00pm UTC](https://discourse.julialang.org/t/cannot-push-git-commits-by-libgit2/104076/6 "2023-09-21T15:00:12Z")

</div>

Sorry, my recollection was about the [GitREPL](https://github.com/JuliaVersionControl/GitREPL.jl) package, not Git.

If I don’t find a solution eventually, I will give the Git package a try.

---

<div class="post-metadata">

### Author: ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)
#### Post date: [September 21, 2023, 3:05pm UTC](https://discourse.julialang.org/t/cannot-push-git-commits-by-libgit2/104076/7 "2023-09-21T15:05:15Z")

</div>

I found a related [discussion](https://github.com/libgit2/libgit2/discussions/5928). The person says libgit2 does not verify hosts, so s/he had to do it using libssh2.

I also find that pushing using libgit2 uses push options: [c - How to push (with libgit2) - Stack Overflow](https://stackoverflow.com/questions/28055919/how-to-push-with-libgit2). I’m wondering if I need to set [`LibGit2.PushOptions`](https://docs.julialang.org/en/v1/stdlib/LibGit2/#LibGit2.PushOptions).

---

<div class="post-metadata">

### Author: ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)
#### Post date: [September 25, 2023, 4:29am UTC](https://discourse.julialang.org/t/cannot-push-git-commits-by-libgit2/104076/8 "2023-09-25T04:29:23Z")

</div>

I found a solution. First, in order to use LibGit2, I had to have a properly generated SSH private–public key pair. I describe the procedure [here](https://discourse.julialang.org/t/local-registry-rsa-key-problem/78188/5), which is hugely helped by @GunnarFarneback’s analysis on the LibGit2-related issue.

Then, I was able to push commits to the remote Git hosting service simply by

```julia
LibGit2.push(repo; refspecs=["refs/heads/main"])

```

where `main` in `"refs/heads/main"` is my Git branch name to push.
