Recently, I showed you how to host your own local code repository with Gogs (see: How to host your own local Git repository with Gogs). This tool is a great way to deploy a code repository server to your data center, so your developers don’t have to work with code outside of your network.
Once you have Gogs up and running, you’ll want to know how to actually use it. For those developers who already understand how to use the Git command line, this will actually be quite easy. In fact, it’s very similar to using GitHub; if you can push and pull code to that third-party service, you’ll be doing the same with Gogs in no time.
I’m going to walk you through that process, so you and your developer team can work with Gogs from the Git command line.
SEE: Top 5 programming languages for systems admins to learn (free PDF) (TechRepublic)
What you’ll need
Obviously, you’ll need your Gogs server up and running. You’ll also need Git installed on a desktop or laptop, an SSH public key, and a user account on Gogs.
How to add an SSH key in Gogs
The first thing to be done is to copy your user SSH key into your Gogs account. If you don’t already have an SSH key, they’re quite simple to create. Log in to your desktop (I’m demonstrating on Pop!_OS) and issue the command:
This command will create a public key, named id_rsa.pub in the ~/.ssh directory. To view the content of that file, issue the command:
Copy the contents of that file and log in to your Gogs account. From within the Gogs main page, click your profile icon in the upper-right corner and click Your Settings. In the resulting window, click SSH Keys in the left sidebar (Figure A).
In the next window, click the blue Add Key, give the key a name, paste your public key in the Content window (Figure B).
Click the green Add Key button and your SSH key is saved.
How to create a new repository in Gogs
The next thing you’re going to do is create a new repository to house your code. To do that, go back to the Gogs main page, click the + dropdown, and select New Repository. File out the information for your new repository, and click Create Repository (Figure C).
How to clone your new Gogs repository
Once your repository has been created, you’ll need to locate its associated SSH address. To locate that address, go to the new repository page and click the SSH button to the right of the New File/Upload File buttons (Figure D).
Copy the address, which will be in the form:
Back at your desktop/laptop, open a terminal window and issue the command:
git clone ssh://git@SERVER/USER/REPOSITORY.git
SERVER is the IP address or domain of the Gogs server
USER is your Gogs username
REPOSITORY is the name of the repository you just created
You should now see a new directory with the same name as your repository. Change into that directory with the command:
Where DIRECTORY is the name of the newly-created directory.
How to add code and push it to the repository
Now that you have your repository cloned to your local machine, add (or create) all the code you need for the repository into the new directory and issue the command:
git add .
Now that Git knows about the new files, let’s commit them with the command:
git commit -m "Added initial code to repository"
Of course, you can change the commit information (between the quotes) to whatever you need–just make sure it’s informative.
Once that command completes, push the code to the server with the command:
You should be prompted for the passphrase for your SSH key (and not your Gogs username). Upon successful authentication, the code will get pushed to the Gogs repository and you’re set.
That’s all there is to connecting to your in-house Gogs repository from the Git command line. With Gogs in your data center, you won’t have to worry about your company code getting leaked via a third-party service.
Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.