Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Tree: e6e7de7a1b
Fetching contributors…

Cannot retrieve contributors at this time

74 lines (45 sloc) 4.452 kB
layout: master
title: Pro Git 2.11 Git Basics Tips and Tricks
<h2 id='tips_and_tricks'>Tips and Tricks</h2>
<p>Before we finish this chapter on basic Git, a few little tips and tricks may make your Git experience a bit simpler, easier, or more familiar. Many people use Git without using any of these tips, and we won’t refer to them or assume you’ve used them later in the book; but you should probably know how to do them.</p>
<h3 id='autocompletion'>Auto-Completion</h3>
<p>If you use the Bash shell, Git comes with a nice auto-completion script you can enable. Download the Git source code, and look in the <code>contrib/completion</code> directory; there should be a file called <code>git-completion.bash</code>. Copy this file to your home directory, and add this to your <code>.bashrc</code> file:</p>
<pre><code>source ~/.git-completion.bash</code></pre>
<p>If you want to set up Git to automatically have Bash shell completion for all users, copy this script to the <code>/opt/local/etc/bash_completion.d</code> directory on Mac systems or to the <code>/etc/bash_completion.d/</code> directory on Linux systems. This is a directory of scripts that Bash will automatically load to provide shell completions.</p>
<p>If you’re using Windows with Git Bash, which is the default when installing Git on Windows with msysGit, auto-completion should be preconfigured.</p>
<p>Press the Tab key when you’re writing a Git command, and it should return a set of suggestions for you to pick from:</p>
<pre><code>$ git co&lt;tab&gt;&lt;tab&gt;
commit config</code></pre>
<p>In this case, typing git co and then pressing the Tab key twice suggests commit and config. Adding <code>m&lt;tab&gt;</code> completes <code>git commit</code> automatically.</p>
<p>This also works with options, which is probably more useful. For instance, if you’re running a <code>git log</code> command and can’t remember one of the options, you can start typing it and press Tab to see what matches:</p>
<pre><code>$ git log --s&lt;tab&gt;
--shortstat --since= --src-prefix= --stat --summary</code></pre>
<p>That’s a pretty nice trick and may save you some time and documentation reading.</p>
<h3 id='git_aliases'>Git Aliases</h3>
<p>Git doesn’t infer your command if you type it in partially. If you don’t want to type the entire text of each of the Git commands, you can easily set up an alias for each command using <code>git config</code>. Here are a couple of examples you may want to set up:</p>
<pre><code>$ git config --global checkout
$ git config --global branch
$ git config --global commit
$ git config --global status</code></pre>
<p>This means that, for example, instead of typing <code>git commit</code>, you just need to type <code>git ci</code>. As you go on using Git, you’ll probably use other commands frequently as well; in this case, don’t hesitate to create new aliases.</p>
<p>This technique can also be very useful in creating commands that you think should exist. For example, to correct the usability problem you encountered with unstaging a file, you can add your own unstage alias to Git:</p>
<pre><code>$ git config --global alias.unstage &#39;reset HEAD --&#39;</code></pre>
<p>This makes the following two commands equivalent:</p>
<pre><code>$ git unstage fileA
$ git reset HEAD fileA</code></pre>
<p>This seems a bit clearer. It’s also common to add a <code>last</code> command, like this:</p>
<pre><code>$ git config --global alias.last &#39;log -1 HEAD&#39;</code></pre>
<p>This way, you can see the last commit easily:</p>
<pre><code>$ git last
commit 66938dae3329c7aebe598c2246a8e6af90d04646
Author: Josh Goebel &lt;;
Date: Tue Aug 26 19:48:51 2008 +0800
test for current head
Signed-off-by: Scott Chacon &lt;;</code></pre>
<p>As you can tell, Git simply replaces the new command with whatever you alias it for. However, maybe you want to run an external command, rather than a Git subcommand. In that case, you start the command with a <code>!</code> character. This is useful if you write your own tools that work with a Git repository. We can demonstrate by aliasing <code>git visual</code> to run <code>gitk</code>:</p>
<pre><code>$ git config --global alias.visual &quot;!gitk&quot;</code></pre>
<div id='nav'>
<a href='ch2-10.html'>prev</a> | <a href='ch2-12.html'>next</a>
Jump to Line
Something went wrong with that request. Please try again.