Git Reference Guide

Introduction to Git

Git is a distributed version control system used to track changes in source code during software development.

NCICS GitLab

Create a Repository

Create a New Project:

Pay special attention to the project group and visibility level.

Clone the Repository

Note: Use SSH if you have already set up SSH Credentials. Use HTTPS if you would like to use your login information (preferred on remote, less secure server environments).

Using the command line:

git clone git@gitlab.cicsnc.org:[your-repo-path-here].git

Configuration

git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"

Basic Git Commands

  • git init: Initializing a Repository

  • git clone <repository_URL>: Cloning a Repository

  • git add <filename>: Adding Changes

  • git add -A: Adding ALL Changes

  • git commit -m "Commit Message": Committing changes

  • git status: Checking status of current git repo

  • git log: Viewing Commit History

Branching and Merging

  • git branch <new-branch>: create a new branch based on the current HEAD
  • git branch <new-branch> <base-branch>: create a new branch based on some existing one
  • git switch <branch-name>: switch to
  • git merge <branch_name>: merge into current branch
  • git branch -d <branch_name>: delete

Remote Repositories

Remote repositories are shared locations for storing version controlled code that often allow for collaboration. Examples are GitHub and GitLab.

  • git remote -v: See linked remote repositories (verbose)
  • git remote add <remote_repository_name> <remote_URL>: add a remote repository
  • git push -u <remote_repository_name> <branch_name>: push changes to a remote repository
  • git pull <remote_repository_name> <branch_name>: pull changes from a remote repository
  • git pull <remote_repository_name> <branch_name> -a: pull all changes from a remote repository, including new branches

Note: If you only have one remote repository called origin, you do not need to include the ; git will use origin by default in most cases.

Setting up Overleaf as an Additional Remote

It is often helpful to be able to push/pull/update content in multiple locations. This is especially important for connecting Overleaf and GitLab. For more information: Using Git and Github with Overleaf

Note: If you registered for Overleaf using your Google Account (which you probably did), you will need to set up a password on Overleaf to pull from the Overleaf repo. To do so, go to your Home screen in Overleaf, access the Account Dropdown and Reset/Change/Configure Settings.

  1. Start a project on Overleaf. This will create a Git Repo on Overleaf’s servers.

  2. Make sure you have an existing project on GitLab that you have cloned to your development environment. Setting up your project on GitLab then cloning it should have already set up your remote in your environment.

To confirm, run git remote -v. You should see your GitLab remote listed.

  1. Add Overleaf as an additional remote. To do so, run git remote add overleaf <YOUR OVERLEAF PROJECT GIT URL>. You can find your Overleaf project Git URL in Overleaf by selecting the Menu button in the top right corner when you are inside the project and clicking on the Git button under the Sync subheading. The https://git.overleaf.com/<project-id> url is what you need to add your project as a remote.

  2. Double check you have added the remote correctly. To do so, run git remote -v. You should see both your GitLab and Overleaf remote.

  3. Pull the content from your Overleaf project to your environment. To do so, run git pull overleaf master --allow-unrelated-histories. This will likely generate a merge conflict and notify you accordingly. Run :q to leave the editor.

  4. Make whatever changes and adjustments are needed on the project.

  5. Add and commit changes. To do so, run git add -A && git commit -m "configured project"

  6. Push changes to GitLab. Run git push.

  7. Push changes to Overleaf. Run git push overleaf main:master.

  8. Check your Overleaf project to confirm that the changes have arrived there.

Note: You do not need to Add, Commit, or Push on Overleaf with GitLab (GitHub sync supports this). To bring changes made in Overleaf to a different environment, simply pull the Overleaf Repo to that environment: git pull overleaf master.

Helpful Git Tutorials & Resources