Git is a version control system for tracking changes in computer files and coordinating work on those files among multiple people.

It is mainly a command line tool, but several GUI frontends exist for the program. This article aims to give a basic tutorial on the command line tool, as well as the git GUI Sourcetree.

Command Line Tutorial

Starting a local repository

To start a local git repository, you can use the git init command.

In general, you want to start your git repository in the project folder that you are working in. For example, if I have a project called test, I want to call the git init command in path/to/test/

cd /path/to/test/
git init

The git init command will create a hidden directory called .git in your project directory. This directory contains the information git uses to track changes in your files. By default, git tracks your files under the master branch.

You can remove your local repository at any time through rm -r .git.

Cloning a remote repository

github.png
GitHub Clone Link
gitlab.png
GitLab Clone Link

You can clone a remote repository through sites like GitHub or GitLab.

The command to do this is git clone. An example of this command is below:

# git clone will place my project folder at /path/to/place/project
cd /path/to/place/
git clone https://github.com/DosenbachGreene/dglabdocker.git

This will create a copy of the remote repository as a local repository. By default, the remote repository will be assigned the origin keyword, which you can use to push/pull changes to/from with your local git repository.

You can find the clone links on the remote repository page (For our lab, it will be GitHub (For Public Repos) and GitLab (For Private Repos).

Staging Changes

Staging means that a file is ready to be committed. You can stage a file with git add:

git add [name of file]

Your file will be staged, if there have been changes made to it since the last commit.

Alternatively, you can stage all your changed files at once with git add --all:

git add --all

This will add all your changed files to the staging area.

You can see your staged files git status:

git status

If you would like to remove files from the staging area, you can use git reset:

git reset [name of file]

Commiting Changes

Commiting a file records the changes made to it in the local repository. You must have the file on staging before commiting.

You make a commit with the git commit command:

git commit

The next step will ask you for a message to go along with your commit. In general, you want to give a summary of the changes you made to the code. See commit messages

By default, git will start the vi text editor for modifying your commit messsage. You can change this behavior by modifying your terminal's VISUAL and EDITOR shell variables:

# This is a bash example
export VISUAL=vim     #(or nano or emacs or whatever you want...)
export EDITOR="$VISUAL"

You can commit in one step by using the -m flag. This will allow you to append your commit message as a string (which is useful for short messages):

git commit -m '[commit message]'

Pushing and Pulling to a Remote Repository

Once you've made a commit to your local repository, you will need to sync it with your remote repository. To do this, we use the git push command.

git push origin master

In general, the remote git repository is defaulted to the origin keyword during git init/clone. The master keyword specifies what branch you want to sync your local repository to.

Pulling changes from a remote repository grabs any new commits from the remote and applies them to your local repository. This is accomplished with the git pull command:

git pull origin master

Branching

TO-DO

Merges

TO-DO

Rebasing

TO-DO

Sourcetree Tutorial

Sourcetree is a free Git client for Windows and Mac. If you understand the basics of Git, it is fairly intuitive to understand and use.

TO-DO