Git Cheat Sheet

Quick Git Reference

Git Cheat Sheet

by John Vincent


Posted on January 22, 2017



Put in one place those pesky Git options.

This stuff ends up sprayed everywhere, so let’s create a reference document.

Git Options

Other guides:

Git commands

Create local repository

git init
git add .
git commit -m "Message"

git status
git branch
git remote add origin git@github.com:{my-github}/{new-repo}.git
git push -u origin master
git remote -v
git log

Usual edit cycle

git add .
git commit -m "Message"
git push

List branches

git branch

Show differences

git diff

Show git log

git log

Clone repository

git clone url

Add remote repository reference

git remote add origin git@github.com:{my-github}/{NEW_REPO}.git

Push files to remote repository

git push -u origin master

Verify remote repository

git remote -v

change to new remote:

git remote set-url origin https://github.com/{github-user}/{github-project}

Clone Branch

Clone master

git clone git@github.com:{github-user}/{github-project}

Clone branch

git clone -b feature_1 git@github.com:{github-user}/{github-project}

Create Feature Branch

Choose a feature branch name that is descriptive, for example:

feature/saved-user

Create a feature branch

cd {git-project-directory}
git checkout -b feature_1

Push to remote

git push origin feature_1

Merge Feature Branch

Ensure feature branch is committed.

cd {git-project-directory}
git status

Switch to master branch

git checkout master

Merge feature branch into master

git merge feature_1

Push to remote master

git push origin master

Delete the feature branch

git branch -d feature_1

Heroku

For notes regarding git and Heroku, please see Heroku Notes

.gitignore

node_modules

Create a Repository

The following is my process.

  • Create public repository at Github or,
  • Create private repository at Bitbucket
  • cd {my-github}/{subdir} or,
  • cd {my-github}/{subdir}
  • create-repo remote-repository-name

Change {my-github} to your own Github account.

This will quickly provide a development environment.

create-repo

#!/bin/sh
#
# script to create a git repository in github
#
echo "Script to create a git repository in github"
echo " "
echo "ENSURE YOU HAVE ALREADY CREATED the REPOSITORY in Github"
echo ""
NEW_REPO=$1
if [ -z "$NEW_REPO" ]; then
    echo "Usage: create-repo repository-name"
    exit 1
fi
#
echo "Creating local repository $NEW_REPO"
#
echo " "
echo "Checking if directory already exists"
if [ ! -d "$NEW_REPO" ]; then
    echo " "
	 echo "Making directory $NEW_REPO"
	 mkdir $NEW_REPO
    touch $NEW_REPO/index.html
#
    mkdir $NEW_REPO/css
    touch $NEW_REPO/css/main.css
#
    mkdir $NEW_REPO/js
    touch $NEW_REPO/js/app.js
fi
#
echo " "
echo "cd to $NEW_REPO"
cd $NEW_REPO
#
if [ ! -d ".git" ]; then
    echo " "
	 echo "Create basic files"
	 touch README.md
	 touch .gitignore
    echo "node_modules" > .gitignore
#
    echo " "
	echo "Create local repository"
	git init
#
    echo " "
	echo "Adding files to repository and commit"
	git add *
	git add .gitignore
	git commit -m "Commit"
fi
#
echo " "
echo "Check status and branch info"
git status
git branch
#
echo " "
echo "Add remote repository reference"
git remote add origin git@github.com:{my-github}/$NEW_REPO.git
#
echo " "
echo "Push files to remote repository"
git push -u origin master
#
echo " "
echo "Verify remote repository"
git remote -v
#
echo " "
echo "Show git log"
git log
#
echo "Completed"

Change {my-github} to your own Github account.