Getting around Git can be a daunting task. But using Git is essential when it comes to sharing your development with a team or a community, it allows you to keep track of the changes and follow the version of the project. Anyway if you are here you don’t want to listen anymore on the benefits on using Git, let’s get straight to the point as always with some examples.
Installation on Linux
First things first, if you haven’t done it yet, install Git on your Debian server (you are the project manager aren’t you ?).
# apt-get install git-core
Well that’s all !
Now it’s time to configure Git so pick up a username and email of your choice and launch the git config console :
$ git config --global user.name "John Doe" $ git config --global user.email "email@example.com"
Then if you type # git config –list , you will see the entries you ‘ve just added.
The config file (.gitconfig) is generated to your home directory, so it belongs to the current user who just input the commands, did you notice the $ instead of the # on the above commands ?
Ok, you are ready for the ride now, let’s use Git for a project.
Creating a new project
Select a directory for your work :
$ mkdir yourproject $ cd yourproject
Add your project files right here in this directory or create new files. For instance add the whole Zend project we talked about here : Beginning with Zend 3.0
Then initialize the GIT repository :
$ git init
This command will build a hidden directory for Git use : .git
Let’s make Git aware of the files :
Once you’ve added some files, you can issue the following line :
$ git add .
This will tell Git there is something new !
Now apply the new version for your project :
$ git commit
A file opens and you must write a message at the top to indicate the changes you’ve just made.
If this is the first time, tell it about it : First version right after installation
Then quit the program (nano, vi…).
Now each time you modify or add a file, you must update the index for instance :
$ git commit -a
The -a indicated that you’ve added some file to index in Git. When you do this, you need to tell git about the changes you made in the opened file.
Before we continue, we should review some Git features. At the moment, there is no much happening and it’s difficult to see the advantages of using it.
What is a branch : as its name implies, a branch represents a different version of a development, the more version of a repository, the more branches you will get.
$ git branch
will give you the current branch you are working on.
Chances are that will be something like : * master
The master branch represents the default branch head. The asterisk indicates the current loaded branch.
Yes there are HEADS in trees ! In fact the head of a branch can be seen as a base. The master branch is the single branch of a new initiated project.
A project can also be referenced by its versions which are defined by : TAGS
$ git tag -l
will provide you with the different versions of a project.
To create a new branch head, input the line :
$ git branch branchname
It will reference the same point of history as the current branch.
And to make the current branch the one specified by thebranch :
$ git checkout thebranch
And a useful command is to check the different branches of a remote repository :
$ git branch -r
Back to work now !
We shouldn’t use the master branch for making changes, committing and pushing. So we need to create branches. That will reveal the power of GIT.
$ git branch newbranchname
You have now your new branch created, let’s load it using the current branch details :
$ git checkout newbranchname
We assume here that you’ve achieved these steps on the server side. Git becomes really interesting when you dispatch a project among several developers on different machines.
If you type the git branch command you should get something like this :
Additional computer clients
On the central machine or the server, we can set up a public repository but for the sake of this article we will stay private, it is the basic schema for a few users on the project.
You must have access to ssh on the server in order to clone and pull the repository :
Let’s clone from the server :
Get yourself into a chosen empty directory on your local machine then trigger the command :
$ git clone ssh://firstname.lastname@example.org:22/home/username/public_html
the public_htnl directory is on the server side.
Note : if you are on the server using multiple users, you can also trigger the command, it will clone the repository into the local user directory. It will work as if you were on a client machine.
Now you can browse your local directory and see your files.
At this time, you may verify how Git works. Simply change one file (from your local client machine) then issue the command :
$ git commit -a
And check out the status of your change :
$ git status
Then push the changes from your new branch to the server :
$ git push origin newbranchname
Be careful when you push to the server, on the server side, you should be on a different branch otherwise it won’t let you push.
No worries, you will understand :
- now you’ve just pushed right ?
- check out the changes on your server
- well nothing seems to have changed !
- do the following : # git checkout newbranchname
- What do you see now ?
- YEAH ! all changes are showing !
Make sure you change back to the master branch for instance (if you only have 2) :
- # git checkout master
- now you can push again… and again…
Git you rock ! Now you get the point and you know why branches are mandatory, you will get used to them and play even more with tags and so on without worrying about the integrity of your files.
From one branch to the other
Let’s assume your control your server which is always on the master branch and all commits are getting from clients on other branches.
At any time, once you review the changes on these different branches, you need to update the master one, so here is how you should achieve this :
- from the server side, get yourself into master branch
- type : $ git merge otherbranch
- the changes will be made updating the master branch
You can use master or other branches, usually branches reflect the version of your project.
To check the difference between branches, issue the following command :
$ git diff
More about the push
What did you notice ? May be nothing, no worries, this is just a note, but an interesting one : Git remembers all the details of your server.
Check this command :
# git log
More on Git
At this point, you have the basics on setting up and using Git for your project with a few users, however to go further and avoid conflicts changes while working along with a community of developers, there are some additional steps and rules to follow if you don’t want to screw up your work. This will be the treated soon in another tutorial.
We hope you enjoyed this article and we invite you to read the documentation on Git :