Pages

Friday, July 20, 2012

Can git help me?

Sometimes I work on creating my own project management system. More than once I have been thinking about making it into a github or sourceforge project hoping that soemone will find it interesting enough to join in on developing.

I still think it is too much of a specialized project and because of this I have decided to continue the work myself so I can streamline it the way I want it for our company.

This means I need to try new features or another approach for a certain problem. And sometimes it works as expected but sometimes I have been reverting to my previously backed up file. (I.e. copying from another folder.) I have used bazaar in the past but decided I would take a look at git since I have heard so much good thingds about it.

Git is a way of keeping track or your script-files (or any other file) changes. This makes it easier to revert to a previous version of the file if you need to. But of course you might want to commit the changes that is not working before doing this. One day, or night, you might come up with the solution for the problem you got stuck with and abandoned a week or two ago.

So here is a good starting point to get you started with the simple way of using git.

Initial repository

Either you already have files you are working on or you are going to start a new project. The first step is to cd into the folder where your files are (or will be) and make a git repository. You do this by running the commands:

cd /home/linproject/git/website
git init


Initial commit

If you already had some files in the repository folder (~/git/website) in this example it is now time to add your files into the repository.
This is done with these commands (note the punctuation after git add):

git add .
git commit -a

Remember to insert a reason for comitting.

So this is where the fun starts. Because now we have a starting point for branching out. (I always thought it sounded too problematic so I avoided this for a long time. I was the only developer anyways.)

We now have a master branch that is active. And we have commited our files to this branch.  (Run the command: git branch -a and you will get a list of all your branches and with an asterisk in front of the active one.)
But I was thinking about adding a module for sending out automated email reminders to myself and our employees. So I will create a branch called email.

Remember that we are still in the repository folder when we do this.

 

Making a new branch

I create the new branch with the following command:

git branch email


After that I have to checkout my newly created branch. When I checkout the branch; the working folder (/home/linproject/git/website) will be replaced with the content in the branch called email. The command to checkout the branch is simply:

git checkout email


Working on the new branch

Running the command git branch will now show an asterisk in front of the email branch name so we know this is the active branch.
Now we'll add a php-file to our project and commit this into our website repository.
We can use gedit, bluefish or whatever editor we wish to create the file email.php and save it in the working folder.

Now we are happy with what we did inside the file email.php. And we want to be able to get back to this moment later on. So we commit the changes to the branch email by adding new files and comitting everything running the following commands (note the punctuation after git add):

git add .
git commit -a


If you run the command: git diff email..master you will see all the changes between these to branches listed on your screen. (Hit q to exit the viewer.)

Let's summarize a little before we go any further.
  • We've created an initial repository (git init) and added our files into this repository (git add .) and saved/committed the changes (git commit -a).
  • Then we created a new branch (git branch email) and made sure we were working on it (git checkout email).
  • We added an email module/file into our folder and added it to our email branch (git add . ) and we committed these changes to our email branch (git commit -a).

Merging branches

If we run ls -l to see what files we have in our website folder we will see we have added email.php into the directory.

We can continue our work with the email branch any time we wish without breaking the master branch. To prove it we switch to the master branch and take a look at our working directory:

git checkout master
ls -l

This will show that email.php is missing from the directory. This is because the file email.php belongs to the email branch, and we checked out the master branch.
If we suppose that we are certain our email.php script is working as we want it, we would want to have this module as a part of our master branch. This is what's called merging.
We make sure we are in the master branch before we do this by running git branch and check to see if the asterisk is in front of master branch.

When we are certain our master branch is the active branch we issue the following command to merge the changes made in the email branch into the master branch:

git merge email


If there are no conflicts the two branches should now be identical. In case there are conflicts you'll need to manually decide what is the correct changes.

This article have been a very short primer for using git. Hopefully it is enough to get you started in using a git repository on your local machine, and if you learn the simple tricks provided here you'll find git to be invaluable while you do your development.

More about git can be found on their website at git-scm.org.
Also check out the documentation on their site. 


No comments:

Post a Comment