Wednesday, May 30, 2012

Introduction to Version Control and Git

Version control is one of the most useful tools that a programmer can have. It gives a very clear understanding of what has been programming, but more importantly, it gives the confidence to make radical changes. Simply put, version control allows a programmer to save a certain state of code, go back to that state of code, and compare different states of code. Git is the newest version control system (VCS), and it is understood to be better than all other types for various reasons that I will not go into.

So let's jump right into learning git.

Installation

Make sure you have git installed:
$ git --version
git version 1.7.9.5
$ sudo apt-get install git-core

Setup

Change your git config:
$ git config --global user.name "Firstname Lastname"
$ git config --global user.email "your_email@youremail.com"
$ git config --global color.ui true
These were just shorcuts to editing ~/.gitconfig. See this by running:
$ cat ~/.gitconfig
Make sure that git is ignoring files that don't need to be tracked. Edit ~/.gitignore
$ gedit ~/.gitignore
And put in:
*.pyc
This will make git ignore all files that have the file extension .pyc, which are files that speed up the loading of python (google for more info).

Learn Git!

Now start the git immersion tutorial on lab 3 by going here. This will hopefully teach you git. I've got a few ammendments that you should follow:
After Lab 5: Try out:
$ git diff
This will show you what has been added and removed in each file. Out of everything in git, I use this command the most.
Lab 8: This tells you how to interactively edit your commit messages. Don't worry about this. Just do:
$ git commit -m "Using ARGV"
$ git status
Lab 10: Just go over the first and second steps, that is:
$ git log
$ git log --pretty=oneline
The other parts are unnecessary
Lab 11: There are lots of shortcuts here. The only thing you need to do is the hist alias:
$ gedit ~/.gitconfig
Add this to the bottom to the bottom and save:
[alias]
  hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short
Lab 13: Optional
Stop after lab 15 

Revised lab 16 and 17:
Make sure you are on master and have no changes
$ git checkout master
$ git status
There should be no changes in either Now edit hello.rb:
# oops, this was a bad comment
name = ARGV.first || "World"
puts "Hello, #{ARGV.first}!"
Check the difference:
$ git diff
Now commit:
$ git add hello.rb
$ git commit -m "Adding a bad commit"
$ git status
The status should be clean.
Look at the log:
$ git hist
To go back to another commit, do use git reset:
$ git reset <hash>
Where hash is the hash of the previous commit, as seen next to the commit in the hist or log. A hash is just random letters and numbers, like 72ba5ec.
Another way to do this is:
$ git reset HEAD~1
This means "reset to the parent of HEAD, that is one commit before HEAD" Notice that you are now at the point right before adding and committing:
$ git status
$ git diff
$ git hist
Go ahead and undo the changes
$ git checkout hello.rb
Your index and working directory should now have no changes:
$ git status
Congratulations! You should now know the basics of git. Remember, next time you want to initialize a repository, just do:
$ git init
$ git add .
$ git commit -m "Initializing directory"
Later on: pushing/pulling/remote repositories merging branching github

No comments:

Post a Comment