Manage Your Source Code Locally With Git
What is Git? And why should we use it? Git is a source code management software, that able to track the revisions in our source code. We usually call that kind of software as the revision control. Most people use revision control like Git or Subversion (SVN) when they are working as a team to develop one software together. That's good because they can always get the updates from everyone in the team and see what others have modified. But, the revision control is not only needed when we are working in team. It's also important to use it even though we are working alone. Why? Because we can track changes history in our source code, we can revert to the previous code if the new one is not working as expected, and we can also view and generate the change log because every modifications we made can be recorded.
Should we setting up a repository server to use the revision control? No, because Git can be used locally. Before we start working on the project, we can setup a local Git repository by using the following command.
cd project_folder
git init
After that, we can start writing the first progress of our project. When we finish, we can commit the changes to the local repository using the following command. First, view the repository status
git status
We will see the list of untracked files which means the list of new files that will be added to the repository.
Then, add those files before we commit. In this example, we use " * " to add all files. Of course we can add only specific files only to be committed by specifying more specific path.
git add *
Before we commit, we must identify our self first by specifying the name and e-mail address using the following commands.
git config user.name "Your Name" git config user.email "your@email.com"
Finally, commit the files and also write the commit message.
git commit -m "First commit for the project."
After that, we can track the changes history along with its commit reason and the affected files.
git log --name-status
We can also track the changes inside the file. Just modify one file or more than type the following command to show the difference. After that, add and commit the file.
git diff
What if we made mistake and wanted to revert the file to the previous version before commit? First we need to view the commit log.
git log
By reading the commit log, we can also get the commit ID which appear as MD5 hash. Copy the hash and use it with the following command. By assuming the file that I want to revert is src/Controller/FirstController.php, the command will be like this.
git checkout [commit_id] src/Controller/FirstController.php
Then commit the file once again to fix it.
git commit -m "Revert the src/Controller/FirstController.php to previous commit."
We have learned some basic Git commands. There are many other powerful commands in Git which can't be explored all in one short article. But, with the ones explained here, at least we can manage our source code better.
NOTE:
When the output of your git command, such as git log and git diff, contains the ESC[ characters, you must run the following command to get rid of them.
git config --global core.pager "less -r"
The following link in Stackoverflow may give more explanation.
Add new comment