Overview

Git is a free and open-source distributed version control system designed to handle everything from small to extensive projects with speed and efficiency.

Install Git
  • Linux: sudo apt-get install git
  • Mac: brew install git
  • Windows: Download
Additional Resources
The GitHub Flow
  1. Create a branch from the repository
  2. Modify files and, Add commits
  3. Open a pull request
  4. Code Review, Make changes as needed
  5. Merge the pull request
  6. Tidy up the branches. Deploy/ release version

Configuration Git
# global identity configuration
git config --global user.name "Your Name"
git config --global user.email "your@email.com"

# add Color to Git Output
git config --global color.ui true

Create a New Code Repository
# initialize a new git repository
git init my-repo
cd my-repo

# add sample files
touch file1.js file2.js

# check status
git status

Clone an Existing Code Repository
# clone the remote repository
git clone 'url.repo.git'
cd repo

# check status
git status

Stage & Commit Project Files
# add individual files
git add file1.js file2.js

# or add all files from the directory
git add .

# committing changes
git commit -m "Commit message"

Checkout Project Version/ Commit
# view project history
git log --stat --summary

# checkout version
git checkout <commit_hash>

Setup Branches
# lists available branches
git branch

# lists all branches including the remote branches
git branch -a

# lists branches in the remote repositories
git branch -r

# create a new branch 
git branch <new_branch_name>

# checkout branch
git checkout <branch_name>

# rename branch
git branch -m [old_name] [new_name]

# delete branch testing
git branch -d testing

# force delete testing
git branch -D testing

# check if the branch has been deleted
git branch

Check Differences Between Branches
# shows the differences between the current head of master and your_branch
git diff master your_branch

# shows the differences in your branch based on the common ancestor for both
git diff master...your_branch

Merge Branches
# note: normally merges are done via pull requests
git merge <branch_name>

Push Changes to the Remote Server
# show the existing defined remotes
git remote

# show details about the remotes
git remote -v

# set remote origin
git remote add origin https://github.com/USERNAME/YOUR-REPOSITORY.git

# push changes to the remote server
git push --set-upstream origin master

Contribute to the Repository
# get the latest version
git pull

# checkout work branch
git checkout <work_branch>

# make necessary changes
git add .
git commit -m 'Revision message'
git push