Setting up a repository

This tutorial provides an overview of how to set up a repository (repo) under Git version control. This resource will walk you through initializing a Git repository for a new or existing project. Included below are workflow examples of repositories both created locally and cloned from remote repositories. This guide assumes a basic familiarity with a command-line interface.

The high level points this guide will cover are:

  • Initializing a new Git repo
  • Cloning an existing Git repo
  • Committing a modified version of a file to the repo
  • Configuring a Git repo for remote collaboration
  • Common Git version control commands

Initializing a new repository: git init

To create a new repo, you'll use the git init command. git init is a one-time command you use during the initial setup of a new repo. Executing this command will create a new .git sub directory in your current working directory. This will also create a new master branch.

Versioning an existing project with a new git repository

This example assumes you already have an existing project folder that you would like to create a repo within. You'll first cd to the root project folder and then execute the git init command.

cd projectfolder/
git init

Cloning an existing repository: git clone

git clone is used to create a copy or clone of remote repositories. You pass git clone a repository URL. Git supports a few different network protocols and corresponding URL formats.

  1. SSH
  2. HTTPS
git clone git@<git repourl>.git

Saving changes to the repository: git add and git commit

Now that you have a repository cloned or initialized, you can commit file version changes to it. The following example assumes you have set up a project at /path/to/project. The steps being taken in this example are:

  • Change directories to /path/to/project
  • Create a new file sample.txt with contents ~"test content for git "~
  • git add sample.txt to the repository staging area
  • Create a new commit with a message describing what work was done in the commit
cd /path/to/project 
echo "test content for git tutorial" >> sample.txt 
git add sample.txt 
git commit -m "added sample.txt to the repo"

After executing this example, your repo will now have sample.txt added to the history and will track future updates to the file.

Repo-to-repo collaboration: git push

Git push will push your changes in staging to remote repository

git push -u origin <branchname>

Branching & Merging

Command Description
git branch List branches (the asterisk denotes the current branch)
git branch -a List all branches (local and remote)
git branch [branch name] Create a new branch
git branch -d [branch name] Delete a branch
git push origin --delete [branch name] Delete a remote branch
git checkout -b [branch name] Create a new branch and switch to it
git checkout -b [branch name] origin/[branch name] Clone a remote branch and switch to it
git checkout [branch name] Switch to a branch
git checkout - Switch to the branch last checked out
git checkout -- [file-name.txt] Discard changes to a file
git merge [branch name] Merge a branch into the active branch
git merge [source branch] [target branch] Merge a branch into a target branch
git stash Stash changes in a dirty working directory
git stash clear Remove all stashed entries

Sharing & Updating Projects

Command Description
git push origin [branch name] Push a branch to your remote repository
git push -u origin [branch name] Push changes to remote repository (and remember the branch)
git push Push changes to remote repository (remembered branch)
git push origin --delete [branch name] Delete a remote branch
git pull Update local repository to the newest commit
git pull origin [branch name] Pull changes from remote repository
git remote add origin[username]/[repository-name].git Add a remote repository
git remote set-url origin[username]/[repository-name].git Set a repository's origin branch to SSH

Inspection & Comparison

Command Description
git log View changes
git log --summary View changes (detailed)
git diff [source branch] [target branch] Preview changes before merging