4  Git

There are a veritable boatload of introductory guides out there to Git and how to use it with your host of choice (e.g. GitHub, or GitLab). This is not among said guides. If you’re a Git n00b and an R person, I highly recommend Happy Git with R (Bryan, Hester, and The STAT 545 TAs 2022). If that’s not you, a simple internet search will reveal a panoply of excellent options.

4.1 How to add a local project to Git

I usually start projects by creating a new Git repo and cloning that locally. When I don’t, here’s how I get an existing project/folder into a Git repo (i.e. under version control).

Pure Git edition

I’m gonna assume you’ve installed and introduced yourself to Git1.

  1. Navigate to the root folder of your project in the terminal and initialize the directory as a Git repository.

    cd your-project
    git init
  2. Add the and stage the files in your project to the Git index (files you don’t want to track should be in your .gitignore).

    git add .
  3. Commit said files with a message using the -m option.

    git commit -m "Your message here."
  4. Create an empty project2 on your Git host of choice, and add its clone URL as a remote.

    git remote add origin <*REMOTE-URL*>
  5. (Optional) Verify your remotes using -v to show the remote URLs.

    git remote -v
  6. Push the changes from your local repository to your remote.

    git push -u origin main

GitHub CLI edition

Here’s how to do the same thing with a combination of command-line Git and the GitHub CLI (gh):

  1. From the project root directory, initialize it as a Git repo:

    git init -b main
  2. Stage and commit the files.

    git add . && git commit -m "Initial commit"
  3. Create a GitHub repo for your project.3

    gh repo create
  4. Select Push an existing local repository to GitHub.

  5. Follow the interactive prompts, the results of which might look something like the following:

What would you like to do? Push an existing local repository to GitHub

Path to local repository .

Repository name my-repo

Description This is my repo

Visibility Public

Add a remote? Yes

  1. Don’t forget to push!

    git push -u origin main

GitLab CLI edition

Now with the GitLab CLI (glab):

Follow the above steps 1–2 (git: init, add, commit).

  1. Create a GitLab repository for your project (by default, glab repo create will use the current directory name as the project name).

    glab repo create
  2. Push changes to your remote, and go on your merry way!

    git push -u origin main

Adapted from the GitLab CLI docs.

4.2 How to do a blobless clone

Before getting into the how (which is quite simple), let’s give a very cursory explanation of what a blobless clone actually is. A blobless clone is a type of partial clone. A partial clone is a git clone that uses the --filter option, which:

request[s] that the server sends a subset of reachable objects according to a given object filter.

Basically, you’re cloning the repo without all the baggage.

Syntax-wise, the blobless clone uses --filter=blob:none. This, as described in Get up to speed with partial clone and shallow clone, means that “the initial git clone will download all reachable commits and trees, and only download the blobs for commits when you do a git checkout.”4 In the case of the blobless clone, the unnecessary baggage is the content of old blobs (don’t worry, you’ll get the necessary blobs automatically when doing a git merge).

The how is as follows:

git clone --filter=blob:none <url>

4.3 How to exit a git diff in the terminal

Because I rarely use git diff or view git status lists in the git CLI, I always forget how to get out of the view and back to the regular session.

The answer is to use the less command:

:q

A plain q (or Q, for that matter) will usually work as well, but :q hasn’t failed me yet.

4.4 How to sync a fork with the GitHub CLI

I have a lot of forks of GitHub repos that I don’t touch very often, but, when I do, need to be brought into sync with their upstream source repo. Even though you can now sync a fork branch from GitHub’s web UI, it’s nice to be able to do this without leaving the command line. Enter the GitHub CLI, which allows you to update a fork from its parent with gh repo sync -b BRANCHNAME by supplying the fork and branch name:

gh repo sync owner/cli-fork -b BRANCHNAME

Note that you do not need to supply the parent/upstream repo name (which is automatically detected). For example, I have a fork (batpigandme/quarto-web) of the repo for the Quarto website (quarto-dev/quarto-web), which I use to make branches and submit PRs. To sync the main branch of my fork using the GitHub CLI, I’d run:

gh repo sync batpigandme/quarto-web -b main

Assuming there are no conflicts caused upstream, I’ll get the following return message:

Synced the “batpigandme:main” branch from “quarto-dev:main”


  1. If not, jump to the Install Git chapter of Happy Git with R (Bryan, Hester, and The STAT 545 TAs 2022), or your Git-setup guide of choice.↩︎

  2. Empty means no README, no template etc.↩︎

  3. gh is the GitHub CLI, which you will need in order for this step to work.↩︎

  4. If you want to know what that means, and see diagrams, etc., check out Stolee’s full post.↩︎