A Git startup cheat sheet

stashstash

A compilation of useful Git commands when starting with a fresh install.

Set user info:

git config --global user.name "John Doe"
git config --global user.email johndoe@example.com

Set the proxy:

The good way to configure the proxy for git is by defining the http_proxy, https_proxy and no_proxy environment variables used by curl and similar tools. Git allows to override http_proxy and https_proxy, as explained below, but unfortunately it does not offer the possibility to override the no_proxy variable.

git config --global http.proxy http://proxyuser:proxypwd@proxy.server.com:8080
git config --global https.proxy http://proxyuser:proxypwd@proxy.server.com:8080

Unset the proxy:

git config --global --unset http.proxy
git config --global --unset https.proxy

Do a rebase by default when pulling:

git config --global pull.rebase true

This is in order to avoid polluting the history with merge commits when pulling code from remote branches. In case you don’t want to use a rebase, you can still initiate a “fetch” followed by a “merge”. This need can occasionally take place when you’re responsible for the merges of features between remote branches. But even then, you shouldn’t need to do this frequently.

Don’t use fast-forward when merging:

git config --global merge.ff false

When merging feature branches (aka topic branches), you always want to create a merge commit because you want to track the branch where the changes were made in the history. Creating a merge commit allows to roll back a specific feature from the history at a later stage, or to merge the same modifications in another branch; if for example you work with development and production branches.

Do not commit automatically when merging:

git config --global merge.commit false

This gives you a chance to review the result of the merge before the commit is done. It works in conjunction with the non fast-forward option above.

Flag or fix whitespace problems:

git config --global core.whitespace trailing-space,space-before-tab

  • trailing-space warns about whitespaces at the end of a line or at the end of a file.
  • space-before-tab warns when there is a space before a tab used for indentation.

git config --global apply.whitespace fix

This last option is used in order to fix the whitespace problems when applying a patch.

Enable the recording of the conflict resolutions in order to reapply them more easily:

git config --global rerere.enabled true

Rerere is an acronym for reuse recorded resolution.

Posted in Tips and tricks | Tagged , | 2 Comments

How to install Docker in Ubuntu 14.04 and up

docker
Installing Docker in Ubuntu 14.04 and above is fairly easy. It just requires one line:

sudo apt-get install docker.io

Optionally, you may also want to use the docker command rather than docker.io:

sudo ln -sf /usr/bin/docker.io /usr/local/bin/docker

To get an Ubuntu image running inside your Ubuntu OS:

sudo docker.io pull ubuntu
sudo docker.io run -i -t ubuntu /bin/bash

The first command downloads a Docker container for an Ubuntu system. The second command launches this container with a Bash shell inside it. The option -t allocates a terminal, -i attaches stdin and stdout to it. Once you exit the shell, the Docker image will be switched off.

Alternatively, you may want to launch a specific version of Ubuntu: a container can contain multiple images. This command shows the available images that you have downloaded so far:

sudo docker.io images

Excerpt of the result:

REPOSITORY          TAG                 IMAGE ID
ubuntu              vivid               76ca2fd90787
ubuntu              15.04               76ca2fd90787
ubuntu              utopic              cfaba6b5fefe
ubuntu              14.10               cfaba6b5fefe
ubuntu              14.04               5ba9dab47459
ubuntu              trusty              5ba9dab47459
ubuntu              14.04.1             5ba9dab47459
ubuntu              latest              5ba9dab47459
ubuntu              12.04.5             69c02692b0c1

When launching without specifying an image version, Docker launches the image that has the latest TAG. You can check with the IMAGE ID column to which it corresponds actually. Inside the Ubuntu container, you can also launch this command in order to check the version:

root@daa88df2d9ad:/# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.1 LTS"

Now if you want to launch another version, you can simply preprend the TAG of the version you want to launch to the container in this way:

sudo docker.io run -i -t ubuntu:12.04.5 /bin/bash

A check of the version shows now:

root@04ecc1b35fe8:/# cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.5 LTS"
Posted in Tips and tricks | Tagged , | Leave a comment

Add a new virtual volume to an LVM partition of a virtual VMWare system running a RedHat type Operating System

lvm

 

 

  • Add a new drive to your virtual machine using the graphical VMware tool, currently VMware vSphere
  • Reboot the virtual machine so the new drive can be seen by the host Operating System
  • Launch the command vgdisplay as root in order to get the name of your LVM virtual group:

> vgdisplay
— Volume group —
VG Name               myvg
System ID
Format                lvm2
Metadata Areas        5
Metadata Sequence No  10
VG Access             read/write
VG Status             resizable
…etc

  • Use the command pvdisplay (i.e. “physical volume display”) as root in order to get the device name (/dev/sdx) of your new drive. You are looking for an entry that shows “Allocatable  no”
  • Add the new device to the volume group:
    vgextend myvg /dev/sdx
  • Get the location (LV Path) of your logical volume using lvdisplay (i.e. “logical volume display”)> lvdisplay
    — Logical volume —
      LV Path                /dev/myvg/rootvol
    …etc
  • Resize the logical volume, adding the size of the new drive (in this example 30 gigabytes):
    lvresize -L +30G /dev/myvg/rootvol
  • Resize the partition of the logical volume. Resize2fs is able to detect the system is live and is able to apply the correct change, even on the root partition:
    resize2fs /dev/myvg/rootvol
  • Optionally reboot the virtual machine, just to make sure all is fine
Posted in Tips and tricks | Tagged , , , | Leave a comment

Linux: find the dependent packages

packages

Sometimes, you would like to update a package, but you are not sure what other packages depend on it.

 

If you are on Fedora, Redhat or CentOS, this simple command will definitely help you:

repoquery --whatrequires --installed --recursive packagename

Using Debian, Ubuntu, Mint, …etc ?

apt-cache rdepends --installed --recurse packagename

Posted in Tips and tricks | Tagged , , , , | Leave a comment

Modify the default character encoding for the Vim text editor

Image By default, Vim tries to detect the character encoding of a file when it is edited and it will use that encoding when saving the file. In case of UTF-8 files, Vim will search for a BOM at the beginning of the file and, if not found (relatively likely), it will use the encoding of the platform (as defined by the LANG variable on Unix, or by the locale setting on Windows), or Latin-1 if no platform encoding is set.

You can always detect what is the current encoding with the following command under Vim:

:set encoding

You can also set a specific encoding for the duration of the editing session with this command:

:set encoding=utf-8

The command above sets the character encoding when saving the file. It also sets it for the internal buffers of Vim, unless the variable fileencoding is specified.

This can have dramatic effects in case you are editing an UTF-8 file and the default internal format for the buffers of Vim is set to something else than UTF-8 via the fileencoding variable.

You can check the value of this second variable using the command:

:set fileencoding

Similarly, you can specify a value with:

:set fileencoding=utf-8

If you always want to reuse the same encoding, you can set the default value for these two variables in a .vimrc (Unix) or _vimrc (Windows) file, saved into your $HOME directory (%HOMEDRIVE%%HOMEPATH% on Windows).

Don’t forget to restart Vim for the changes to take effect.

Posted in Tips and tricks | Tagged , , , , | Leave a comment

Create registry file (.reg) equivalent to the RegSvr32 command

If you have already programmed a bit in OLE/ActiveX/Shell extensions/etc. in the Microsoft world, you already know the RegSvr32 command used to register/unregister these dll components. The regsvr32.exe command only calls the DllRegisterServer entry point, i.e. a method with that name located in the DLL. Typically, but not limited to, this method creates keys (nodes and leaves) in order to register the component inside the Windows registry.

Now, let’s say you have an ActiveX component and you want to see the keys it would create when self-registering, but for some reason you don’t want to actually install it on your computer. Visual Studio has a tool that is extremely convenient for this. It is called Registry Capture (regcap.exe). You can launch it on your dll component in order to generate a reg file containing the registry keys:

regcap /O output_file.reg activex.dll

Posted in Tips and tricks | Tagged , , , , , , , , , | 3 Comments

Workaround the Grub installation issue in Debian Wheezy

Debian logoThe current testing version of Debian, aka Wheezy, contains a rather annoying bug that prevents the installation of Grub, the multi-OS bootstrapper of Linux. Although this bug will be corrected at some point in the possibly near future, it is already present for a few months, making life harder for the fresh installations. The traditional workaround in order to install Debian Testing is to install a stable version (Squeeze) and then modify your mirrors to upgrade it to a testing version. In this way Grub will be installed correctly. However, if you have encountered the Grub issue, this means you have already completed your install from a broken edition (if you encounter the issue but you haven’t completed the install yet, just finish it in a clean way without a bootloader, and then continue reading here). So, maybe you don’t want to download a new ISO image, burn a new DVD or write a new USB key with it, and then restart the installation process from scratch. The other solution is then to keep the installation that you have just done and to setup the installation of Grub from the stable mirrors. It’s quite straightforward once you know how to do this.

First you need to boot your fresh installation. In order to do this, you can use any valid Debian bootable media, like for example the media you have used to do your installation. In the boot menu, you should select ‘Advanced Options’ and then the ‘Rescue Mode’. This will launch a recovery shell as root user (be careful). In the following screens, select the root partition where Debian is installed in order to mount it (mountpoint /).

The first thing to do now is to make sure the network is started and accessible. You can try a ping debian.org.

Next, use vi to modify the /etc/apt/sources.list file. Make sure the installation media (CD, DVD, USB, etc.) is commented out and replace ‘wheezy’ by ‘squeeze’ in the lines where the networked mirrors are specified (lines starting with deb and deb-src followed by a URL, do not modify the ones that contain the URL for the security patches). Save, quit the editor and update aptitude in order to apply the changes: aptitude update. Now you can download the grub-pc package from the Squeeze mirrors: aptitude install grub-pc. During the install process, you will be asked the partition where you want to get it installed. When this is finished, run update-grub in order to be sure the configuration files are correctly generated. This last step is probably not necessary, but let’s play it on the safe way just in case.

Now, you can remove your bootable media and cleanly reboot the computer: shutdown now -r. It should restart fine.

The next thing to do, as root, is to modify back the /etc/apt/sources.list file with ‘wheezy’ instead of ‘squeeze’ and launch again the command aptitude update to apply the change.

Posted in Tips and tricks | Tagged , , , | 11 Comments