Wednesday, December 14, 2011

How to restore locally deleted file in git

If you have not committed yet then find the last commit 
git rev-list -n 1 HEAD -- <file_path> 

Then checkout the version at the commit before.
git checkout <deleting_commit>^ -- <file_path>

How to see list of tags for a git repo

To see list of all tags for a git repo, run command
git tag

To also see the commit message along with the tag, run command
git tag -n

How to push tags in your local git repo to remote

Following command pushes all tags from local git repo to remote
git push --tags


Monday, November 28, 2011

List of all git commands

Following is the list of all git commands available in the latest version of git (As of Nov 2011)
git-add

git-annotate

git-apply

git-archive

git-bisect--helper

git-blame

git-branch

git-bundle

git-cat-file

git-check-attr

git-checkout

git-checkout-index

git-check-ref-format

git-cherry

git-cherry-pick

git-clean

git-clone

git-commit

git-commit-tree

git-config

git-count-objects

git-describe

git-diff

git-diff-files

git-diff-index

git-diff-tree

git-fast-export

git-fetch

git-fetch-pack

git-fmt-merge-msg

git-for-each-ref

git-format-patch

git-fsck

git-fsck-objects

git-gc

git-get-tar-commit-id

git-grep

git-hash-object

git-help

git-index-pack

git-init

git-init-db

git-log

git-ls-files

git-ls-remote

git-ls-tree

git-mailinfo

git-mailsplit

git-merge

git-merge-base

git-merge-file

git-merge-index

git-merge-ours

git-merge-recursive

git-merge-subtree

git-merge-tree

git-mktag

git-mktree

git-mv

git-name-rev

git-notes

git-pack-objects

git-pack-redundant

git-pack-refs

git-patch-id

git-peek-remote

git-prune

git-prune-packed

git-push

git-read-tree

git-receive-pack

git-reflog

git-remote

git-remote-ext

git-remote-fd

git-replace

git-repo-config

git-rerere

git-reset

git-revert

git-rev-list

git-rev-parse

git-rm

git-send-pack

git-shortlog

git-show

git-show-branch

git-show-ref

git-stage

git-status

git-stripspace

git-symbolic-ref

git-tag

git-tar-tree

git-unpack-file

git-unpack-objects

git-update-index

git-update-ref

git-update-server-info

git-upload-archive

git-var

git-verify-pack

git-verify-tag

git-whatchanged;

git-write-tree

How to install git on Linux (CentOs)

Following are the list of command you can execute on CentoOs 5 to install git

yum -y install zlib-devel openssl-devel cpio expat-devel gettext-devel;
cd /usr/local/src;
wget
http://git-core.googlecode.com/files/git-1.7.7.4.tar.gz
tar xvzf git-1.7.7.4.tar.gz;
cd git-1.7.7.4;
autoconf;
./configure --with-curl=/usr/local$;
make;
make install;

ln -s /usr/local/src/git-1.7.7.4/git /usr/bin/git

Monday, November 21, 2011

How to export projects out of git archive

You can do this using git archive command

Following code will extract the current checked out branch, create tar and zip it
git archive --format=tar --prefix=<any dir name you want>/   HEAD | gzip >latest.tar.gz

Following code will extract master branch, create tar and zip it
git archive --format=tar --prefix=<any dir name you want>/   master | gzip >latest.tar.gz

Wednesday, November 16, 2011

Converting a Subversion repository to Git

This is an excerpt from page https://37s.backpackit.com/pub/1465067

Converting a Subversion repository to Git

Assuming we’re converting ExampleApp to git:
  1. mkdir /path/to/exampleapp-git.tmp
  2. cd /path/to/exampleapp-git.tmp
  3. git-svn init file:///u/repos/exampleapp --no-metadata --trunk=trunk --branches=branches --tags=tags
  4. git config svn.authorsFile /path/to/authors.txt
  5. git-svn fetch
The authors.txt file mentioned above is the file attached, below. Note that for large repositories, the ‘fetch’ command here can take quite some time (Basecamp took almost an hour and a half).
Once the fetch finishes, we just need to do some cleanup. First, let’s kill all of the remote tracking branches that we no longer need.
  1. git branch -r
  2. git branch -r -d stale-branch stale-branch …
Where “stale-branch” is the name of a remote-tracking branch that we no longer need.
Next, we can run prune to get rid of any orphaned objects, and gc to clean things up:
  1. git prune
  2. git gc
Then, we can repack everything optimally:
  1. git repack -f -a -d --depth=500 --window=500
Finally, we check out any remote tracking branches, so that when we later push to the final repository location, the branches get remembered:
  1. git checkout -b branch-name branch-name
The branch-name is specified twice because the first one is the name you want to give the local branch, and the second is the name of the remote branch you want to track. Do the above for each branch (“branch-name”) that you want to keep.
Note that if the branch that was checked out upon completion of the fetch was not the master (trunk), then you’ll need to use “git branch -m” to rename the current branch to what was actually checked out, and then checkout trunk as master (git checkout -b master trunk).
Once that is done, we can create our new “bare” repository, where we will all push to and pull from:
  1. cd /u/git
  2. sudo -u app mkdir -m 0770 exampleapp.git
  3. cd exampleapp.git
  4. sudo -u app git --bare init --shared=group
Then, we move back to our converted repository, and push it all to our bare repository:
  1. cd /path/to/exampleapp-git.tmp
  2. git push --all /u/git/exampleapp.git
The “--all” flag tells git that we want to push all branches, not just the current branch. Once that is done, you should be able to clone the new repository from your local machine:
  1. git clone ssh://{repo-server}/u/git/exampleapp.git
Done!

Tuesday, November 15, 2011

How to identify, using git cat-file, type of an object in Git by looking at it SHA1 value

All objects in Git have a sha1 value. Git provides a way to inspect a sha1 and tell you the type of object represented by that sha1
git cat-file -t <sha1>

E.g
git cat-file -t 6a00e12055941d688466e68d6880d2bc06f12b55

This command also supports batch mode that will read a list of objects from STDIN. Just run command
git cat-file
to see all options supported by it

How to see contents of objects in git

Following command shows the content of and object in git
git show <sha1 of object>

The sha1 of the object can be found in directory .git/object/[a-z0-9][a-z0-9]/[a-z0-9]{38}. Note that while create subdirectories in objects folder git takes the first 2 letters to create a sub-directory and remaing 38 to name the file that contains the content. In the command above you will have to give full 40 characters 

How to see decompress git objects (in text)

Git maintains all its objects in in .git/objects directory. These files are compressed. To read the text contents of the file, create a file read_git_obj.pl and add folloing code to it

use strict ;
use warnings ;
use Compress::Zlib ;

my $x = inflateInit() or die "Cannot create a inflation stream\n" ;
my $input = '' ;
binmode STDIN;
binmode STDOUT;

my ($output, $status) ;
while (read(STDIN, $input, 4096)){
        ($output, $status) = $x->inflate(\$input) ;
        print $output
        if $status == Z_OK or $status == Z_STREAM_END ;
        last if $status != Z_OK ;
}
die "inflation failed\n" unless $status == Z_STREAM_END ;


This program reads data from STDIN. It can be used as
cat .git/objects/fd/3feaffcc13c86991c5cbe920f006b4e9b5922f | perl read_git_obj.pl

Tuesday, October 25, 2011

How to undo a "git add files"

You can use the following command to undo a "git add files"
git reset -- files

Thursday, February 10, 2011

How to push from your local master to remote master

Suppose you have added some files to your local master and want to push it to remote master then use the following git command
git push origin master

Wednesday, February 9, 2011

How to create, work and delete a remote branch in git

- Create a new remote branch "abc"
git push origin origin:refs/heads/abc

- Checkout remote branch in your local directory to work on it
git checkout --track -b abc origin/abc

- To make any changes to branch abc and push to remote branch "abc"
git push

- To get latest changes from the remote branch "abc"
git pull

- Delete local  and remote branch "abc"
git checkout master
git branch -r -d origin/abc
git branch -D abc

- Delete remote branch "abc" from github
git push origin :abc

Wednesday, February 2, 2011

How to see all remote and local branches in git

If you have a centralized remote repository cloned in your local drive then to see all branches available in the remote server and your local drive, run command
git branch -a

Tuesday, February 1, 2011

How to get the master branch from central git server

To get a branch from central git repo (assuming you already cloned the central repo)
  1. Logon to the git repo on your local machine
  2. git branch TESTBRANCH
  3. git checkout TESTBRANCH
  4. Check that you have swtiched to that branch by running command git branch. * should be on the new branch

How to get the master branch to your local machine from central git server

To get local copy of git repo from centralized got server, run the following command
git clone git@<your got server>:some/path/project.git

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.