Tag Archives: GIT

Git on CentOS

How to install GIT on CentOS

Today I would like to show you how I install GIT on CentOS servers. It might be super easy or slightly harder. It depends if you want to have the latest version (2.16.3) or you don't care:)

Easy way to install git on CentOS with yum

The easiest way is to use yum of course. Simply type

sudo yum install git -y

and it will install git on your server. That's it 😀 However if you will check your git version:

git --version

you will note that it will install 1.8.3.1 version (or higher). But wait! Isn't there git v2.0? Yes, there is. And I will show you how to install it.

How to compile git from source on CentOS?

I show you two approaches. The only difference is with using OpenSSL version. By default CentOS is shipped with older version. I like to have most recent version so I compile OpenSSL from source. It is not mandatory, since you can use default version.  If you prefer to upgrade it  follow this tutorial first and get back here once you will have latest version. If not just follow along.

There are 5 mandatory and 2 optional steps to have latest Git version on CentOS.

First you need to install some tools that are required to compile git. You will need some libraries and compiler. Install them by executing following command:

sudo yum install autoconf libcurl-devel expat-devel gcc gettext-devel kernel-headers openssl-devel perl-devel zlib-devel -y

Here is the fun part. Even if you don't have git installed, after executing command above you will actually have git on your server. It is because one of the packages has git as a dependency. But you shouldn't worry about that too much now.

If you didn't compile latest OpenSSL like I did you must install one more package:

sudo yum install openssl-devel -y

Step two is to download git source code. The easiest option is to download it from GitHub. Visit git release page - https://github.com/git/git/releases and pick desired version. I'm usually using latest, non-rc version. In my case it's 2.16.3. Right click on tar.gz icon and copy the link.

Now execute following command on your server (link you see is copied from GitHub):

curl -O -L https://github.com/git/git/archive/v2.16.3.tar.gz

It will download tarball with git source files to your server. -O flag will save the file instead of redirecting curl output to stdout. -L will follow redirects if any (usually there is one when using GitHub releases).

Step three is obviously unpacking downloaded source code:

tar -zxvf v2.16.3.tar.gz

Now is the fun part - compiling. Very important is to know, that you should never ever use root account for compiling. This might lead to serious security issues. Imagine that you will compile code that contains malicious parts as root. This code can modify your server and cut off your root access. Google about it, there were such cases in the past. So if you use root account, think about creating separate account to manage your server. Read this tutorial to learn how to setup additional account with root privileges. I'm not saying that you can't compile it as root, because obviously you can. But you just shouldn't do that.

Execute following command as non-root account:

cd git-v2.16.3
make clean
make configure

Depending on which OpenSSL you are using...

If you compiled latest OpenSSL use following command:

./configure CFLAGS='-I/usr/local/openssl/include' LDFLAGS='-L/usr/local/openssl/lib' --prefix=/usr/local/git --with-openssl=/usr/local/openssl

If you are using regular version:

./configure --prefix=/usr/local/git

Note parameter --prefix in configure command above. It will prevent overriding Git installed via yum. It will also help with overriding compiled version with packages installed from yum.  Once package is configure run:

make

For installing you need to have root account or be in sudoers group. So last step is installation:

sudo make install

Now you have latest version of git in your system!

Optionally you can remove downloaded package and uncompressed directory. To cleanup execute:

cd ..
rm -rf git-v2.16.3 v2.16.3.tar.gz

Adding GIT to $PATH

Here is the important part of this tutorial. To force using GIT that was just installed we need to add it to $PATH. It will prevent running git installed with yum.

Create file git.sh in this location:

sudo vi /etc/profile.d/git.sh

and paste there following contents:

pathmunge /usr/local/git/bin

After exiting and logging again to your SSH session you will always call GIT compiled from source:)

How to update GIT to newer version?

When new version of GIT will be released, simply repeat the steps in this tutorial, but with newer version. You don't need to remove GIT that is currently installed. New build will override the one that you already have.

Drawbacks of compiling git from source

You must know that there are certain drawbacks of compiling git from source. There are not really painful to be honest.

  1. You must manually check if there is new version of git. When you try to do yum update, you won't see it on the list
  2. Theoretically you will have two GITs installed - one via yum (old version), and latest version compiled from source. In practice, you will only use the one that was compiled.

If you don't want to install git manually you can use our Lamp On Steroids project based on Ansible. We have git role that automates the whole process.

GIT Deployment Keys

GIT Deployment Keys on Windows

Hello everybody!
Today I'd like to show You how to setup deployment keys on Windows OS. It's pretty helpful and time saving thing. If You are tired of passing username and password each time, it'll be simpler to use deployment key. After git push, your won't have to pass username and password.

OK, so how to start with deployment keys?

I'm using both GitHub and Bitbucket. They have possibilities to use deployment keys. I'm not entirely sure how about the rest of services. You will have to click through the settings to check if there's any Deployment key option. As I assume You already have the account. There's no need to read this article further if You don't have such account:p

Second thing that You have to have is GIT. Personally I use msysgit. But deployment keys trick will work on any other GIT that You are using.

Third, the most important thing is Your SSH key. Usually I'm using PuTTY Key Generator (puttygen.exe) to create public and private key pair. But You can use any software like OpenSSH. If You don't know how to generate private and public key, You can learn how to do this here. Or just typing in google something like "windows generate ssh key". There are plenty of good tutorials for that, depends on which software You are using for generating ssh key. While You are using PuTTY Keygen please choose from menu Conversion -> Export OpenSSH key. It will save Your ssh key in OpenSSH format.

When You are saving Your private and public key, it's nice to stick to Linux naming convection. So name Your keys like that:

  • private key -> id_rsa
  • public key -> id_rsa.pub

It's not necessary of course, but it's more readable to other users.

What to do with public ssh key?

When You have your keys generated which will become Your deployment keys, please login to GitHub or Bitbucket. In Your account settings You will find SSH Key options.

  • In Bitbucket You can find this section by visiting Manage Profile and SSH key.
  • In GitHub it's on Account Settings and SSH Key

In both cases please press Add SSH key  button in order to add new one. Label it as You wish and paste SSH public key into Key text area.

Check Repository config

When You create new repo, it's not a problem as long as You copy SSH not HTTPS URL for Your repo.

If You have existing repository, You will have to change it's config file if You want Your deployment keys to be working correctly. Simplest way is just to edit it in text editor.

Please open .git/config file. On Windows .git directory is hidden by default. Modify this file like this:

[remote "origin"]
    url = https://github.com/username/your_project.git //Remove this line
    url = git@github.com:username/your_project.git //Add this line

You can also do it from console by typing this command:

git remote set-url origin git@github.com:username/your_project.git

If You are not sure what Your repository ssh URL should look like, please login to Bitbucket/GitHub, go to Your repository and under clone choose SSH instead of HTTPS. The URL for clone will be the URL that You have to provide in set-url or paste it after url = in .git/config file.

Where to put my ssh keys?

That's very good question actually. When I wanted to setup deployment keys at the first time I didn't really know where to put them. In Linux OS we have .ssh directory in our home directory. So You just have to copy it there and it will work.

On Windows OS there is also ~ directory. Just type ls ~ in You command line and it will take You to Your home directory. Usually it'll be C:\User\USERNAME where USERNAME is Your Windows username. You can create .ssh directory there and it will behave like on Linux OS.

If this trick doesn't work for You, please try to create .ssh folder and copy your keys to msysgit installation folder.

Is it working?

Let's test our setup with deployment keys. Try for instance git pull. After first connection You will be asked if You want to add Your key permanently to know_hosts file. Type yes. You will have to type your passphrase to key if You set one. After that You can use deployment keys and never be notified that You have to type username or password.

Still not working? Please describe Your setup and errors in comments below. I'll try to solve Your problem:)

Our services: