<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>xk0der &#187; version control</title>
	<atom:link href="http://blog.xkoder.com/tag/version-control/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.xkoder.com</link>
	<description>therez light!</description>
	<lastBuildDate>Sat, 12 Dec 2009 13:58:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Git Tutorial Part II &#8211; Sharpen your Git-Fu with 10 more commands.</title>
		<link>http://blog.xkoder.com/2009/06/06/git-tutorial-part-ii-sharpen-you-git-fu-with-10-more-commands/</link>
		<comments>http://blog.xkoder.com/2009/06/06/git-tutorial-part-ii-sharpen-you-git-fu-with-10-more-commands/#comments</comments>
		<pubDate>Sat, 06 Jun 2009 09:31:47 +0000</pubDate>
		<dc:creator>xk0der</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[fu]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[version control]]></category>

		<guid isPermaLink="false">http://blog.xkoder.com/?p=93</guid>
		<description><![CDATA[Some time back I had posted a simple Git tutorial that introduced you to 10 git commands, to get you started with this wonderful piece of tool. The response that I got for that post was great. I also got comments asking me for how to perform few other tasks. So here it is, the [...]]]></description>
			<content:encoded><![CDATA[<p>Some time back I had posted a simple <a title="Git Tutorial : Starting with git using just 10 commands " href="http://blog.xkoder.com/2008/08/13/git-tutorial-starting-with-git-using-just-10-commands/">Git tutorial that introduced you to 10 git commands</a>, to get you started with this wonderful piece of tool. The response that I got for that post was great. I also got comments asking me for how to perform few other tasks. So here it is, the second installment of the git-tutorial. I hope you enjoy it as much as the first part.</p>
<p><strong>Remember</strong>, the text presented here targets a single developer setup (as the first part did). I intend to do a distributed git setup in the third installment.</p>
<p><span style="color: #999999;"><em>Note : Few commands have been broken into two or more lines to fit into the blog&#8217;s layout.<br />
</em></span></p>
<p>Let&#8217;s begin!</p>
<h3>Command 1 : Time Travel: Moving back and forth between revisions.</h3>
<p>The whole point of a version control system is to maintain revision history of your source code. And permitting you to view, how your code looked, at any point of time in the recorded history.</p>
<p>Git records your revisions in the form of commits. Every commit is assigned a unique hash to identify it. (Git uses SHA1 hashes). You can view the commit hash by issuing the <em>git-log</em> command (Check <a title="Git Tutorial : Starting with git using just 10 commands " href="http://blog.xkoder.com/2008/08/13/git-tutorial-starting-with-git-using-just-10-commands/">part one</a>). The hash is displayed as a hexadecimal string on the line beginning with the word &#8220;commit&#8221; in the log.</p>
<p>Now that we know all this technical mumbo-jumbo, let&#8217;s use it to do some real work.</p>
<p><strong>Move to a commit in the past.</strong></p>
<p>To move to a particular commit back in time we use the command git-checkout, introduced in <a title="Git Tutorial : Starting with git using just 10 commands " href="http://blog.xkoder.com/2008/08/13/git-tutorial-starting-with-git-using-just-10-commands/">part-I</a> for moving between branches. The syntax is similar, but instead of the branch name we use the commit hash. <strong>Always make sure you commit any changes or stash them (see next command) before you issue the checkout command; Else the changes will move across!</strong></p>
<p>here&#8217;s the syntax:</p>
<pre>$ git checkout <em>commit_hash</em></pre>
<p>Let&#8217;s see an example.</p>
<p>First I issue the git-log command to find the hash of a commit I want to switch to.</p>
<pre>$ git log
<strong><span style="color: #ffcc00;">commit 1ef801f70a99b07bb578bac4a3c2edb52b367e1d</span></strong>
Author: xk0der &lt;<span style="color: #0000ff;">amit .. AT .. xkoder .. com</span>&gt;
Date:   Fri Jun 5 12:52:16 2009 +0530

Added few comments

<strong><span style="color: #ffcc00;">commit ccbaeec43300f19dd04308b6c62a3f03f6233725</span></strong>
Author: xk0der &lt;<span style="color: #0000ff;">amit .. AT .. xkoder .. com</span>&gt;
Date:   Fri Jun 5 12:51:30 2009 +0530

Initial import</pre>
<p><em><span style="color: #c0c0c0;">(email address obfuscated for spam bots)</span></em></p>
<p>As you can see, I have just two commits here. I want to move to the commit with the log message &#8220;Initial import&#8221; i.e. my first commit. So I issue the following command.</p>
<pre>$ git checkout <strong>ccbaeec4</strong></pre>
<p>as you can see I just typed in the first 8 characters of the hash. That is the minimum that Git requires to successfully identify the commits uniquely. You may supply more than eight characters or the full hash if you wish.</p>
<p>After the checkout is complete, you can view your files to see how they looked in the past <img src='http://blog.xkoder.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . If you want to modify your files and want to propagate the changes to the master&#8217;s HEAD, check <strong>command 6 </strong>below.</p>
<p><strong>Moving back to the present</strong><br />
To go back to the master&#8217;s HEAD; which is the present state of your code; issue the command:</p>
<pre>$ git checkout <strong>master</strong></pre>
<p>As I&#8217;ve said earlier, <strong>always remember</strong> to commit changes or stash them before moving between branches or commits.</p>
<h3>Command 2: git-stash &#8211; Moving without committing.</h3>
<p>This is one handy command you&#8217;ll find very useful. It lets you store your current changes to a temporary location. These <em>stashed</em> changes can then be recalled back when required. Thus alleviating the need to commit every time you need to move across branches or commits.</p>
<p><strong>Stashing changes</strong><br />
the syntax is:</p>
<p>$ git stash</p>
<p>When issued, all changes will be stashed to a temporary location and you will have a clean current HEAD. You can verify this by issuing the <em>git-status</em> command.</p>
<p>When done with your other work, you can pop back the stashed changes by issuing the following command.</p>
<p>$ git stash pop</p>
<p>Pretty cool &#8216;eh! But when to use it?</p>
<p><strong>When to use git-stash; Here is a one typical scenario</strong><br />
Suppose you are working on one of the <em>feature</em> branch and you get a mail that something is not fine with the code on your trunk, the &#8216;<em>master branch</em>&#8216;. Now one way to to fix something on the master branch is to:</p>
<ul>
<li> First commit changes on the &#8216;feature&#8217; branch.</li>
<li> Next, checkout the master branch and fix what&#8217;s required.</li>
<li> Commit changes to master.</li>
<li> Checkout the &#8216;feature&#8217; branch.</li>
</ul>
<p>But, what if, you do not want to commit the changes on the &#8216;feature&#8217; branch? Probably because you are halfway through something, or you want to test your code before you commit it. You you stash all changes as shown below.</p>
<pre>$ git stash
$ git checkout master
<span style="color: #999999;"><em>...Fix some stuff...</em></span>
$ git commit -a -m "Bug:12 fixed - thread no longer dies prematurely"
$ git checkout feature
$ git stash pop
<span style="color: #999999;"><em>...Continue working normally...</em></span></pre>
<p><strong>Multiple stashes</strong><br />
You can stash more than one set of changes, if required, by issuing the git-stash command multiple times.</p>
<p>When you use the `git-stash pop` command, the latest stash comes out first, much like a stack.</p>
<p><strong>Viewing stored stashes</strong></p>
<p>To view a list of stashed changes issue the following command</p>
<p>$ git stash list</p>
<p>There are more fancy things, that you can do with this command (checkout the man pages for git-stash). But for normal daily use the first two commands will suffice.</p>
<h3>Command 3: git-diff &#8211; some switches to differentiate better.</h3>
<p>I&#8217;ve already explained this command in the <a title="Git Tutorial : Starting with git using just 10 commands " href="http://blog.xkoder.com/2008/08/13/git-tutorial-starting-with-git-using-just-10-commands/">first part</a>. Here I&#8217;ll discuss some command line options that you can supply, to help find the right information.</p>
<p><strong>Finding which files differ (instead of what differs inside files)<br />
</strong></p>
<p>Here&#8217;s the command:</p>
<pre>$ git diff --name-status</pre>
<p>This will list the files that have been modified(M), added(A) or deleted(D) with respect to the current HEAD and uncommitted changes, if any. You may supply commit hash(es) to find difference between those two commits.</p>
<pre>$ git diff --name-status <em>hash_1 hash_2</em></pre>
<p><strong>Using the HEAD symbol</strong></p>
<p>You can use the <em>HEAD </em>symbol to view diff&#8217;s between the current <em>HEAD </em>and previous revisions, as follows:</p>
<pre>$ git diff <strong>HEAD~1</strong></pre>
<p>This command will show you the diff between current <em>HEAD </em>and the immediate previous commit. This command is a shortcut for</p>
<pre>$ git diff commit_hash_for_HEAD
  commit_hash_just_before_HEAD</pre>
<p><strong>What is the HEAD?</strong></p>
<p>If you remember, as discussed in part one of this tutorial, <em>HEAD </em>refers to the latest commit on the current branch. <span style="color: #999999;"><em></em></span></p>
<p>You can change the number after the tilde (~) to find differences between HEAD and &#8216;that&#8217; many commits before the HEAD commit. experiment and see what you find. This is a <em>totally safe</em> command to play with <img src='http://blog.xkoder.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Command 4 : git-add revisited.</h3>
<p>In part one of this tutorial we learned how to make git keep track of a file or number of files by issuing the <em>git-add</em> command. Well that is one work <em>git-add </em>does. Let&#8217;s find out what&#8217;s the other thing <em>git-add</em> is used for.</p>
<p>Before that, let&#8217;s see how we have been committing changes up till now.</p>
<pre>$ git commit -a -m "Commit Message"</pre>
<p>We know that the <strong>-m</strong> switch is to specify the commit message, what is that <strong>-a</strong> switch doing there?</p>
<p>Before I explain; The above command is (<em>almost</em>) equivalent to the following two commands, in sequence:</p>
<pre>$ git add .
$ git commit -m "Commit Message"</pre>
<p>So you see, that little <strong>-a</strong> switch is actually a shortcut to add all files. But haven&#8217;t we already added the files for git, to keep track of?</p>
<p>Yes we have but git also wants you to tell it <em>Which files to commit</em>; and this is what the first &#8216;<strong>git add .</strong>&#8216; command is doing. It is telling git to add all new, modified or deleted files to the &#8216;<em>commit stage</em>&#8216;. So after the files have be staged for committing, we actually commit i.e. register our changes, by issuing the second command (<em>git-commit</em>).</p>
<p><strong>One small but <span style="color: #ff0000;">important </span>quirk.</strong><br />
As I said the command is <em>almost</em> equivalent to the latter sequence; so what&#8217;s the catch?</p>
<p>The switch<strong> -a</strong> with <em>git-commit</em> <em>DOES NOT ADD</em> files that are <em>not</em> currently being tracked by git.<br />
<em>WHEREAS</em>; &#8216;<strong>git add .</strong>&#8216; adds files not being tracked by git for commit as well.</p>
<p>Why to use git-add this way? Read ahead.</p>
<p><strong>Committing only selected files</strong></p>
<p>Suppose you are working on, say 3 source files. You realize that you are pretty much content with the changes you have made to two of the files but the third file is still work in progress. You can commit just these two files by issuing the following commands:</p>
<pre>$ git add path/to/file1
$ git add path/to/file2
$ git commit -m "Some feature done"</pre>
<p>When you are done with the third file; You can commit changes by either issuing the following commands</p>
<pre>$ git add path/to/file3
$ git commit -m "Third feature done"</pre>
<p>OR, by issuing the following command (Since we want to commit all modified files.)</p>
<pre>$ git commit -a -m "Third feature done"</pre>
<p>You can use &#8216;*&#8217; and &#8216;?&#8217; wild-cards to stage multiple files for commit with the <em>git-add</em> command.</p>
<p>example:</p>
<pre>$ git add header/*</pre>
<p>This command will add all files inside the folder &#8220;header&#8221; to the &#8216;<em>commit stage&#8217;</em>.</p>
<p><strong><span style="color: #ff0000;">Remember</span>:</strong> <em>git-add</em> just <em>stages</em> the changes for commit (i.e tells git which files to commit), the actual commit i.e. write to the repository happens when you issue the git-commit command.</p>
<p>Also checkout the <em>git-reset</em> command listed below; to learn, how to remove files from &#8216;commit stage&#8217;.</p>
<h3>Command 5 : git-rm ; Few more things you should know.</h3>
<p><strong>The &#8211;cached switch:</strong></p>
<p>When git-rm is used with this switch the file are removed from git&#8217;s repository but are no deleted from the disk.</p>
<p>Example:</p>
<pre>$ git rm --cached
  path/to/file/or/folder</pre>
<p>The above command will remove the particular file or folder but they will continue to exist on the disk. If you issue <em>git-status</em> command the file will be shown as deleted, as well as un-tracked.</p>
<p><strong>git-rm without the &#8211;cached switch:</strong></p>
<p>When used without the <em>- -cached </em>switch <em>git-rm </em>removes the file from the repository as well as from the disk.</p>
<p><span style="text-decoration: underline;"><em>You need to commit changes</em></span> after using the <em>git-rm</em> command with or without the <em>- -cached</em> switch.</p>
<p><strong>All isn&#8217;t lost!</strong></p>
<p>Remember that the file(s) that you removed with git-rm and then commit changes, <em>only apply to that commit</em>. All your previous revisions will still have the removed file(s) or removed folder(s). So if you checkout previous revisions you can get the files or folder back.</p>
<p>You cannot alter the commit history(<span style="color: #999999;">well! see <em>git-reset</em> below</span>), as that goes against the version control system&#8217;s primary functionality; which is to preserve history of changes to the source code repository.</p>
<h3>Command 6 : Managing branches.</h3>
<p>We already know how to create, delete and move between branches. Here are a few more commands to help you manage your branches.</p>
<p><strong>Creating a branch out of some older commit.</strong></p>
<p>Suppose you want to move back to a particular commit and create a branch out of it. You can issue the following commands</p>
<pre>$ git checkout <em>commit_hash</em>
$ git branch <em>branch_name</em>
$ git checkout <em>branch_name</em></pre>
<p>A convenient shortcut for the above command-set is:</p>
<pre>$ git checkout
  -b <em>branch_name</em> <em>commit_hash</em></pre>
<p>The above command is exactly equivalent to the three commands listed before.</p>
<p>An example with the shortcut command:</p>
<pre>$ git checkout
  -b <strong>bugfix_branch</strong> <strong>HEAD~2</strong></pre>
<p>The above command will create a branch named <em>bugfix_branch</em> which will be positioned at two commits bellow the current <em>HEAD</em>.</p>
<p>You can use the <strong>gitk </strong>gui to visually checkout the branches. Try it, it&#8217;s really cool! (Checkout the <a title="Git Tutorial : Starting with git using just 10 commands " href="http://blog.xkoder.com/2008/08/13/git-tutorial-starting-with-git-using-just-10-commands/">part one</a> of this tutorial to know more about gitk).</p>
<h3>Command 7 : git reset command</h3>
<p><strong>Clearing changes</strong><br />
You made some changes to your source code and realize you don&#8217;t need those changes. Maybe you were just testing something. What to do now?</p>
<p>Just use the following command to clear any changes and move back to the latest clean state, i.e. the last commit.</p>
<p>$ git reset &#8211;hard</p>
<p><strong>BE VERY CAUTIOUS</strong> while issuing this command as it is <strong>IRREVERSIBLE</strong>.</p>
<p>This command will clear all un-committed changes and revert back all your files to the state reflected by your last commit.</p>
<p>The above command can be quiet useful if you do a merge between branches or from other people repository and get some conflicts; and you want to restore your repository back to the clean state. Just issue this command you&#8217;ll be back to the un-merged state.</p>
<p><strong>Clearing files staged for commit</strong><br />
After adding files to the &#8216;commit stage&#8217; you realize you do not want to commit those files. You can issue the following command to un-stage them.</p>
<p>$ git reset</p>
<p>This command un-stages files staged for commits ONLY, so all your changes are still there.</p>
<p><strong>Deleting commits!</strong></p>
<p>This command is provided for those rare cases where you REALLY REALLY DO need to &#8216;alter&#8217; the history. Or for those occasional cases where you did some bad commits.</p>
<p>Issue the following command to delete a commit forever and ever.</p>
<p>$ git reset &#8211;hard HEAD~2</p>
<p>This command gets rid of the last two commits; kind of rewinds back to the third last commit.</p>
<p><em>If you have shared your repository, you are strongly discouraged to use this command.</em> But as a single developer you can use it if really required.</p>
<h3>Command 8: Garbage collection; Compress your repo and get some free space.</h3>
<p>I&#8217;ve saved the simplest command for the end! Smile now <img src='http://blog.xkoder.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Issue the following command to make git compress your repository and clean it up.</p>
<pre>$ git gc</pre>
<p>This operation might take some time, a couple of minutes or so.</p>
<p>When finished, you&#8217;ll probably have more disk space <img src='http://blog.xkoder.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>You should gc once is a while.</p>
<p><em>gc = garbage collection.</em></p>
<p>Yeah, that&#8217;s it! Just eight commands, but I&#8217;ve got two more things to make life easier for git users. So it adds up to ten right! <img src='http://blog.xkoder.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<h3>Extra 1 : Ignore some files; .gitignore explained.</h3>
<p>Not all files inside your project folder are required to be tracked by git. Some example include: object files, swap files etc.</p>
<p>Create a file name &#8216;<strong>.gitignore</strong>&#8216; in the base folder of your git repository and put in the name of files inside &#8216;<strong>.gitignore</strong>&#8216; as explained below, to make git ignore them.</p>
<p><strong>Simple wild-cards are recognized</strong>.<br />
&#8216;<strong>*</strong>&#8216; and &#8216;<strong>?</strong>&#8216; wild-cards plus regular expression&#8217;s square bracket `<strong>[ ]</strong>&#8216; notation is recognized.</p>
<p>So, for example, if you want to ignore all files ending with the extension &#8216;<em>bak</em>&#8216; you can specify the following in your .gitignore file.</p>
<pre>*.bak</pre>
<p>The above line, if present, in your .gitignore files will ignore all files ending with &#8216;.bak&#8217; anywhere inside the project folders.</p>
<p><strong>Ignoring a particular file or folder using absolute path notation.</strong></p>
<p>Suppose you have the following directory structure for your project, where &#8220;<strong>Project</strong>&#8221; is the base directory of your project&#8217;s repository.</p>
<pre>Project
    |-- .gitignore
    |
    |-- .git
    |
    +-- Folder_1
    |
    +-- Folder_2
    |       |
    |       \--- Folder_3
    |
    \-- Folder_4
            |
            +--- File_1
            +--- File_2
            +--- File_3</pre>
<p>Now suppose you want to ignore the folder `<em>Folder_3</em>&#8216; put following in your .gitignore file:</p>
<pre>/folder_2/folder_3/</pre>
<p>Notice the forward slashes at the beginning and at the end:</p>
<ul>
<li> If you remove the preceding forward slash, git will ignore all folders named &#8216;<em>folder_3</em>&#8216; inside any folder named <em>folder_2</em>.</li>
<li> Removing the forward slash at the end will make git treat the name &#8216;<em>folder_3</em>&#8216; as a file name instead of a folder. So in case if we had a file named &#8216;<em>folder_3</em>&#8216; inside <em>folder_2</em>, that would be ignored.</li>
</ul>
<p>Now, if you wanted to ignore the file <em>File_2</em> as shown in the diagram above, you place the following in your .gitignore file:</p>
<pre>/folder_4/file_2</pre>
<p>Notice we did not put a forward slash at the end, if we had, git will treat the name &#8216;<em>file_2</em>&#8216; as a folder name.</p>
<p>So here are the rules, re-iterated.</p>
<ol>
<li> Absolute paths begin with a forward slash &#8216;/&#8217;.</li>
<li> Folders names should always be suffixed with a forward slash &#8216;/&#8217;.</li>
<li> File names should NEVER be suffixed with a forward slash &#8216;/&#8217;.</li>
</ol>
<p>The use of square brackets as shown in the example below. They work very much like regular expression notation.</p>
<p><strong>An example .gitignore file.</strong></p>
<pre><span style="color: #999999;"># git ignore file
# comments start with hash</span>

<span style="color: #999999;"># ignore all object files, all
# files ending either with '.o' or '.a'</span>
*.[oa]

<span style="color: #999999;"># Ignore all files with
# the extension *.swp</span>
*.swp

<span style="color: #999999;"># Ignore a single file</span>
/folderA/folderB/build.info

<span style="color: #999999;"># Ignore a folder named
# temporary in the base folder.</span>
/temporary/

<span style="color: #999999;"># Ignore folders named _object
#  anywhere inside the project.</span>
_object/</pre>
<p><strong>What&#8217;s the use of .gitignore, after all?</strong></p>
<p>Well, if still haven&#8217;t figured out let&#8217;s look at a typical coding scenario.</p>
<p>You have your project, added all files to your .git repository, and did and initial commit. Now you start editing some files, your favorite editor creates some backup files ending with &#8216;.bak&#8217; extension.</p>
<p>Now you do git-status to check what all files changed, git tell you which files have been changed but also lists a the &#8216;.bak&#8217; files as being untracked.</p>
<p>After few days, the list of untracked files becomes so long, that doing git-status scrolls past your screen. Not pretty right! <img src='http://blog.xkoder.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Also, you cannot do &#8216;git add .&#8217; to stage files for commit, as this command will add the &#8216;.bak&#8217; files to be committed as well, unnecessary bloating your repository.</p>
<p>What&#8217;s the solution? Just create a .gitignore file inside your project&#8217;s base directory and place a line containing &#8216;*.bak&#8217; in it. Now do git-status; wow! neat &#8216;eh. <img src='http://blog.xkoder.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Extra 2 : Throw in some colors and introduces yourself to git.</h3>
<p><strong>Your name and email address in commits</strong><br />
Edit the file .git/config inside your base folder (project folder) and enter the following lines.</p>
<pre>[user]
    name = Your Name
    email = your email Id</pre>
<p>example:</p>
<pre>[user]
    name = xk0der
    email = amit@xkoder.com</pre>
<p>After placing these details in your file, the next time you commit any changes, your name and email id will be registered as the author of that commit.</p>
<p><strong>Colorizing git&#8217;s output.</strong></p>
<p>Gary has a small and simple post on how to get color&#8217;s in git&#8217;s output here:</p>
<p><a title="Colors in git" href="http://scie.nti.st/2007/5/2/colors-in-git">http://scie.nti.st/2007/5/2/colors-in-git</a></p>
<h3>That&#8217;s it! Go do some Git-fu.</h3>
<p>In the next installment I intend to cover distributed git setup and few more commands. Till then enjoy! <img src='http://blog.xkoder.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.xkoder.com/2009/06/06/git-tutorial-part-ii-sharpen-you-git-fu-with-10-more-commands/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Git Tutorial : Starting with git using just 10 commands</title>
		<link>http://blog.xkoder.com/2008/08/13/git-tutorial-starting-with-git-using-just-10-commands/</link>
		<comments>http://blog.xkoder.com/2008/08/13/git-tutorial-starting-with-git-using-just-10-commands/#comments</comments>
		<pubDate>Tue, 12 Aug 2008 21:25:24 +0000</pubDate>
		<dc:creator>xk0der</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[version control]]></category>

		<guid isPermaLink="false">http://blog.xkoder.com/?p=8</guid>
		<description><![CDATA[Part &#8211; II of this tutorial is now available here. Git is marvellous piece of tool every programmer must have. Git is a distributed version control system developed by Linus and others. Yes you guessed it, it is used heavily by the Linux (kernel) developers apart from many other projects. How will Git help you [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Git Tutorial Part II - Sharpen you Git-Fu with 10 more commands." href="http://blog.xkoder.com/2009/06/06/git-tutorial-part-ii-sharpen-you-git-fu-with-10-more-commands/">Part &#8211; II of this tutorial is now available here</a>.</p>
<p><a title="Git - Project page" href="http://git.or.cz/">Git</a> is marvellous piece of tool every programmer must have. Git is a distributed version control system developed by Linus and others. Yes you guessed it, it is used heavily by the Linux (kernel) developers apart from many other projects.</p>
<p><strong>How will Git help you and what this post covers.</strong></p>
<p><span style="text-decoration: underline;">Scope of this post</span> : This post will deal with the usage of Git by an individual developer only<span style="text-decoration: underline;">.</span></p>
<p><span style="text-decoration: underline;">Why use version control</span> : Version control gives you a great deal of flexibility while coding. You can make changes without having to worry about breaking things, as you can always move back to previous version(s) of your code. Version control also allows you to compare changes as you move ahead, helping in bug-fixing apart from other benefits.</p>
<p><span style="text-decoration: underline;">Why use Git</span> : There are a number of other version control systems available, some centralized systems some distributed systems. Amongst them, Git is one of the fastest version control system, very simple and easy to use, and has lot of helpful features packed in very a small package. Check my <a title="Why we picked Git and Trac" href="../2008/06/26/git-and-trac-a-love-story/">other post</a> to know more reasons.</p>
<h2>Prerequisite</h2>
<p>1) You need a computer <img class="wp-smiley" src="../wp-includes/images/smilies/icon_smile.gif" alt=":)" /> with preferably Linux (any flavor) installed on it. (This post does not deal with <a title="Git for windows" href="http://code.google.com/p/msysgit/">Windows</a>, though you may use the commands given here without any modifications (probably!)).</p>
<p>2) You need Git.</p>
<p>on Fedora distribution, you may install git using the following command</p>
<pre>$ sudo yum install git</pre>
<p>On Ubuntu distribution, you may install git using the following command</p>
<pre><span style="text-decoration: line-through;">$ sudo apt-get install git</span>
$ sudo apt-get install git-core</pre>
<p><em>(Edit: 21/Oct/2008 &#8211; Thanks to <a title="Robo47's blog" href="http://www.robo47.net/">robo47</a> for pointing out the mistake)</em></p>
<p>For other methods and building from source check out <a title="Git dowloads" href="http://git.or.cz/#download">this page</a>.</p>
<p>3) You need some project which you are developing (or would be developing) <img class="wp-smiley" src="../wp-includes/images/smilies/icon_smile.gif" alt=":)" /> . The project’s language could be anything .. C, C++, D, Python, Perl, HTML, JavaScript, plain text files … you got the idea right? <img class="wp-smiley" src="../wp-includes/images/smilies/icon_smile.gif" alt=":)" /></p>
<h2>Let the show begin</h2>
<h3>Command 1 : Initializing an empty git repository.</h3>
<p>Befor you can start using the power of git you need to create an empty repository and tell git which files to track. The first command that I’m presenting initializes an empty git repository.</p>
<p>Before executing the command you need to be at base folder of you project. For example if you have all the source code for you project stored here <strong>/home/xk0der/SourceCode/myProject</strong>, (We’ll call this folder as the <strong>base folder </strong>for ease of discussion), move there first. Now execute the following command once you are in the base folder.</p>
<pre>$ git init</pre>
<p>This will create an empty repository in the current directory (the base folder).</p>
<p>For every project that you want to track using git, go to its base folder and issue this command first and then the next 2 commands presented below.</p>
<h3>Command 2 : Adding files to git repository.</h3>
<p>The next step is to add files to your git repository, essentially telling git that these are the files we want to keep track of. Execute the following command from the base folder.</p>
<pre>$ git add .</pre>
<p>This will add all the files from the current folder and sub-folders recursively to the git repository.</p>
<h3>Command 3 : The commit command.</h3>
<p>This is one command you’ll be using quite often.</p>
<p>the above command adds the files but does not commit the changes to the git repository. So you need to issue the following command to make the changes final.</p>
<pre>$ git commit -a</pre>
<p>This will open a text editor (probably vi) where you can add some comments about this commit. If you prefer not to use the text editor, you can specify the commit message from the command line it self, just use the following format instead of the above one.</p>
<pre>$ git commit -a -m "Initial import"</pre>
<p>The -m switch is used to specify the commit message. In our case the commit message is “Initial import”. You can put anything between the quotes for your message.</p>
<h2>You are ‘all’ ready!</h2>
<p>Now open your favorite text editor and start coding. When you feel you have accomplished something or you just want to save the current state of your source code issue the git-commit command as described above. The commit command will create a new checkpoint and save the current status of your source code.</p>
<p>Git (like any other version control system) saves all the commits you have done to your source code repository. This enables you to go back and forth between commits and inspect the changes.</p>
<p>Now let’s move forward and learn a few more commands.</p>
<h3>Command 4 : Viewing the commit log.</h3>
<p>You can view all the commits you have done till now using the following command</p>
<pre>$ git log</pre>
<p>This command shows the commit history with the</p>
<p>1) Unique commit hash<br />
2) Name and email of the person who performed the commit<br />
3) Date and time of commit<br />
4) The commit message</p>
<p>Of all these, the first item, the unique commit identifier, the SHA1 hash is of importance to us. This hash will be used with some of the commands listed next, and is used with a plethora of other git commands, not listed here <img class="wp-smiley" src="../wp-includes/images/smilies/icon_smile.gif" alt=":)" /></p>
<h3>Command 5 : Checking the current status.</h3>
<p>While coding, you may want to see what all files have changed, before you do a commit to store them in the git repository. This command helps you achieve that.</p>
<pre>$ git status</pre>
<p>The above command will list all files that have changed since your last commit.</p>
<h3>Command 6 : Finding the difference between commits.</h3>
<p>Apart from just viewing files that have changed, you may be interested in viewing the actual differences in the source code.</p>
<pre>$ git diff</pre>
<p>The above command will show a difference ( a diff ) with respect to your last commit and current changes.</p>
<p>The diff command can actually be used to compare difference between any commits</p>
<p><strong>To view difference between a previous commit  and current changes.</strong></p>
<pre>$ git diff <em>commit_hash</em></pre>
<p>here the <em>commit_hash</em> is the first eight characters (hex digits) of commit hash as shown in the commit log by the git-log command. Yes! you need not specify the full hash, just the first eight digits are good enough.</p>
<p><strong>To view differences between two previous commits.</strong></p>
<pre>$ git diff <em>commit_hash_1</em> <em>commit_hash_2</em></pre>
<p>This command will display differences between the two commits identified by the <em>commit_hash_1</em> and <em>commit_hash_2.</em></p>
<h3>Command 7 : Creating branches!</h3>
<p>Don’t be scared by the word “branching”, specially if you never dealt with them. Branches are easy to play with and are very useful.</p>
<p>Before I show you the command a bit more discussion about branches.</p>
<ul>
<li>You can view branches as being diversion from the main linear commit tree.</li>
<li>All branches stem out from the <strong>master branch</strong>, the name given to the main trunk.</li>
<li>Every branch has it’s own commit log.</li>
<li>You can have different code in same files across difference branches. (This is the main feature of branching). Actually git goes even further and allows you to have not only different content in files across branches but even different sets of files across different branches.</li>
<li>At one point of time, you can be sitting only on one branch. What this means is that, the source code from the current branch will be the effective one, the one to which you would be making changes.</li>
</ul>
<p><strong>Why use branches?</strong></p>
<p>Suppose you have an almost stable source code and you are to release the product in a month or in a week or two. You also have plans to include another great feature into your code, but are afraid it might break your code and your release plans may go hay-wire.</p>
<p>This is where branches come into picture. Just create an experimental branch from you main stable trunk ( the one you’ve been working on up till now is the main trunk, the <strong>master</strong> branch). After you have created the experimental branch, you can continue fixing bugs and polishing the code on the main, i.e. the master branch. Meanwhile you can also continue working on the experimental branch without affecting a line of code on the master branch.</p>
<p>In the end, if you find that the experimental feature is working good enough, merge it into the master branch and release your product. If the experimental feature didn’t quite work out, you can still release the product from the master branch. The experimental branch is still there so you can continue working on it till it gets stable.</p>
<p>This is one scenario (and the most often reason, though not the only one), to create branches.</p>
<p>Okay now, back to commands . Issue the following command when you want to create a branch.</p>
<p><strong>Creating a branch</strong></p>
<pre>$ git branch <em>branch_name</em></pre>
<p><em>branch_name </em>could be anything that you wish, for example:</p>
<pre>$ git branch <strong>experimental_feature

</strong></pre>
<p><strong>Branching a branch or “I want more branches”.<br />
</strong></p>
<p>You can create as many branches from inside any branch you want, creating a very dense tree if you like.</p>
<p>Just move inside (check-out that branch, see command 8 below) and issue the branch creation command.</p>
<h3>Command 8 : Moving to a branch and listing all branches</h3>
<p>Once you have created the branch move to it by issuing the command listed below. <strong>Always</strong> Make sure you do a commit before you issue this command or else changes will move across branches. <em>Make this a habit! </em>(<span style="text-decoration: underline;">OR</span> have a look at git-stash command, discussed in <a title="Git Tutorial Part II - Sharpen you Git-Fu with 10 more commands." href="http://blog.xkoder.com/2009/06/06/git-tutorial-part-ii-sharpen-you-git-fu-with-10-more-commands/">part II</a> of this tutorial.)</p>
<pre>$ git checkout branch_name</pre>
<p>Here the branch name is the branch where you want to move. Once checked-out, you can view staus, log, diff etc, using the commands presented earlier.</p>
<p>To go back to the <strong>master</strong> branch (the main trunk) issue the following command. (Again make sure you had issued the commit command)</p>
<pre>$ git checkout master</pre>
<p><strong>Listing all branches</strong></p>
<p>Issue the following command to see all available branches in the current repository.</p>
<pre>$ git branch</pre>
<h3>Command 9 : Merging two branches</h3>
<p>Move to (checkout) the branch with which you want the merge to happen and issue the following command.</p>
<pre>$ git merge branch_name</pre>
<p>This will merge the branch branch_name with the current branch.</p>
<p>For example if you want to merger the “experimental_feature” branch with the master branch, issue the following commands</p>
<pre>$ git checkout master
$ git merge experimental_feature</pre>
<p>Git will notify you with any conflicts it cannot resolve automatically (if any). You can then resolve the conflicts manually.</p>
<p><strong>Deleting a branch</strong></p>
<p>After the merge is done you can delete the experimental branch if you wish by issuing the command</p>
<pre>$ git branch -d experimental_branch</pre>
<p>The above command will only delete the branch if it is fully merged with the current branch’s HEAD. HEAD is the current position in your branch, the latest commit.</p>
<h3>Command 10 : Deleting stuff from the repository.</h3>
<p>Issue the following command if you do not want git to keep track of a file or folder and (this is the opposite of the git-add command). The file stays in the working directory, on the disk.</p>
<pre>$ git rm --cached path/to/the/folder_or_file</pre>
<p>The <em>git-rm</em> command removes the files from the repository for current HEAD <em>only</em>; previous revisions/commits will still have the file.</p>
<h2>That’s it, you’re done!</h2>
<p>Before I leave you <img class="wp-smiley" src="../wp-includes/images/smilies/icon_smile.gif" alt=":)" /> a few more command/features that may interest you, but are not totally necessary as of now.</p>
<h3>A graphical repository viewer</h3>
<p>To invoke a graphical repository viewer, invoke the following command</p>
<pre>$ gitk</pre>
<h3>Git &#8211; Graphical user interface</h3>
<p>For those who prefer GUI, you can install a graphical front end to git.</p>
<p><strong>On Fedora</strong></p>
<pre>$ yum install git-gui</pre>
<p><strong>On Ubuntu</strong></p>
<pre>$ apt-get install git-gui</pre>
<p>Run the gui by issuing the following command</p>
<pre>$ git-gui</pre>
<h2>Yup, totally done now! <img class="wp-smiley" src="../wp-includes/images/smilies/icon_smile.gif" alt=":)" /></h2>
<p>That’s all folks. <img class="wp-smiley" src="../wp-includes/images/smilies/icon_smile.gif" alt=":)" /> I hope this post will help you in getting started with one of the most powerful version control system available around.</p>
<p>When you are done playing with these commands, you may want to read the second installement of this turotial here: <a title="Permalink for : Git Tutorial Part II - Sharpen you Git-Fu with 10 more commands." href="../2009/06/06/git-tutorial-part-ii-sharpen-you-git-fu-with-10-more-commands/">Git Tutorial Part II &#8211; Sharpen you Git-Fu with 10 more commands.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.xkoder.com/2008/08/13/git-tutorial-starting-with-git-using-just-10-commands/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Git and Trac &#8211; A Love Story.</title>
		<link>http://blog.xkoder.com/2008/06/26/git-and-trac-a-love-story/</link>
		<comments>http://blog.xkoder.com/2008/06/26/git-and-trac-a-love-story/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 22:58:10 +0000</pubDate>
		<dc:creator>xk0der</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[trac]]></category>
		<category><![CDATA[version control]]></category>

		<guid isPermaLink="false">http://blog.xkoder.com/?p=40</guid>
		<description><![CDATA[This is a post I’ve written at my Company blog. Here’s a small excerpt and the link to the complete post. Git and Trac &#8211; A Love Story Git and Trac, both got married very recently (at our setup ), and the newlywed couple are quite a charm to work with. It all started with [...]]]></description>
			<content:encoded><![CDATA[<p>This is a post I’ve written at my <a title="Hidden Reflex" href="http://hiddenreflex.com/">Company</a> <a title="HiddenReflex Blog" href="http://hiddenreflex.com/blog">blog.</a> Here’s a small excerpt and the link to the complete post.</p>
<p><a title="Full blog post." href="http://hiddenreflex.com/main/?p=28">Git and Trac &#8211; A Love Story</a></p>
<blockquote><p><a title="Git - The groom" href="http://git.or.cz/">Git</a> and <a title="Trac - The bride" href="http://trac.edgewall.org/">Trac,</a> both got married very recently (at our setup <img class="wp-smiley" src="http://hiddenreflex.com/main/wp-includes/images/smilies/icon_smile.gif" alt=")" /> ), and the newlywed couple are quite a charm to work with. It all started with the search for a <a title="Version Control - Wikipedia entry." href="http://en.wikipedia.org/wiki/Revision_control">version control tool</a> which could be used <em>now</em>, with just four of us using it, and that would scale beautifully to hundreds and thousands of users. The version control system would be the groom. Meanwhile we were also in search for a perfect partner (the bride), an <a title="Issue Tracking - Wikipedia entry." href="http://en.wikipedia.org/wiki/Issue_tracking_system">issue tracking system</a>, that would gel with our choice of version control seamlessly.</p>
<p>To begin with, we all (at Hidden Reflex) were leaning towards a <em>central repository</em> setup ….. <a title="Read the complete post." href="http://hiddenreflex.com/main/?p=28"><em>more</em></a></p></blockquote>
<p><em><br />
</em><span id="sharethis_1"><a class="stbutton stico_rotate" title="ShareThis via email, AIM, social bookmarking and networking sites, etc." href="javascript:void(0)"></a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.xkoder.com/2008/06/26/git-and-trac-a-love-story/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
