GIT Part 2 - The
technical parts
In my previous
post I talked about the setup I had chosen with regards to GIT. In this post I
talk about actually putting it into action.
Installing
GIT on Centos
Below are the
steps I took to install git on Centos: I chose to do the installation from the
graphical interface. I normally use the terminal but I was writing a guide for
my colleagues for when/if they had to follow my installation steps, I took this
blog from there. So, to add GIT onto the box:
·
Find 'Add/remove Software' from the menus.
·
Search
for ‘Git’ and download the latest versions of:
o
Git
(core and tools)
o
Git
Daemon
o
Git
Gui
o
Git
web (if you want it)
·
It
may require some dependencies.
·
Once
installed the GIT repositories are under
o
var/lib/git
Or, yum install git if you have the
dependencies and repositories added and want to do it from the terminal.
Live: A copy
of http://joemaller.com/990/a-web-focused-git-workflow/
So on Live we
want to create a git repo that sits over the live code which is our PRIME.
The live code is in /var/www/html.
$ cd /var/www/html
$ git init
$ git add .
$ git commit
-m"initial import of pre-existing web files"
This creates an
initial repository over /var/www/html
and adds all of the files in it to the staging area:
Explanation Note: There are
the files in the folder, the files that have been staged/in the index, and the
files that are in the repo.
- The files that are in the index are those that are in the repo, plus those from the folder that have been staged.
- Staging is telling the repo what files want to be put into the repo in the next commit.
- Committing is adding the files into the repo.
Once the files
are in the index (staging area), it is all committed with a message.
Next we want to
create HUB.
$ cd /;
$ mkdir git; (or
whatever you want to call it, e.g. hub.git)
$ cd git
$ git --bare init
This creates a
bare repo in HUB (A repo with no code checked out or a repo with no code
viewable when browsing to the folder)
Now, from inside
PRIME, we want to add HUB as a remote repository so we can then push the
master from PRIME to HUB
$ cd var/www/html
$ git remote add
hub /git (or whatever you called it from above)
$ git remote show
hub
(will show your hub)
$ git push hub
master
The
Hooks
Hooks are
rules/scripts that can be set to automatically run when something happens in a
GIT repository. We want to make sure that the two repositories are kept in
sync.
The HUB hook
- Post Update
In the .git
folder (the hidden folder) within /git
(or whatever you called it), there is a hooks folder. There are lots of sample
hooks in there, we want to rename 'post-update.sample' to 'post-update'
and then make sure its contents are:
#!/bin/sh
echo
echo “*** Pulling
changes into Prime [Hub’s post-update hook]”
echo
cd $/var/www/html
|| exit
unset GIT_DIR
git pull hub
master
exec
git-update-server-
The script swaps
to PRIME and then does a pull from HUB into PRIME with the master
branch. This will then update the code in the working directory.
The PRIME
hook - Post Commit.
Imagine that
someone accidentally changes code in PRIME (ftp or direct access). The code for
the website will change, but the git repos will be out of date. Assuming that
the code change is checked in then at least the PRIME repo is up to date. But
what about HUB and therefore (because this is the one we connect to), DEV?
In /var/www/html there will be a hidden
folder called .git which will have the hooks folder in it. In this folder there
probably isn’t one of the files that we need -> 'post-commit', so
create it. (I just copied one of the other files and renamed it). The file
should have the content of:
#!/bin/sh
echo
echo "**** pushing changes to Hub [Prime's
post-commit hook]"
echo
git push hub
This script will
simply push the contents of the repo to HUB. (This will, in turn, make PRIME
pull HUB again, but oh well).
Testing
I tested this by
changing a file in PRIME. (I changed the background colour of a part of a test
page on moodle and transferred the file up to PRIME).
In the terminal
I ran:
cd $/var/www/html
git status
git add [file
name]
git commit -m”Test
Commit”
This then outputted
the output from the two hook scripts.
The ignore
file
As with many
systems there are certain files that we don’t want to track and have in the
repository. For Moodle there is at least: config.php. We also had a few others
including gmail folders (blocks/gdata, blocks/gmail, auth/gsaml). Dev has its
own config file (different addresses) and we didn’t want the accidental sending
of emails.
The ignore file
is a file that will ignore all of the files specified from being tracked if
they are changed or from being added to the GIT repo.
In the .git
folder add a .gitignore file and, on each line, put the files and folders that
you want to ignore. Ours is: (I added this file in both PRIME and HUB.)
config.php
.htaccess
/blocks/gdata
/blocks/gmail
/auth/gsaml
We still have a
slight issue though. We committed and pushed the above files to the repos and
we need to get rid of them. To do this we need to run:
git rm --cached
config.php
git rm --cached
.htaccess
Commit and add a
message.
This will remove the files specified from the git repo itself.
This will remove the files specified from the git repo itself.
Permissions
When I was
testing all of this I was struggling to get the correct remote repo commands to
work when connecting from my DEV machine. In the end I found it to be
permissions.
I created a user
and group called 'git' and gave them the correct writes over the git repos:
chgrp –R git
[location of HUB]
chmod –R g+swX
[location of HUB]
chgrp –R git
[location of PRIME]
chmod –R g+swX
[location of HUB]
Now we should
have a working live system.
Installing
GIT on Centos (DEV)
A ‘git’ user
must exist and be in the ‘git’ group. I removed any moodle files that I already
had making sure that I kept the dev server specific moodle config.php file. I
made sure GIT was installed following the same initial install steps from above.
In a terminal window I then ran:
cd /var/www
git clone
ssh://git@[ipadress]:[port]/[folder] html
This changes to the www folder and then attempts to clone
the [folder] of our HUB from the ipaddress of the live server and create a git
repo in html. It will also check master branch out. It will probably ask for a
git password.
Note: I am happy to leave authentication between live and
dev to always ask for the password and not store it in any keys. For windows to
DEV I did keys.
I added the exact same ignore file to DEV as LIVE and
copied in the config.php file from before.
Next I created some new branches on DEV:
cd/var/www/html
git branch
newFeatures
git branch
branchNameWhatever
Now I had a working copy of DEV.
Installing
GIT on Windows
The following is
an amalgamation of:
I use GIT gui,
tortoisegit and putty configuration for my setup.
- Download and install PUTTY (http://www.putty.org/) PUTTY will allow you to have a linux command line inside the windows terminal. But will also act as the SSH connection between the Linux GIT server and the desktop GIT.
- I put all three of my PUTTY downloads into my in c:\tools\bin (which is also in the PATH of the computer (right click on computer, properties, advanced system settings, environmental variables and then edit the PATH. A semi colon is used to separate the paths))
- Download Pageant. (http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html) This will be used later to store the pubic/private keys for GIT so that every time you do a push/pull command over SSH you don’t need to put in the public or private key for each file
- Download Plink. (http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html) This will be used later as an interface to the PUTTY back end.
- Download PuttyGen. (http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html) This will create the public and private keys we will need later.
- Download and install GIT for windows. Called msysgit (http://code.google.com/p/msysgit/downloads/list). Download the .exe, run it and it will download and install GIT for windows. I kept everything default except the following options
a.
Default
location is fine to install to
b.
‘Select
Components’ -> Default is ok, except, make sure that ‘Windows Explorer
Integration’ is ticked and all its options.
c.
‘Adjusting
your PATH environment’ -> Run Git
from the Windows Command Prompt
d.
‘Choosing
the SSH executable’-> Use (Tortoise) Plink. Now search for the plink.exe
(I put all three of my downloads into my in
e.
‘Configuring
the line ending conversions’-> Checkout as-is, commit as Unix Style
- Download and install TortoiseGIT (http://code.google.com/p/tortoisegit/downloads/detail?name=TortoiseGit-1.7.6.0-32bit.msi&can=2&q=) this is a client gui based system to allow you to work with GIT easier.
a.
‘Choose
SSH Client’ -> TortoisePlink
b.
‘Location’->
Default location is ok
Ok, so now we
have GIT installed on Windows we need to link it to DEV and clone.
Step 1
Login to DEV and
create a new user and new password (for each developer) and put this user in
the ‘git’ group. Create a ssh folder and an ‘authorized_keys’ file:
Create a .ssh
folder in the home folder
e.g. /home/username/.ssh
Create an ‘authorized_keys’
file in this folder
e.g. /home/username/.ssh/authorized_keys
The American spelling
is required and there is no file extension.
Step 2
Now we need to add the private and public keys so we can
communicate with the remote repo.
Run the
puttygen.exe program that was downloaded earlier.
Click on
‘generate’
Wait for it to
generate the private key and move your mouse around over the bar
Click save private
key (I saved without a pass phrase and as private.ppk)
Click save public
key (I saved as public.ppk)
Now we need to add the private key to pageant on our
machine:
Run pageant
Double click on
the pageant icon in the tray
Add the private
key
Now we add the public key to the DEV server:
In your
authorized_keys file paste your public key. (I copied it to a text file and
FTP’s this up to the server so I could access it, alternatively use putty to
ssh in.)
Step 3
I next had to make sure that the ssh folder and file had
the correct permissions. So I ran:
$ chmod 700 ~/.ssh
$ chmod 600
~/.ssh/authorized_keys
Step 4
Now we can clone the remote repo.
Get rid of any Moodle installations from the windows box
(remember to save the config.php file)
In the folder level above your original ‘Moodle’ folder
(for me it was xampp/httpd/moodle) right click on the folder and select ‘Git
Bash Here’. (I clicked on httpd folder inside my xampp folder)
Once the command comes up type:
git clone ssh://[yourUsernameHere]@[ipaddressDEV]:22/git
moodle
Assuming your public and private keys are set up
correctly then you will get a clone of everything that is in git. It will add
this to a new folder called Moodle
and it will ‘checkout’ all of the current working branch.
Step 5
The next step is to add your configuration details.
Edit -> options
Then put in your username and your email address in both
halves. (The left is this repo, the right is all repos.)
Step 6
We now need to configure our local copy of the branches
so we can use them on our local machines. This includes:
·
Swapping the config file out
·
Deleting the htaccess file
·
‘Ignoring’ these files on all future commits.
If the config file and .htaccess from ‘dev’ has come down
then remove them both and put back the config file from your original moodle
(or create one).
Assuming that tortoise git is installed. Right click on
the following files and add to the ignore list:
·
config.php
·
.gitignore (will be created as you ignore the
file above)
If the file is already in the repo then we need to delete
it. Run the git bash (over the moodle folder) and type the following commands:
·
Git rm --chached config.php
·
Git rm --chached .htaccess
This should remove them from the commit and will also not
have them show up when comparing the local copy to your last snapshot.
Removing Web Access to the GIT repository
To remove web access from GIT (as its at the root of the accesible website) we need to modify the .htaccess file on LIVE in PRIME. Add:
# deny access to the top-level git repository:
RewriteEngine On
RewriteRule \.git - [F,L]
RewriteEngine On
RewriteRule \.git - [F,L]
Done
That should now be all set up and in the next post I talk
about how we use our git setup. This includes branching, merging and checking
in.
No comments:
Post a Comment