Common Git Commands Reference/Getting Started

Here’s a round up of the main features that I have made use of in getting started with Git. This is by no means a complete list, but I thought it could be a great supplement to any “Getting Started” guide you may have (feel free to comment and post links!).

Basic Commands

Here’s a run-down of the most common commands I have found myself using.

Command Description

git help commandName

REALLY your friend when getting started! Use it!

git init

Creates a Git repository in the current directory.

git status

Reports the current repository status (e.g. commits pending etc.)

git add

Add files to source control (use the “-A” switch for ALL files in the directory.

git commit

Performs a commit operation to the current repository.

git branch

Lists the branches in the repository (with the current branch marked with an asterisk).

git branch name

Create a new branch called with the given name. This will be created based on the HEAD revision of the master (i.e. latest code).

git branch -d

Deletes a merged branch. If you wish to delete a branch regardless of it’s merged status, use “-D” instead.

git checkout branchName

Change the working tree to the given branch name.

git checkout –b newBranchName

Creates a new branch with the given name and switches it to the working tree.

git log

Lists the log messages for the current branch.

git gui

Pretty nice, simple GUI for a more visual run-down of the current Git state.

git reset --hard

Reverts the working tree back to HEAD revision (useful for messing with code and not wanting to commit).

Ignoring Files & File Types

I found the best (and easiest way) to do this is to create a text file that contains the ignore patterns you would like to add. For example:

   1: # Core Excludes for Git
   2: # Cached items used by VS.
   3: obj/
   4: bin/
   5:  
   6: # Resharper Cache
   7: _ReSharper.*/
   8: *.resharper.user

Once we have created the file, we then need to tell Git to use that file for its exclude configuration. We do this using the “git config” command to set the environment variable “core.excludesfile” like so:

   1: git config core.excludesfile {path}

While working with the excludes file you will probably want to test that your patterns are matching correctly. This is where “dry run” will be your friend. When you have added your excludes file to your configuration, flick back to your project and type:

   1: git add –A –-dry-run

Here we are telling git to add all the files in the project directory (using the “-A” switch) but we have also specified it is a dry run (note the double-dashes preceding dry). This will basically list all the files that would be added if you had not have specified “--dry-run”.

Now, if you are anything like me, your first pattern may not be right. So you will go back and update the excludes file, save and then try again.

But wait, there is no difference to what is matched? It would appear that Git stores the configuration in memory once loaded (makes sense) so we need to reset it. Simply re-execute the “git config core.excludes {path}” command again and all will be well.

One other point to note, patterns are case-sensitive… That one had me for a good five minutes.. :)

Wrap Up

It is very likely that this post will continue to grow as I come across new commands/processes etc. So it may be one to fave in the RSS reader if you have found it useful.

If you have any ideas/suggestions for other items to add to the list, please let me know!

Obviously I have not covered merging yet, but that is the next on the list. I do also intend to add working with remote repositories (for those wanting to use GitHub) soon, so check back for that!

Comments

Popular posts from this blog

GTD: Biphasic Sleep Experiment – Day 4

Visual Studio: Unknown Build Error (HRESULT 0x80131515)

TDD – Getting Started with Test-Driven Development