Remotes in GitLab

Last updated on 2025-05-22 | Edit this page

Overview

Questions

  • How do I safely back up my work to a remote site?
  • How do I share my changes with others on the web?

Objectives

  • Explain what remote repositories are and why they are useful.
  • Push to or pull from a remote repository.

Version control really comes into its own when we begin to collaborate with other people. We already have most of the machinery we need to do this; the only thing missing is to copy changes from one repository to another.

Systems like Git allow us to move work between any two repositories. In practice, though, it is easiest to use one copy as a central hub, and to keep it on the web rather than on someone’s laptop. Most programmers use hosting services like GitHub, Bitbucket or GitLab to hold those main copies.

Let us start by sharing the changes we have made to our current project with the world. To this end we are going to create a remote repository that will be linked to our local repository.

1. Create a remote repository


Log in to USGS GitLab, then click on the icon in the top right corner to create a new project called vampires-and-werewolves:

Creating a Project on GitLab (Step 1)

Select Create blank project

Creating a Project on GitLab (Step 2)

Name your project “vampires-and-werewolves”, select your username as the namespace, uncheck “Initialize repository with a README”, and then click Create project.

Note: Since this repository will be connected to a local repository, it needs to be empty. That is why “Initialize repository with a README” needs to be unchecked. See the “GitLab README files” exercise below for a full explanation of why the project needs to be empty.

Creating a Project on GitLab (Step 3)

As soon as the repository is created, GitLab displays a page with a URL and some information on how to configure your local repository:

Creating a Project on GitLab (Step 4)

This effectively does the following on Gitlab’s servers:

BASH

$ mkdir vampires-and-werewolves
$ cd vampires-and-werewolves
$ git init

If you remember back to the earlier episode where we added and committed our earlier work on mars.txt, we had a diagram of the local repository which looked like this:

The Local Repository with Git Staging Area

Now that we have two repositories, we need a diagram like this:

Freshly-Made GitLab Repository

Note that our local repository still contains our earlier work on mars.txt, but the remote repository on GitLab appears empty as it does not contain any files yet.

2. HTTPS Setup


Before Dracula can connect to a remote repository, he needs to set up a way for his computer to authenticate with GitLab so it knows it is him trying to connect to his remote repository.

We are going to set up an “Access token” that we can use to authenticate to GitLab.

In GitLab, click on your user icon and then Preferences.

Where to Find User Settings on GitLab

Once you are in your User Settings, click on Access tokens. If you already have a personal access token and you have it saved, you do not need to follow these steps. If you do not have a personal access token, click on Add new token.

Screenshot of Access tokens page and Add new token button

Add a token name that will be meaningful to you. If you are setting this up as your primary access token for GitLab, you will probably want to select all of the scopes. These scopes establish what you are able to do in GitLab with this personal access token. You may also want to delete the expiration date. If removed, GitLab will automatically set the expiration date to the maximum of one year from the day created. Then, click Create personal access token.

Add a personal access token screen in GitLab with Token name entered and all scopes selected

You will be presented with your new personal access token. Make sure you save it some place secure since you will not be able to access it again through GitLab.

Screenshot of GitLab's presentation of your new personal access token

See the GitLab Documentation for more information on personal access tokens.

When you start interacting with the remote from your computer, if you have not already saved your personal access token, you will be prompted to enter a username and password. The prompt may appear in your command prompt or you may get a pop up on your machine. Your username will be your email address and the password will be your personal access token. See the Password Manager spoiler below for more information on saving your personal access token and handling token expiration.

3. Connect local to remote repository


Now we connect the two repositories. We do this by making the GitLab repository a remote for the local repository. The home page of the repository on GitLab includes the URL string we need to identify it:

Where to Find Repository URL on GitLab

Click on the clipboard icon under ‘Clone with HTTPS’ to use the HTTPS protocol.

HTTPS allows you to communicate with GitLab using the HTTPS protocol. This approach tends to be a little simpler and allows you to use a Personal Access Token (similar to a password) to authenticate. You can use the same Personal Access Token across multiple machines.

SSH is considered slightly more secure and requires setting up a public and a private key. There is a little more overhead to using SSH over HTTPS, especially if working on more than one machine, which is why we teach the HTTPS method in this Lesson. That being said, it is not too hard to configure your account to use SSH and the instructions are available at https://docs.gitlab.com/ee/user/ssh.html.

With the URL copied from the browser, go into the local vampires-and-werewolves repository, and run this command:

BASH

$ git remote add origin https://code.usgs.gov/vdracula/vampires-and-werewolves.git

Make sure to use the URL for your repository rather than Vlad’s: the only difference should be your username instead of vdracula.

origin is a local name used to refer to the remote repository. It could be called anything, but origin is a convention that is often used by default in git and GitLab, so it is helpful to stick with this unless there is a reason not to.

We can check that the command has worked by running git remote -v:

BASH

$ git remote -v

OUTPUT

origin  git@code.usgs.gov:vdracula/vampires-and-werewolves.git (fetch)
origin  git@code.usgs.gov:vdracula/vampires-and-werewolves.git (push)

We will discuss remotes in more detail in a future episode, while talking about how they might be used for collaboration.

4. Push local changes to a remote


Now that authentication is setup, we can return to the remote. This command will push the changes from our local repository to the repository on GitLab:

BASH

$ git push origin main

Since Dracula set up a personal access token, it will prompt him for it. If you have already saved your personal access token in Git, it may not prompt for a password.

OUTPUT

Enumerating objects: 16, done.
Counting objects: 100% (16/16), done.
Delta compression using up to 8 threads.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (16/16), 1.45 KiB | 372.00 KiB/s, done.
Total 16 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
To https://code.usgs.gov/vdracula/vampires-and-werewolves.git
 * [new branch]      main -> main

Proxy

If the network you are connected to uses a proxy, there is a chance that your last command failed with “Could not resolve hostname” as the error message. To solve this issue, you need to tell Git about the proxy:

BASH

$ git config --global http.proxy http://user:password@proxy.url
$ git config --global https.proxy https://user:password@proxy.url

When you connect to another network that does not use a proxy, you will need to tell Git to disable the proxy using:

BASH

$ git config --global --unset http.proxy
$ git config --global --unset https.proxy

If your operating system has a password manager configured, git push will try to use it when it needs your username and password. For example, this is the default behavior for Git Bash on Windows. If you want to type your username and password at the terminal instead of using a password manager, type:

BASH

$ unset SSH_ASKPASS

in the terminal, before you run git push. Despite the name, Git uses SSH_ASKPASS for all credential entry, so you may want to unset SSH_ASKPASS whether you are using Git via SSH or https.

You may also want to add unset SSH_ASKPASS at the end of your ~/.bashrc to make Git default to using the terminal for usernames and passwords.

If your personal access token was saved in your password manager and it expires, you will need to generate a new personal access token and open the password manager to delete the saved credential. Then, Git will prompt you for the new password on your next git push.

Our local and remote repositories are now in this state:

GitLab Repository After First Push

The ‘-u’ Flag

You may see a -u option used with git push in some documentation. This option is synonymous with the --set-upstream-to option for the git branch command, and is used to associate the current branch with a remote branch so that the git pull command can be used without any arguments. To do this, simply use git push -u origin main once the remote has been set up.

We can pull changes from the remote repository to the local one as well:

BASH

$ git pull origin main

OUTPUT

From https://code.usgs.gov/vdracula/vampires-and-werewolves
 * branch            main     -> FETCH_HEAD
Already up-to-date.

Pulling has no effect in this case because the two repositories are already synchronized. If someone else had pushed some changes to the repository on GitLab, though, this command would download them to our local repository.

GitLab GUI

Browse to your vampires-and-werewolves repository on Gitlab. On the right side menu under Project Information, find and click on the text that says “XX commits” (where “XX” is some number). Hover over, and click on, the two buttons to the right of each commit. Additionally, click into each commit. What information can you gather/explore from these buttons and views? How would you get that same information in the shell?

The left-most button (with the picture of a clipboard) copies the full identifier of the commit to the clipboard. In the shell, git log will show you the full commit identifier for each commit.

The right-most button lets you view all of the files in the repository at the time of that commit. To do this in the shell, we would need to checkout the repository at that particular time. We can do this with git checkout ID where ID is the identifier of the commit we want to look at. If we do this, we need to remember to put the repository back to the right state afterwards!

When you click on the commit name, you will see all of the changes that were made in that particular commit. Green shaded lines indicate additions and red ones removals. In the shell we can do the same thing with git diff. In particular, git diff ID1..ID2 where ID1 and ID2 are commit identifiers (e.g. git diff a3bf1e5..041e637) will show the differences between those two commits.

Uploading files directly in GitLab browser

GitLab also allows you to skip the command line and upload files directly to your repository without having to leave the browser. When you are in Code –> Repository, you can click the + button in the toolbar at the top of the file tree, then click Upload File under This directory.

GitLab Timestamp

Go to the repo you just created on GitLab and check the timestamps of the files. How does GitLab record times, and why?

GitLab displays timestamps in a human readable relative format (i.e. “22 hours ago” or “three weeks ago”). However, if you hover over the timestamp, you can see the exact time at which the last change to the file occurred.

Push vs. Commit

In this episode, we introduced the “git push” command. How is “git push” different from “git commit”?

When we push changes, we are interacting with a remote repository to update it with the changes we have made locally (often this corresponds to sharing the changes we have made with others). Commit only updates your local repository.

GitLab README files

In this episode we learned about creating a remote repository on GitLab, but when you initialized your GitLab repo, you did not add a README.md file. If you had, what do you think would have happened when you tried to link your local and remote repositories?

In this case, we would see a merge conflict due to unrelated histories. When GitLab creates a README.md file, it performs a commit in the remote repository. When you try to pull the remote repository to your local repository, Git detects that they have histories that do not share a common origin and refuses to merge.

BASH

$ git pull origin main

OUTPUT

warning: no common commits
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://code.usgs.gov/vdracula/vampires-and-werewolves
 * branch            main     -> FETCH_HEAD
 * [new branch]      main     -> origin/main
fatal: refusing to merge unrelated histories

You can force git to merge the two repositories with the option --allow-unrelated-histories. Be careful when you use this option and carefully examine the contents of local and remote repositories before merging.

BASH

$ git pull --allow-unrelated-histories origin main

OUTPUT

From https://code.usgs.gov/vdracula/vampires-and-werewolves
 * branch            main     -> FETCH_HEAD
Merge made by the 'recursive' strategy.
README.md | 1 +
1 file changed, 1 insertion(+)
create mode 100644 README.md

Key Points

  • A local Git repository can be connected to one or more remote repositories.
  • Use the HTTPS protocol to connect to remote repositories.
  • git push copies changes from a local repository to a remote repository.
  • git pull copies changes from a remote repository to a local repository.