Saturday, October 18, 2014

How to rename a GIT tag in the remote repo?

Some times we would have added a tag, pushed it to remote and then realized that we’d named it wrong. 
Ex. VER_7_2_0 instead of VER_7_1_2. 

Here is quick tip to rename a git tag in the remote repo. 

Step 1: Create a new tag on the same commit point of old tag and push it, 
git tag new_tag old_tag 
git push --tags 

Step 2: Delete the old tag from local repo. 
git tag -d old_tag 

Step 3: Delete the old tag from remote repo. 
git push origin :refs/tags/old_tag 

You can verify the changes made, by executing the below command. It shows all remote tags: 
git ls-remote --tags origin

Saturday, October 4, 2014

Configuring Git Email Notifications via Post-receive hook

Here I'm going to explain how to configure Git to send email notifications when 'central' repo is updated on Linux and windows machines. 
LINUX:
Step 1: Copy (or symlink) the post-receive script, or download it here. The script must be in the ‘hooks’ directory like this:
$GIT_DIR/hooks/post-receive
Make sure it is named “post-receive” so that Git recognizes it.
Step 2: Make sure that the script is executable:
chmod a+x $GIT_DIR/hooks/post-receive
Step 3: Edit the first line of $GIT_DIR/description to be your repository name. This will be in the subject of the email.
Step 4: Edit $GIT_DIR/config with some email settings such as recipient / sender email and subject-line prefix. Here’s some basic settings (read the script for all possible settings).
[hooks]
mailinglist = "receiver1@receivers.com, receiver2@receivers.com"
envelopesender = sender@senders.com
emailprefix = "[GIT] "
Note: you can also set these by using git-config:
git-config hooks.mailinglist "receiver1@receivers.com, receiver2@receivers.com"
git-config hooks.envelopesender sender@senders.com
git-config hooks.emailprefix "[GIT] "
That’s it ! Try doing a git-push to your shared repository, and see if you get email notifications.
WINDOWS:
STEP 1: Same as Linux.
STEP 2: The post-receive script is using 'sendmail' for sending mails. But Git for Windows provides msmtp not sendmail. So we need to configure smtp server and use it in the script. Replace sendmail with msmtp in the post-receive script as follows
send_mail()
{
    if [ -n "$envelopesender" ]; then
        msmtp --host="$smtpserver" -t -f "$envelopesender"
    else
        msmtp --host="$smtpserver" -t
    fi
}

And finally, near the end of the file is a section that reads in the git config variable so add a new line to read server values:

smtpserver=$(git config sendemail.smtpserver)

STEP 3: Same as Linux.
STEP 4: It requires all configuration as mentioned in step 4 Linux. Apart from that it requires one more variable to configure "sendemail.smtpserver"
git-config sendemail.smtpserver 192.168.16.12
That's it for windows!.