A short Git reference

A short git reference

A short git reference explains the most common git commands for setting up repositories, cloning remote repositories, etc.

Repository Setup

git config

Git should be set up with a minimum configuration before starting to create new repositories. With  git config it is possible to configure user settings and repository preferences. The most common settings are provided below.

set user name and email for commits

#set the user name and e-mail address which should be used for commits to the current repository
git config user.name <USERNAME>
git config user.email <EMAIL>

#alternatively do the same with the --global option which will store the config settings in the user's home ~/.gitconfig
git config --global user.name <USERNAME>
git config --global user.email <EMAIL>

set alias for git commands

git config --global alias.a "add --all"
git config --global alias.co checkout
git config --global alias.ci commit

set default editor for commits

#for <EDITOR> put in the name of the default editor which should be used for git commands
git config --system core.editor <EDITOR>

set http proxy

git config --global http.proxy <HTTP_PROXY>

open global config file in editor

git config --global --edit

The configuration settings from above should result in a .gitconfig file similar to the one below:

[user]
    name = Test User
    email = test.user@somedomain.com
[alias]
    a = add --all
    co = checkout
    ci = commit
[core]
    editor = vi
[http]
    proxy = http://some.proxy:8080

Note that Git can be configured in different ways:

  1. ~/.gitconfig – store config settings on a per-user basis
  2. <REPOSITORY>/.git/config – store config settings for a specific repository
  3. /etc/gitconfig – store config settings system wide

Since the git config command is very powerful, have a look at the full documentation on Git Config for further options.

git init

Creates a new Git repository. Also, creates a .git directory in the repository’s root directory where all the necessary meta data will be stored.

#create a git repository within the current path
cd /path/to/git/repository
git init

#create a new git repository within the specified path
git init /path/to/git/repository

Create a new Git repository without a working directory by using the –bare flag. This creates a central repository for shared usage. By convention such a directory should end with the suffix .git

git init --bare /path/to/git/<REPOSITORY>.git

If several users should be able to share the central repository use the following command. This allows users belonging to the same group to push into the repository.

git init --bare --shared /path/to/git/<REPOSITORY>.git

git clone

Creates a completely isolated copy of an existing Git repository. The repository might be on the local or a remote machine. Remote repositories can be accessed through http or ssh.

git clone <REPOSITORY>

Create a local copy of <REPOSITORY> into the directory <DIRECTORY>

git clone <REPOSITORY> <DIRECTORY>

Example

Assume we created a repository with git init on server myserver.com. We then want to get a copy of this remote repository on our local machine.

#on server myserver.com a new repository is created
git init --bare --shared /path/to/git/<REPOSITORY>.git

#we now create a local copy of the newly created remote git repository
git clone ssh://<USER>@myserver.com/path/to/git/<REPOSITORY>.git

Publish changes

Git workflow

The local Git repository consists of three parts which are relevant when changes are published.

  • working copy – consists of the real files to be committed to the repository
  • staging area (index) – a logical intermediate stage between working copy and HEAD
  • HEAD – pointing to the local project history

Note: the following commands only influence the local repository.

git add

Tells Git to update changes from the local working copy to the staging area.

#changes in FILE/DIRECTORY will be staged for the next commit
git add <FILE|DIRECTORY>

git commit

Commit changes from the staging area to the local project history. Note: the git commit command does not have an impact on the status of the remote repository.

#commit the staged files to the HEAD
git commit

#commit the staged files to the HEAD and provide a commit message
git commit -m "commit message"

git push

Commit changes from the local to the remote repository.

#commit changes from from <LOCAL_BRANCH> to the <REMOTE> repository
git push <REMOTE> <LOCAL_BRANCH>

Example

git add demo.java
git commit -m "initial commit"
git push origin master

iSCSI initiator with VirtualBox and Synology NAS

This is a short description of how to configure an iSCSI initiator in VirtualBox using a Synology NAS.

Synology iSCSI initiator and VirtualBox

This approach is ideal for Synology users who want to use their NAS as a remote storage for VM images. Instead of wasting local disk space one can easily create an iSCSI LUN Target in Synology’s control panel and access it through a local Virtual Box instance. Also, the iSCSI protocol perfectly fits in as a fast communication alternative when accessing the NAS through wireless lan.

Setup

I used the following setup for testing:

  • Synology NAS DS-415+
  • VirtualBox 4.3.26
  • Guest OS: Ubuntu 12.10 Desktop i386
  • Host OS: Mac OS X

Approach

There are a few things necessary up-front before we can start. Not all of them will explicitly be explained in this post, but additional links will be added where necessary.

Create an iSCSI LUN target in your Synology admin panel

For details, see Use iSCSI Target service on Sinology NAS. In short, just do the following:

  • go to Storage Manger > select iSCSI Lun > click create
  • select iSCSI LUN (Regular Files) (this ist the only LUN type available when you only have a single storage volume)
  • next, provide a descriptive name for the iSCSI LUN. In my case I named it after the os this LUN will be hosting: ubuntu-12.10-desktop-i386
  • set the size for the LUN slice you want to create
  • within the next step provide a name for the related iSCSI LUN Target which will later be referenced by the Virtual Box VM. In my case I set it to: TARGET-ubuntu-12.10-desktop-i386. Note, that you will connect your VM through the IQN name later
  • all other options are explicitly explained within the documentation mentioned above
  • that’s all about the Synology setup. The next step will explain how to configure the virtual machine

Create a new Virtual Machine in VirtualBox

The final step consists of the VM setup. It is assumed that the process of setting up a virtual machine with VirtualBox is well known, otherwise visit Virtual Box – First Steps

Since we want to attach the iSCSI target we created before on our Synology NAS we need to explicitly tell Virtual Box (during the process of the vm setup) that we don’t want to add a local hard drive yet. After completing the VM setup we would have to add the iSCSI target manually.

Follow these steps when setting up the VM:

  • create a new VM by choosing a name (which you have to refer to later on), the guest operating system and its version
  • choose the appropriate memory size
  • when you’re stated to select a hard drive choose “no hard drive” and click create. Ignore the following warning from VirtualBox telling you that you cannot run a VM without a properly set up hard disk
  • now the VM has been created and we have to tell VirtualBox to attach our iSCSI LUN Target to the VM’s hard disk controller. Since this is not possible through the VirtualBox UI we have to use the built in command VBoxManage which is shipped with VirtualBox.
  • Open a new terminal on your Mac/Linux OS and type in the following command (on Windows systems first open a command line and navigate to the VirtualBox installation directory) :
VBoxManage 
  storageattach <VBox VM Name> 
  --storagectl  <VBox Storage Controller Name> 
  --port 0 
  --device 0 
  --type hdd 
  --medium iscsi 
  --server <Synology NAS IP address> 
  --target <IQN Name of iSCSI LUN Target> 
  --tport 3260
  • replace the following expressions with your concrete settings:
    • VBox VM Name: the name of your newly created VM
    • VBox Storage Controller Name: lookup the name of the SATA storage controller in your VM’s mass storage settings
    • Synology NAS IP address: the internal ip address of your Synology NAS
    • IQN Name of iSCSI LUN Target: the exact name of the IQN identifier generated for the Synology iSCSI LUN Target (Storage Manger > select iSCSI Lun Target)
    • you can see an example for my concrete setup below
VBoxManage 
  storageattach "ubuntu-12.10-desktop-i386"
  --storagectl  "SATA" 
  --port 0 
  --device 0 
  --type hdd 
  --medium iscsi 
  --server 192.168.2.15 
  --target "iqn.2000-01.com.synology:diskstation.Target-1.45" 
  --tport 3260
  • for detailed explanations on additional options of the VBoxManage command and the meanings of the options provided above have a look at VBoxManage
  • now run the newly created VM and enjoy!