This is an old revision of the document!
Table of Contents
Using git
You may need to use git as a version control system in some of your CSCI or DSCI classes. It is also heavily used in industry, in open-source software development, and in Data Science.
If you have a particular task you need to complete for a class, you might check out the section below titled "I need to..."
Overview: What is git?
Version control system
Repositories
Local repository: files, staging area, repository
init, add, status, commit
Server: GitLab, GitHub, etc
Create and clone
Push and pull
Fork another repository
Setting up SSH Credentials
Common Commands
The following are common tasks to complete using git. Where we refer to GitLab, the same task can be completed with other git servers, such as GitHub, usually with very little or no change.
fork
: Make your own copy of someone else's repository on GitLab.clone
: Copy a GitLab archive to your personal computer.init
: Initialize a new git repository on your own computer. (Will not be connected to a server.)status
: Show what files are untracked, changed, staged, etc., in the current local repository.add
: Add files to your local staging area.commit
: Move files from the staging area to your local repository.pull
: Update your local archive with changes/additions made to the server. (Assumes that you have an “upstream” repository on a server to pull from.)push
: Push changes in your local repository upstream to the server repository. (Assumes that you have an “upstream” repository on a server to pull from.)log
: See a list of commits made to your repository.checkout
: Switch to a new branch.diff
: See differences between various branches.
The .gitignore File
Often there are files we do not wish to include in the git repository. Usually we don't include object files and executables, for example, since these are generated automatically by compiling your source files. (So we don't actually need copies of these.)
If you want to automatically exclude these files so that git will always ignore them, create a file called .gitignore
in the same directory as the project. In the file, enter a list of all the files you want git to ignore, one per line.
Once you have done this, you will see that (for example) a git status
command will no longer list those files as untracked, and git add -A
will not add those files, and so on.
"I need to..."
Some common tasks you might need to know how to complete for a class assignment:
- I need to make my own copy of a repository on GitLab: (In the following, you can basically replace GitLab with GitHub or most other git servers) This will involve a fork and a clone operation.
- First, fork the repository on GitLab:
- Clone the repository to your home machine:
- I need to submit a copy of my work to a GitLab repository. There are two possible cases:
- This project was not created from a GitLab repository. In this case, you need to create a GitLab repository and connect it with this project. After completing this step, you will move on to the case below, “I have already cloned this project from an existing Gitlab repository” to commit and push your work.
- Easiest way:
- Create a project with whatever name you wish on GitLab.
- Clone your project to a folder on your computer:
- On the GitLab page for your project, select either the address under “SSH” or “HTTPS” (drop down menu near the top of the page), depending on whether you are using HTTPS or SSH. (If in doubt, you can use HTTPS.) Copy the link.
- On your local computer, go to the directory in which you would like to make a folder for your new project. Type
git clone <address you copied>
. This will clone the (empty) GitLab repository into a folder on your computer. - Copy the files from your project into this empty directory. Now you are ready to add your files, commit, and push back to your existing GitLab directory. (See “I have already cloned this project…” below.)
- Slightly more complicated: I have already created a git repository on my local computer, and I want to make a GitLab repository connected to it.
- (cont)
- I have already cloned this project from an existing GitLab repository, and I want to return my changes to that repository.
- First, make sure your current project is up-to-date.
- Run
git status
in your project directory. This will tell you if there are any current changes which are not saved by git. - If anything is not up-to-date in your repository, add any files with changes that you want to make to the staging area with the command
git add <filename>
. If you want to add all changed files, you can usegit add -A
.
Note: It is common to leave some files, like executables and editor save files out of a git repository. If you are trying to leave these out, you may want to set up a git ignore file. - Once everything that you want to save is in the staging area, add it to your local repository with a commit: You can type
git commit
, and git will open an editor for you to write a commit message, which explains what changes you have made. Or you can typegit commit -m “commit message here”
if you want to create a short commit message instead of using an editor.
- Finally, you need to push your local repository to the remote repository. In most cases, you can do this just by typing
git push
in the repository. Now your changes should appear–together with your commit message–on GitLab (or whatever git server you are using).