GIT Basics

GIT is a distributed version control system which allows groups of people to work on the same code at the same time, and without stepping on each other’s toes.

Repository or “repo” : The folder containing the files you are tracking – the folder for your project, for example. A directory where GIT has been initialized to start version controlling your files.

.gitignore : A file which lists files and file extensions that GIT should not track in the repository. “Ignore these files.” Placed in the repository folder.

Commits: Keep track of your changes by “committing” changed files to the repository. Commit early, Commit often.

Branch: Create a separate path for development and adding features.

master: The main branch for a repository

The States of a File in a GIT Repo

  • untracked – New file that’s never been added to the repo.
  • working – Tracked file with new changes.
  • staged –  File that has been added to the “staging area”.
  • committed – File is added to repo and is now tracked by GIT.

A Simple Workflow

  1. Create and checkout a new working branch
  2. Work in that branch. Making changes adding features
  3. Once everything is working, merge changes back into master branch
  4. Delete feature branch
  5. Deploy master (push to web server)

GIT Commands:

  • git config –global color.ui true
  • git config –global user.name <your_name>
  • git config –global user.email <your_email>
  • To initialize/create a GIT Repository – git init
  • To add file(s) to the staging area – git add .
  • git commit -m “Initial Commit”
  • To check what the current state of the repository is – git status
  • To check commits – git log
  • To show compact list of commits – git log –oneline
  • Messed Up Working File (not yet staged) – git checkout <path/to/file>
  • Messed Up A File and Staged It – git reset HEAD <path/to/file>
  • git checkout HEAD^ <path/to/file>
  • git reset –hard HEAD^
  • To check what you have changed – git diff / git diff — <file/directory>
  • Create and Checkout New Branch – git checkout –b <working_branch_name>
  • What Branches Do I Have – git branch
  • To Show all Local and Remove Branches – git branch –a
  • To Show Only Remote Branches – git branch –r
  • To Switch to a Branch – git checkout <branch_name>
  • To Merge Changes – git merge <feature_branch_name>
  • Delete Old Branch – git branch -d <feature_branch_name>
  • The git checkout command can be used to update specific files or directories in your working tree with those from another branch, without merging in the whole branch. This has been very helpful for me when working while working with several feature branches.

    git checkout refresh2013
    git checkout hero-module — main.css
    git commit -am “Update main.css from hero-module”The above command updated my refresh2013 branch to include the latest changes made to a file in this case main.css that is on the hero-module branch.