How Do You Upload Code to a GitLab Project from a Server That Does Not Have a Desktop UI?

Problem scenario
You have a Linux server with the command prompt but no GUI.  A separate server hosts GitLab.  How do you configure the first character-based server to use GitLab?

This assumes that you have deployed GitLab; if you need directions for this, see this posting.  This assumes that you have created a project in GitLab.  If you need directions for this, see this posting.

1.a.  From the web UI of the GitLab server, obtain the repository URL for the project.  It will look something like this:


If you cannot readily find this repository URL, you can log into the web UI of GitLab, go to Projects or "Projects" -> "Your projects".  Click on the project you want.  In the middle top of the screen there should be a field in between a "Fork"  button and an icon of a cloud/download button.  It will say "SSH" or "HTTP". Copy this string.  

1.b.  Go to the backend of the GitLab server.  Once you are at the character terminal, find the project on the backend.  To do this search for name of your .git file with a command like this:
sudo find / -name my-contint-project.git # but substitute the "my-contint-project.git" with the name of your git file (as the name should have been deteremined in step 5.a).

1.c.  Make a note of the full directory path found in step 1.b.

2.a.  Remain logged into the GitLab web UI.

2.b.  In the web UI for GitLab, click on the Project that has the repository you want (unless you are still at the "Details" screen where the Project's SSH or HTTP URL is visible).

2.c.  On the left click on "Settings".

2.d.  Click on "Repository" under "Settings."  

2.e.  Click on the "Expand" button for "Deploy Keys".

2.f.  Provide an arbitrary phrase in the "Title" field.

2.g.  Consider which server and which user will upload code to this GitLab project.  Go to that server and log in as that user even if it is a local user to that server.  Find the content of the file for this user (e.g., /home/jdoe/.ssh/  If no file exists yet, go to the backend of the Linux server, log in as jdoe (or the user of your choice), run 'ssh-keygen -t rsa -P ""' and press enter to the next prompt.

Copy the contents of the file for this user on the server that will connect to the GitLab server and upload code to the GitLab project that you just created.  For the "Key" field input that is on the GUI of the GitLab server after step 2.f., paste the content of the file.

2.h.  Check the option for "Write access allowed"

2.i.  Click the green "Add key" button.

3.a.  From the server you want to upload code from, go to the Linux command prompt.  Log in as the user you want to upload  code as (e.g., jdoe).

3.b.  Verify git is installed (run "git --version").  Install it if necessary ("sudo apt-get -y install git" for a Debian distribution or "sudo yum -y install git" for a RedHat distribution).

3.c.  Draft a command like the one below (with "git clone" preceding the URL constructor obtained in step 1.a. above).    

git clone git@"<GitLabFQDN>":"<pathNotedin1.c>"/nameOfRepo.git

Substitute "<GitLabFQDN>" with either the FQDN of the GitLab server or the IP address.  Replace "<pathNotedin1.c>" with the  path you obtained in step 1.c above.  Substitute "nameOfRepo.git" with the name of the repository found in step 1.a.

Run this drafted command after you did the substitutions.

You may be prompted if you want to continue.  Choose "yes" with no quotes.  If you run the command above and you are prompted for a password, then the keys were probably not properly loaded to the GitLab GUI.  It is inadvisable, but you can continue with the git user.  If you do you may want to change the git user's password.  There are other ways of circumventing this problem (and this is extremely inadvisable to change the git user's password on the back end of the GitLab server because there are supported methods of using git commands). If you can change the git user's password (e.g, you are implementing a proof of concept or experimenting for the sake of learning with no sensitive data on a temporary server), log into the backend of the GitLab server.  Run this:  sudo passwd git

3.d. On the non-GitLab server, use this command:  cd nameOfRepo # where "nameOfRepo" is the name of the repository you just clones.  

3.e.  Create a file. For this example we'll use "" with the content of these two lines:

echo $var1

3.f.  Run this command:  git add .

3.g.  Run this command but replace "jdoe" with the username that will interact with the GitLab project:  git config --global "jdoe"

3.h.  Run this command:  git commit -m "Added a basic file."

3.i.  Run this command: git push origin master

Now you have added a file to the GitLab project.  Now you can add more files to the GitLab project via the Git command line.

Leave a comment

Your email address will not be published. Required fields are marked *