Archivo de la categoría: GIT

Using multiple SSH keys for same host in Ubuntu

Any developer has at least one SSH key, mostly used for login through SSH to remote machines or working with GIT repositories.

When we try to connect through SSH to another machine or pulling from a GIT repository, our system is going to use the first SSH key we created by default.

So, what happens if we want to use different SSH Keys for same server or same repository? How we deal with this trouble?.

We have to make a config file in our SSH folder and specify to the system which key has to use given host.

Let’s see an example where we have two different repositories hosted in Github.com and we want to use different SSH keys for each repository.

First we have to create our ~/.ssh/config file:

Host project_1
    HostName github.com
    IdentityFile ~/ssh/dev_key

Host project_2
    HostName github.com
    IdentityFile ~/ssh/deploy_key

For example, it is common having multiple SSH keys in our production server, where each SSH key have access to specific repositories.

Now in our production server, if we want to pull changes for our project_1 we have to do run this command:

git pull git@project_1:user/project.git

For project_2, will be the same command with the host in the repository url modified:

git pull git@project_2:user/project.git

Analyzing the config file, Host it’s the alias for our hostname, in our case github.com, and in the IdentityFile we link to our SSH key path.

That’s all, easy and powerful.

How to show the current GIT branch name in command prompt

git-branches

We can get the current GIT branch name in command prompt with adding a simple code into the .bashrc file.

Open your terminal and type the following command:

sudo nano ~/.bashrc

Go to the end of the file and add this code:

function parse_git_branch () {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
 
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
NO_COLOR="\[\033[0m\]"
 
PS1="$GREEN\u@\h$NO_COLOR:\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "

Save file changes.

Now, if you open a new terminal and access into a GIT repository, you will see the current GIT branch name.

How to ignore folder tracking with GIT

A frequently asked question on GIT is how to ignore a folder tracking. We can have in our project folders which contains cache files, file uploads, etc and are unnecesary to be included in our GIT repository.

To do this, we need to create a .gitignore file on our GIT root folder.

Then, if we want to disable a folder for tracking, we add the next line on the file:

folder/*

 
Save the .gitignore file.

Now all files and subfolders from this directory will be ignored by GIT.

How to manage a website with GIT

It is not known by many people, but we can use GIT to manage our website or web project in seconds, without the need to upload one by one all the files through a FTP clients.

Maybe, your are asking: Why I must start using it?

  • You can update your website to the last version in seconds.
  • You can go back to the lastest stable version in seconds in case you have any problem on production environment.
  • Prevent human errors when you are uploading many files with a FTP client.
  • Your website update is faster.
  • Prevent reupload unnecesary files.

If you want to try it, continue reading:

First, you need a Linux server with shell access and root permissions.

Obviously, you must have GIT installed in your server.

sudo apt-get install git

Once you have installed GIT, create a folder where you are going to save our GIT repositories.

mkdir /var/www/gitrep/project.git

Access to the just created folder

cd /var/www/gitrep/project.git/

Init a GIT bare repository

git init --bare

A bare repository, is a central repository. This means that is a repository where we only are going to do push from our project.

Now, we create a HOOK which is used for the auto-updating the folder where is located our web project in our web server when we do a push.

sudo vi /var/www/gitrep/project.git/hooks/post-update

Enter the next content:

GIT_WORK_TREE=/var/www/project/ git checkout -f

Replace ‘/var/www/project/’ for the path where is located your project folder in your web server. After this, save the file changes.

Then, you must set necessary permissions to the HOOK you created before.

chmod +x /var/www/gitrep/project.git/hooks/post-update

With the GIT repository configured in your web server, it is time to configure a few things in our local repository (working copy).

First, add remote repository to your local repository:

git remote add remote_repository_name ssh://user@host/var/www/git_rep/project.git

Set write permissions to your GIT repository

sudo chmod -R 0777 /var/www/git_rep/project.git

Push local repository content to remote repository

git push remote_repository_name +master:refs/heads/master

In case that your web server hasn’t updated your project with your local repository content, you must to do a local change in some of your project files, then create a new commit, and push to the remote repository again.

git push remote_repository_name master

And this is all you must to know. Now, when you do a push into your remote (production) server your project will be updated automatically.

If you have any question, tell us as a post comment.