« Back to home

Git 101: Getting to know Git as a source control

Git is an open source version control system that has become popular among the open source community. Large companies such as Google, Facebook and Microsoft has adopted it (well anything released as an open source are most likely hosted at Github).
I have experience in both Subversion and TFS version control but Git is slightly different on how things are accomplished.
Since the cool kids tends to use the command line interpreter (Bash, Command Prompt, Terminal, etc) these days, it's hard not to be intrigued
by this movement. There's something about the black background and white font that fascinates me as well.

After playing around with Git for a few days, I've put together a list that breaks down the basic source control
operations as well as the underlying commands. This is more like a quick reference that I can always go back to.

This requires Git to be installed locally (as a global). Installing the Github Windows or Mac version will pretty much
install everything that you need including a GUI.
The Git team also created a handy Git reference for guidance when starting out or finding out how a command works. There's also
a nice Code School tutorial to try out Git commands and quickly get up to speed with understanding the concepts.

The danger behind Git is executing a command without knowing the consequences of the action if you're working with production code. I'm so
used to the Team Foundation Server UI that it can be hard not having a visual of the changes. At the same time, the thought of using
a command line brings back the old programmer side of me (although not that old either) ;-).

The intention of the next section is to summarize the more important aspects of Git and describe the commands in simple and grouped manner.
The notes for each command is my interpretation while trying to learn Git as a source control.

Initialization

Go outside the current directory and initialize

git init [project folder name]

Staging

Add. You need to browse to the working directory and perform add for new/modified file(s) for staging.

git add . // Adds all files and subdirectories on current directory to staging.
git add * // Alternatively adds all files but not subdirectories.
git add '*.js' // To add all js files including in the subdirectories.
git add filea.js index.html // Or stage individual files.

Reset or "unstage". This basically reverts the current state of the staging to before files were modified.

git reset HEAD --[file] // Undo last commit and unstage the file.
git reset --soft // Undo last commit.
git reset --hard // Undo last commit, unstage files and undo all changes in the working directory.

Remove.

git rm [file] // Removes from staging and delete file.
git rm [file] --cached // Removes from staging but keeps the file.

To undo a file and bring back the version from last commit.

git checkout --[file]

Getting Status and Log

To view GIT status. Any modified file within the current directory will show in status as well as whether the file(s) has been added to
staging or not.

git status -s // short output
git log // chronological order of commits to local repository

Commits

Commits are only committed to your local repository. To commit your staging snapshot.

git commit -m"Commit changes"

Pushing to Remote Repositories

git remote add origin git@github.com:[username]/[git_repo_name].git

The command below pushes the files to 'origin' (GitHub) and the default local branch is 'master'.
The 'u' is used to remember the push parameter. Executing the command will setup to track any branch made remotely.

git push -u origin master

Any changes made on GitHub will be pulled down locally.

git pull origin master

Stash

Saves the current state for later and brings back last commit.

git stash
git stash list // Get all stashes
git stash apply // Applies latest stash OR add [stash_reference] 
git stash drop [stash_reference]  // Removes a stash 

Branching

git branch [branch_name]
git branch // To view current branches.
git checkout [branch_name] // To switch branch.

Notes

  • Commas and apostrophes works similarly.

Comments

comments powered by Disqus