With version control, you can manage the changes that are made in the computer files. When you add this to a programming code, you can track all the changes you have made to the code from the time you initialized it. There are some very popular version control systems on the market, such as Subversion (SVN), Mercurial SCM, CVS, and Git SCM.
In this recipe, we will use Git SCM to add version control to our previously created Magento code. We will initialize an empty repository. After that, we will do several commits to add all the Magento code to the repository.
Go to your command line, and install Git on your local server by running the following command in the terminal:
sudo apt-get install git
The following steps show you how you can add the Magento code to the version control system Git:
We have to initialize the Git repository. To do this, navigate to the source folder and enter the following command:
git init
Run the following command and you will see that all the files are marked as untracked:
git status
In the next steps, we will add the other folders to the repository.
Add the
app/
folder to the repository by running the following command:git add app/
This will add all the files in the
app/
directory. When you run thegit status
command, you will see a list of all the files.Run the following command to remove the
local.xml
file from the repository but not from the drive:git rm --cached app/etc/local.xml
Run the following command to create the first commit:
git commit –m "add app folder"
Run the following command to track the file's status:
git status
Create a
.gitignore
file in the root, and add the following content in it:app/etc/local.xml
Run the
git status
command again, and you will see thatlocal.xml
is ignored.Commit the
.gitignore
file by running the following command:git commit .gitignore –m "add gitignore file"
Add the other application files and folders to the repository by running the command
git add
<folder or filename
>. Run this command for the following folders:downloader
(for installing plugins)errors
(configuration about error handling)js
(the JS library folder)lib
(contains PHP libraries)pkginfo
(information about upgrades)shell/
(contains PHP CLI scripts)skin/
(the CSS and images folder)api.php
cron.php
cron.sh
favicon.ico
get.php
index.php
index.php.sample
install.php
LICENCE_AFL.txt
LICENCE.html
LICENCE.txt
mage
php.ini.sample
RELEASE_NOTES.txt
git commit –m "add additional files"
Ignore the other nonstatic Magento files and folders. Add the following content in the
.gitignore
file:/app/etc/local.xml /errors/local.xml /media/css/ /media/dhl /media/downloadable /media/import/ /media/js/ /media/catalog/ /media/customer /media/upload/ /media/wysiwyg/ /media/captcha/ /media/tmp/ /media/xmlconnect /var/import/ /var/export/ /var/cache/ /var/log/ /var/session/ /var/locks/ /var/package /var/report/ /var/resource_config.json sitemap.xml
Add the
media/
andvar/
folders to the repository with the following command:git add media/.htaccess git add var/.htaccess
We only need the
.htaccess
files, the other files are ignored because they are dynamic.
When working with a version control system, you have to keep in mind that another person who clones the project can set up the environment with a database and the code in the Git. That environment has to be the same as the one you have committed.
It is very important that every Magento core file and your customized files are in the Git repository. You don't have to add configuration files, such as app/etc/local.xml
and errors/local.xml
, in version control. When running your code on another server, the settings in the configuration files are mostly different from the settings on the server.
Dynamically generated files such as cache files and user images are stored in the media
and var
folder, so we don't need the content of these folders.
The only important file in these folders is the .htaccess
file, which has the configuration to restrict the var
folder and the media
folder.