Friday, January 28, 2011

Common command I use in git


To make a directory part of git, go to the directory and run command
git init


To add this directory do 
git add . 


To see list of files that are git
git ls-files


To remove a file from git repository 
git rm filename 


To see all your commit messages in git
git log

To see log messages in one line
git log --pretty=oneline

To limit the number of commit messages to 30 and only see commits till yesterday - 30 days
git log -n 30 --since="1 month ago" --until=yesterday --author="apsingh"
git log -n 20 --pretty=oneline --since="-5 hours"To see which git branch you are working on
git branch --color


To create a branch 
git branch human 

To start working on a branch or checkout a branch 
git checkout human

To create a brach and switch to it 
git checkout -b human


To see diff between 2 branches 
git diff master human

To delete a branch 
git branch -d human


To merge a branch, say human back to master
git co master
git merge human

In case of conflicts during a merge process, you can abort them
git merge --abort 


To revert the merge you just made 
git reset --hard ORIG_HEAD


To rebase a branch changes made in master, checkout the branch you want to rebase 
git rebase master

In case a merge of a branch to master fails then command
git show :1:filename

shows common ancestor; command 
git show :2:filename

shows HEAD version (contents of the file in master); command
git show :3:filename

shows MERGE_HEAD version (contents of the file in the branch)

To checkout directry from remote server to your local directory 
git clone git@remoteServer:/var/git/tmp


Now to synchronize to remote server
git push

To push to remote server "laptop", branch "dog"
git push laptop dog 

To push a branch to origin server
git push origin dog


To update local from remote server
git pull


To create a tracking branch (for easy push and pull)
git branch --track fish origin/fish


To create a tracking branch manually add the the following lines to .git/config
[branch "fish"]
remote = origin
merge = refs/head/fish


To ignore files, in the project base directory, create a file 
vi .gitignore 
and lines like 
*.log
db/schema.rb
db/schema.sql

Git References


References
In addition to the Git objects, which are immutable – that is, they cannot ever be changed, there are references also stored in Git. Unlike the objects, references can constantly change. They are simple pointers to a particular commit, something like a tag, but easily moveable.
Examples of references are branches and remotes. A branch in Git is nothing more than a file in the .git/refs/heads/ directory that contains the sha-1 of the most recent commit of that branch. To branch that line of development, all Git does is create a new file in that directory that points to the same sha-1. As you continue to commit, one of the branches will keep changing to point to the new commit sha1s, while the other one can stay where it was.

Git Objects

Git supports following objects

Blobs
  • The contents of files are stored as blobs.
  • It is important to note that it is the contents that are stored, not the files. The names and modes of the files are not stored with the blob, just the contents.



Tree
  • Directories in Git correspond to trees
  • A tree is a simple list of trees and blobs that the tree contains, along with the names and modes of those trees and blobs. The contents section of a tree object consists of a very simple text file that lists the mode, type, name and sha of each entry.


Commit 
  • We can store arbitrary trees of content in Git, where does the ‘history’ part of ‘tree history storage system’ come in? The answer is the commit object.
  • The commit is very simple, much like the tree. It simply points to a tree and keeps an author, committer, message and any parent commits that directly preceded it.


Tag
  • The final type of object you will find in a Git database is the tag. This is an object that provides a permanent shorthand name for a particular commit. 
  • It contains an object, type, tag, tagger and a message. Normally the type is commit and the object is the sha-1 of the commit you’re tagging. The tag can also be gpg signed, providing cryptographic integrity to a release or version



Git object data is a directed acyclic graph. That is, starting at any commit you can traverse its parents in one direction and there is no chain that begins and ends with the same object. All commit objects point to a tree and optionally to previous commits. All trees point to one or many blobs and/or trees. Given this simple model, we can store and retrieve vast histories of complex trees of arbitrarily changing content quickly and efficiently.