I am sure you are familiar with the following:
If you want to make a change into your PHP website, you will use any FTP client for instance FileZilla, look up your required file, download it, make the changes and upload it back to the server. Once done, refresh browser to see the effective change.
If that is your workflow, don’t worry. You are not alone. Companies normally follow this practice.
However, a true professional strives for improving the way they work. Therefore, in your thirst for better development workflow, I want to add another skill to your tool-belt. It’s called version control.
According to Git; “Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later.”
Sounds good, right?
Now next thing is; What is Git?
Git is the most commonly used version control system and is quickly becoming the standard, means your local copy of code is a complete version control repository. These fully-functional local repositories make it easy to work offline or remotely. You commit your work locally, and then sync your copy of the repository with the copy on the server using remote services like GitHub or Bitbucket, which we will cover in a bit.
How To Use Git For Website Development
As mentioned earlier, Git is often used for local development which you probably should be doing as well. Therefore, I am assuming you have a local website ready to work with throughout this tutorial.
I would like to go with command line instead of Graphical User Interface tools. Why?
- Command Line Centric Tools
- Source Control Git, Subversion, Mercurial
- Ruby on Rails, NodeJS, Grails
- Build tools: Maven, Gradle, Bundler, NPM
- Online Help
- Excellent for Learning
On Windows, I prefer using Git Bash (Bourne Again Shell) as my bash shell environment. Since I am a web application developer, I like using Git for my development projects and Git Bash is a utility that is included in Git for Windows. Git for Windows is a very simple program installer which is very straight forward to step through. Git Bash is more powerful than any default command shell. Cygwin is also available however it is more complicated than Git Bash so we will focus on Git Bash.
Step 1. Installation
In order to use Git, you first have to install it on your computer. The tool is available for download on the Git – Downloads page. Simply choose the right version for your operating system and follow the instructions. In my case, It will be Git for Windows.
Once Installation is completed, to verify, just click on the icon on your desktop or right click anywhere and click on the Git Bash. A popup windows will open and write command
which tells you the version installed on your computer.
Some of the basic commands you should be familiar with;
pwd ls ls -l cd cd .. cd ../../.. cd ~ echo cat touch mv mkdir mkdir -p rmdir rm rm -rf clear exit
Note: We will cover all these basic commands in a future article.
Step 2. Convert your local website into a Git repository
- Create a local repository to store your project files. You can use an existing folder or create a new one.
- Go to your directory, right click and then click “Git Bash Here”.
- Write command
and also set identity by giving your name & email with the following command one at a time:
git config user.name “your-name” git config user.email “[email protected]”
Step 3. Create an account on Bitbucket
Create an account on Bitbucket or GitHub and then set it up as a remote repository to which you can push your local code. I prefer to use Bitbucket as it provides more features than Github in free version.
Step 4. How to create a remote repository, please visit this link.
Step 5. Configuring remote repository on local pc
Once you will create a new remote repository, get the HTTPS URL and add into your local git repository with the following command:
git remote add origin https://[email protected]/your-username/repository-name.git
To push your changes on Bitbucket repository, use the following commands one at a time:
git commit -m “Initial commit” git push -u origin master
Note: Use -u in your push command if you are pushing for the first time. And if you are pushing again, don’t use -u.
Step 6. Create a repository on live server
- Go to your root directory via PuTTY using ssh credentials. For windows we use PuTTY.
- Once you will be on the root directory, write the following commands one at a time:
git config user.name “your-name” git config user.email “[email protected]” git clone https://[email protected]/your-username/repository-name.git
Things to consider
Keeping your configuration files such as “config.php” or any other file containing passwords inside a remote repository would be unwise. For instance, in most cases, these files conflicts with server config files or if another person’s also working on your project, it will conflict with them as well. Therefore always avoid keeping these certain config files on local repository to keep them from pushing to your remote repository (Bitbucket / GitHub).
The best method would be to manually uploading such files the first time on your website.
If you require more information feel free to leave a comment. Happy Deploying!