How I repaired one of my corrupted git repository on gogs git server

While trying to commit my codes to my Raspberry Pi gogs git server, I got a rejection:

 ! [remote rejected] master -> master (missing necessary objects)
error: failed to push some refs to 'http://192.168.1.115:3000/org/a-project.git'

In addition to that, I got a HTTP 500 when I try to view the repository details page via the dashboard.

Since I can view my other repositories on my gogs git server, something must be wrong with a-project.git.

Given that, here are the steps that I took to rectify the issue.

Checking the logs on my gogs git server to find the root of the problem

Firstly, I went ahead to check the log file on my gogs git server. In order to do so, I went into the log folder and view the most recent log:

cd /opt/gogs/log/
sudo cat gogs.log

Given that, I was able to find a log status suggesting that the repository was corrupted:

2022/05/26 19:47:46 [...ules/context/repo.go:384 func1()] [E] CommitsCount: exit status 128 - error: inflate: data stream error (invalid code -- missing end-of-block)
fatal: loose object eeba0893914f4e4c8f16da67fd4b1acd9e9041a3 (stored in ./objects/ee/ba0893914f4e4c8f16da67fd4b1acd9e9041a3) is corrupt

When I saw such a message, I knew that one of the files in a-project.git/objects is corrupted.

Copying a healthy git repository over to the gogs server

After getting the root to the problem, the next step is to solve it.

Locating the same objects folder on my local git repository

Since I am the sole developer, I have a healthy git repo on my development machine. In order to find the objects folder to copy over to my gogs git server, I simply change directory into the .git folder where the root of the source codes of my project sits.

Locating the objects folder on my gogs git server

Next, I proceeded to locate the objects directory on my gogs git server. Since gogs git server saves the repository in the home directory, I am able to find my objects folder in /home/gogs/gogs-repositories/org/a-project.git

Transferring the objects directory from my development machine to my gogs git server

Once I have an idea of where the objects directory resides locally and remotely, I proceeded to run the following commands on my development machine to get the objects folder to the home directory of my admin user on the git server:

cd /folder/to/project-source/.git
scp -r objects adminuser@192.168.1.115:/home/adminuser/

When everything is transferred across, I sshed into my gogs server:

ssh adminuser@192.168.1.115

After getting into my gogs server as the admin user, I make a backup of the objects folder with the corrupted record:

cd /home/gogs/gogs-repositories/org/a-project.git
# Make a backup first just in case
sudo mv objects objects-bak
sudo mv /home/adminuser/objects ./

Given that, my git repository on my gogs server is no longer corrupted. In addition to that, whatever I had committed to my local repository were also transferred over to the remote repository.

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.