Table of Contents

Git Usage


Basics

Some terms that you'll need to understand in context (several of these are commands for the git CLI tool):

It may be helpful to remember that each repository is a kind of with a head pointer pointing to a particular commit. Each time you “checkout” something, the head of your local repository is reassigned to another branch or another commit on the same branch.


How We Use It


GitLab lets us keep our code projects and board designs (Eagle Files are just XML, so they work great in git) in a central location and keep track of versioning. We have a BYU-Holography group on GitLab which contains the following repositories, among others.

We have switched from GitHub (which is more commonly used but somewhat more expensive) to GitLab in order to have private repositories. You will either need a GitLab account (which will be added to the group) or have your SSH key in one of ours in order to access the repositories there.


Using GitHub Desktop (Windows)


Creating a New Repository

On the CLI, there's a few steps to this:

The Desktop Interface makes this easier for us:

  1. With the program open, hit this plus to open the popup for creating/adding/cloning repos:
  2. In this page, give it a name and specify where it is on the computer (can be local or remote, and can be an empty folder), and hit “create”. It will always be in a floder matching the repo name.
  3. Now you'll see this page with the local repo. GitHub Desktop keeps track of changes for you, so you won't have to “add” anything. If you see this dot, you have changes.
  4. Here on the changes page, you'll see everything that has changed since the last commit. Since we just created this repo, everything is new. Every commit has to be given a “message” or name. Give the first one a name and commit it.
  5. With that commit done, now we need to get this repo up onto GitHub. If you select a commit from the list, it'll show you the changes made in that commit. If you do this on the CLI, you'll have to create the repo online first. The GUI will take care of it for us, we just have to “publish” it.
    1. Since we're now using GitLab instead, we'll have to do just a touch of manual work to finish the setup. First, create the repo online at https://gitlab.com/BYU-Holography. Then, right-click the local repository in GitHub Desktop and select “Open in Git Shell”. Once it comes up, the command you want should be,replacing [URL] with the URL of the repo:
      git remote add origin [URL]


  6. Here you'll give the GitHub repo a name (doesn't necessarily have to match the folder it's stored in), and a public description. Also, make sure you select which account it will be stored under (it will default to whomever's signed into the program, but we can select “BYU-Holography” from the dropdown).


And with that, it's DONE!

Making Changes/Updating Code

  1. With each new change, you have to “add” the changes to the next commit. The GUI takes care of that for us by just telling us when there's changes by giving us this dot:
  2. Clicking on the “Changes” button takes you to the commit page. Here, you can select which changes are included in this commit (clicking on them will show you the details), give the commit a name (required) and description (optional), and commit it to your working branch.
  3. Important to note is that this only committed the changes to the local repo. Now it needs to be “push”ed to the origin (GitHub/GitLab). The GUI combines “push” and “pull” into one “sync” button. Hit that after each commit, and anytime new changes may have been pushed to the origin by someone else

Fixing Mistakes

Getting the SSH Key for GitHub Desktop

The program should generate its own SSH key during install or first run. The key pair should be found in

C:\Users\[username]\.ssh\

and have two files: “github_rsa” and “github_rsa.pub”. Don't touch the one without an extension: that's the private key. If you load the public key into your GitLab account (or that of anyone with access to the group), you'll be able to access the repositories over SSH wihtout having to log in. Over HTTPS, you always have to log in with a username and password.


As a side note, GitHub desktop will NOT let you have multiple branches active on the same machine. From the CLI, you can have a different branches in different folders, you'll just have to rememeber which is which (or, if you're REALLY good, you can use git worktree, but that's still somewhat experimental)

Using the command line

Most of this is adapted from http://gitlab.com/help/gitlab-basics/README.md.

Setup

Install git if it isn't already installed.

On your shell, type the following command to add your username:

git config --global user.name "YOUR_USERNAME"

Then verify that you have the correct username:

git config --global user.name

To set your email address, type the following command:

git config --global user.email "your_email_address@example.com"

To verify that you entered your email correctly, type:

git config --global user.email

Next, generate an SSH key pair if there is not already one on your computer. (https://gitlab.com/help/ssh/README.md). To add the public key to your gitlab account, copy it, go to your User Settings > SSH Keys, paste it in the Key box, add a title, and save it (screenshots of this process here: https://gitlab.com/help/gitlab-basics/create-your-ssh-keys.md).

Starting a project

Setting up a local branch for the first time

To create a local copy of the repository in “directory”, run this command. You can leave out the directory argument, and it will make a new directory automatically.

git clone git@gitlab.com:BYU-Holography-Leia/image-capture.git directory

Checking out

To checkout master, do this:

git checkout master

Create branch

If you're going to make changes, first make a branch of the project, then when you're ready, merge that branch back in.

Create branch:

git checkout -b branch_name

or

git branch branch_name
git checkout branch_name

If you do this and then want to work on the master branch again, then make sure to commit changes to your new branch before checking out master again.

Merging

When you have made your changes in a branch and want to put them into the master branch, you do a merge. First get back to master, by checking it out, then run git merge. This merges the branch specified in the command into the currently checked out branch.

git merge branch_name

In the case that your new branch is directly ahead of the master, it will just do a fast-forward, applying all those changes. If master has had commits since you branched from it, then you will need to resolve the differences.

After merging, you can delete the old branch (if you're done working on it):

git branch -d branch_name

This will delete your local branch but not the remote. To do that, add a colon before the branch name.

Merge conflicts

https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging