Saturday, May 25, 2013

What is GIT? How GIT works in Linux?

Hello everybody,
Today, I would like to talk about Revision Control. What is Revision Control and how we can configure it in Linux. Well, when you want to keep track of your changes in configuration files, source code files (like your programming codes), or any files, you need to use Revision Control. So, if your changes in the files cause any problem or for whatever reason you want to roll back, you can easily do it with Revision Control. Also, you have a history of all changes in your files. There are so many Revision Control tools, but I would like to explain about GIT. I discuss some common ways of managing changes at level of individual files by GIT.

Although you are able to create a central repository on the network, I am going to create a local repository by GIT since it's much faster and easier for our purpose here. In order to intsall GIT, run the following command:
yum install git

Before you start using GIT, set your name and email address because committers have to commit their changes as root.Names and email addresses apply to log entries even though you are running as root.

git config --global user.name "Khosro Taraghi"
git config --global user.email "root@localhost.localdomain"



                                                                    Figure 1

Now, let's create a directory and some subdirectories/files, then create a repository to cover that directory:

mkdir /report
mkdir /report/dir1
mkdir /report/dir2
touch /report/file1.txt
touch /report/file2.txt
touch /report/file3.txt
touch /report/dir1/file4.txt
touch /report/dir2/file5.txt


Run the following commands to create the repository's infrastructure in the /report/.git

cd /report
git init



                                                                          Figure 2

The following command puts everything under /report directory to Git's staging area or list. It means that it's the list of files needed to commit:

git add .

The following command commits those files. The -m flag is used to include the log message:

git commit -m "My first Commit"

                                                                           Figure 3

Now, let's test this out. I am going to make a change in file1.txt and file4.txt. Then, check them in to the repository:

echo "This is a test1" > file1.txt
echo "This is a test2" > dir1/file4.txt
git commit file1.txt -m "My first change in file1.txt"


                                                                         Figure 4

Note:
when you name or specify the name of file in the commit command (like above), the reset of changed files are not committed to repository. So, in this case, file4.txt was not committed. When multiple files involve to commit, you can use the following command: (let's change one more file, then commit all of them)

echo "This is a test3" > file2.txt
git commit -a

If you don't use the -m flag, Git will open the editor to add your log message(figure 6). If you ignore to add a message, commit will be aborted.

                                                                           Figure 5


                                                                            Figure 6

There are 2 drawbacks using "git commit -a" command:
1. "git commit -a" command doesn't pick up the new files. So, if you add a new file under /report directory, "git commit -a" doesn't add the new file to the repository.
2. Some files are system files, like /etc/mtab, and they change by system. Therefore, if you use "git commit -a" command, you may commit other unwanted files to repository which is not good.

To avoid this situation, you can use the "git status" command before committing the files:

echo "This is a test4" > file3.txt
git status

                                                                           Figure 7

To see the actual changes in the file, you can use "git diff" command:

echo "This is a test4" > file1.txt
git diff file1.txt

                                                                              Figure 8

If you want that Git ignores some specific files, first you must delete the file from current repository:

git rm --cached file3.txt

NOTE: The cached option prevents Git from actually deleting the file.
Second, you must create a ".gitignore" file and add it the list files that Git needs to ignore


                                                                              Figure 9

In short, GIT is very useful tools to keep track of your changes in files, especially configuration files and it's not more painful than making manual backup copies. And that's it. Hope you enjoyed.
Khosro Taraghi