Dec 26, 2011

git tutorial. Use git locally or with Dropbox

I have been working on a project for which I need to restore to different state of the project. I searched and I got an elegant solution.
That is GIT. This is a simple tutorial to use git locally. As my objective was to just restore to different states of my local project I used simple basic git commands. Next section explains that commands with a simple tutorial with a simple scenario which gives a clear picture of what is git and why it is required.
States Diagram: head can be moved back and front to restore different states of the project.

A simple tutorial

  1. create a folder mkdir gittut and cd gittut
  2. git init
  3. touch a b c d 
  4. git add . (don't forget the dot. git add a b c d is also correct)
  5. git commit -am "Initial commit: touched a b c d"  # this is the initial state of the project
  6. Open 'a' with a text editor and add a line or something to it and save it.
  7. Then git commit -am "second commit: added a line to a" 
  8. Then add a line to fine b and c and delete a. Let it be another state of the project. So commit it.
  9. git commit -am "third commit: added a line to b and c and deleted a" 
  10. Like the above make changes to files or add files or delete or what ever you want. And then commit the changes to save the state. git status will give current status.
    1. When you add a file, notify it to git with git add filename
  11. So now to see all the commits. use git log or git reflog
  12. git reflog
    1. 462da04 HEAD@{2}: commit: fifth commit: update first line in b
    2. 99ca2c4 HEAD@{3}: commit: fourth commit: added a line to d
    3. dc9fb29 HEAD@{4}: commit: third commit: added a line to b and c
    4. 998b6a1 HEAD@{5}: commit: second commit: added a line to a
    5. f21192d HEAD@{6}: commit (initial): Initial commit: touched a b c d
  13. To reset to any previous state use git reset --hard dc9fb29
  14. Then check git reflog. It will show something like the following,
    1. dc9fb29 HEAD@{1}: dc9fb29: updating HEAD
    2. 462da04 HEAD@{2}: commit: fifth commit: update first line in b
    3. 99ca2c4 HEAD@{3}: commit: fourth commit: added a line to d
    4. dc9fb29 HEAD@{4}: commit: third commit: added a line to b and c
    5. 998b6a1 HEAD@{5}: commit: second commit: added a line to a
    6. f21192d HEAD@{6}: commit (initial): Initial commit: touched a b c d


So now if you check the files you will see the state which corresponds to dc9fb29. Check the files and verify it. To go to the latest or the last state state use git reset --hard 462da04. The number 462da04 which was our second last state. But reset will delete the newer commits.

So if you go back to a previous state keeping the newer commits as such, use git checkout 998b6a1. Later if you want to move to latest commit just git checkout master.
You can commit the changes that you made in the older state.

To get colored git messages : use git config --global color.ui true

A good tutorial which I found on web : http://www-cs-students.stanford.edu/~blynn//gitmagic/

Use git with dropbox or any other folder as origin.
For this you should have dropbox installed in your system and so the dropbox folder also. The steps are given below.
These steps I got from  http://stackoverflow.com/questions/1960799/using-gitdropbox-together-effectively.  ( I think this page is not opening well. If not opening, get a google cached page ).


  • ~/project $ git init
  • ~/project $ git add .
  • ~/project $ git commit -m "first commit"
  • ~/project $ cd ~/Dropbox/git

  • ~/Dropbox/git $ mkdir project.git
  • ~/Dropbox/git $ cd project.git
  • ~/Dropbox/git $ git init --bare
  • ~/Dropbox/git $ cd ~/project

  • ~/project $ git remote add origin ~/Dropbox/git/project.git
  • ~/project $ git push origin master
and to create a clone on any other folder :  git clone ~/Dropbox/git/project.git
  • to create another branch git checkout -b branch_name
  • to clone a branch git clone -b branch_name git_repository.git
  • list all branches git branch
Git make a better branch as master

git checkout better_branch
git merge --strategy=ours master    # keep the content of this branch, but record a merge
git checkout master
git merge better_branch             # fast-forward master up to the merge`

git change remote : git remote set-url origin git://new.url.here
git log tree view : git log --graph --oneline --all



For Web Developer

  open -a "Google Chrome" --args --disable-web-security