Using Git, part 1
Git is a Distributed Version Control System (DVCS) (as Mercurial about which I wrote earlier). As well as Mercurial, Git mirrors your repository. In other words every time when you check out code you make a full backup of a repository.
Almost all hosting web sites provide you with the possibility to use Git. One of the most widely used Git hosting web sites is github. Check it out.
There is one very good publicly available book about Git: Pro Git. This is my reference book and I advise everybody to read it.
Let’s start – Configuration
Git comes with a git config tool. It helps you to set and get configuration parameters and control all aspects of Git. All configuration parameters can be found at three places:
/etc/gitconfig– contains global parameters for all users of your system. Gets and sets parameters if you pass--systemoption togit config.~/.gitconfig– contains parameters specific to current user. Works if you pass--globaloption togit config.git/config– contains parameters specifically for the git repository you are currently using. Don’t need to pass any parameters when usinggit config. Works by default.
All parameter values overwrite values on other levels, i.e. if some value for some parameter is specified in /etc/gitconfig file and another value for the same parameter is specified in ~/.gitconfig file, the value from the last file will be used. The same works for values in ~/.gitconfig and .git/config files. In that case the value from .git/config file will be used.
First things which you need to do is to define your identity:
$ git config --global user.name "Dmitry Churbanov" $ git config --global user.email dmitry.churbanov@someemail.com
this code will assign git user name and email address for current user of a system. If you want to set up user name and email just for current project which you are working on right now you need to run the following code:
$ git config user.name "Dmitry Churbanov" $ git config user.email dmitry.churbanov@someemail.com
If you want to check the value of any parameter you need to run the next code:
git config parameter-name
where “parameter-name” is the name of a parameter. For example, to see the name of a user or email you can check it in the following way:
$ git config user.name Dmitry Churbanov $ git config user.email dmitry.churbanov@someemail.com
Initialise the repository
To initialise git repository go to the directory where you want the repository to be placed and run:
$ git init
After this command is executed you will see .git subdirectory which contains all the repository files. This is the only repository where Git stores its configuration files and the history of all changes.
Add files
After you initialised the repository it does not contain any files. To tell Git to start tracking a file you first need to create it in the directory or to copy it to the directory and run:
$ git add filename
where filename is a name of a file to start to track. You can also run:
$ git add .
and Git will start to track all the files in the current directory and all the subdirectories which it doesn’t track at this moment. You can also use some kind of regular expression. If you want to add only .java files you need to run the following command:
$ git add *.java
Committing files and file states
After you added some files you told Git that you want to start to keep track these files in the future, i.e. you want to include these files in your next commit command. This state of files in the Git repository is called: staged.
After you added all the files and are ready to make commit you need to run:
$ git commit -m "commit message"
where “commit message” is a message which goes with this commit and should describe the purpose of the commit. For example, when you fixed a bug and commit you changes you most probably will include some information which describes what bug it is or its number or something else.
If you just run git commit (or git commit -a) command you then will be asked to write “commit message”. It is recommended to run commit command without -m flag if your message is longer than one line. And in most cases it will and should be longer.
After you made commit, all files which were committed got a “committed” state. If file was committed and them modified, it got a “modified” state. So, there are three states which tracked files can have:
- staged – when you told to include the file in the next commit (run
addcommand) - commit – when file was committed and was not changed after this
- modified – when file was committed and was changed after this
I wrote “tracked” earlier to point out that these are the files which Git is tracking. If file is not tracked by Git, it is called untracked. Untracked files are all files which are not tracked.
Status of the files
To check status of all files and to find their states, you need to run:
$ git status # On branch master # Your branch is ahead of 'origin/master' by 1 commit. # nothing to commit (working directory clean)
You can see something like you see above. It means that I made commit and didn’t push my changes (push command is discussed later). Or you can see:
$ git status # On branch master nothing to commit (working directory clean)
which means that your local (working) copy is in the same state as a remote repository.
Ignoring files
Sometimes you don’t want to include some files in the repository but you still want these files to be present in the folder. This can be some of your personal files, logs or anything else. You can say Git to ignore these files. To do so you need to create .gitignore file and write file names or file name patters there.
For example, if you want to say Git to ignore all txt files you need to have the following in the .gitignore file:
*.txt
If some file matches the regular expression which is in the ignore file but you want to tell Git to keep track that particular file, you can do it in the next way:
#Text files should be ignored (this is a comment, which is ignored in the .gitignore file) *.txt !important.txt
In such case important.txt file will be tracked by Git, even if all .txt files are ignored.
Diff command
When changes are made and git status command is executed, you can be wondered, what exactly is changed in the files which are modified. You can use git diff command. This command shows you what changes have been made. The following command:
git diff [path]
will show you what changes have been made in a specific file, if instead of path you write path to the file; or what changes have been made in all files of a specific folder if instead of path you write path to that folder. To see all options of diff command run git diff --help.
One very important note:
If you have already added a file, say file1.txt (run git add file1.txt). And after that you made some changes in that file, then if you run git diff file1.txt you will not see the latest changes. It is because after you run git add file1.txt, Git cached the context of file1.txt file at that moment. To see cached changes you need to run :
git diff --cached [path]
And if you run git commit your latest changes will also not be committed. To add the latest changes you need to run git add after each modification. But there is one trick. You can use -a option with the git commit command to commit all files which were previously tracked and were modified. It allows you not to run git add command each time you made any change, but to run it only once.
Removing files
If you want to remove file, i.e. to remove it from the repository (and remove it from your hard drive) you need to run:
$ git rm [path]
This command tells Git to remove file. After the next commit command this file will be removed from the repository.
But if you want to leave file on your hard drive and only stop to keep tracking that file, you need to run:
$ git rm --cached readme.txt
Moving files
To move file or rename it you need to run the following command:
$ git mv [file-name] [new-file-name]
But it is very interesting that Git doesn’t actually track if you moved your file or not. It is somehow explicitly figures out that you renamed the file after you run something like this:
$ mv file.txt new_file.txt $ git rm file.txt $ git add new_file.txt
Try to run git status and see what happens. It is very convenient because you can rename or move file using whatever tool or program you want and them just remove and add it using Git rm and add commands.
Help
If you need help with any command use git help [command] or git [command] --help. For example, to get help with commit command you need to run:
$ git help commit
This is it for the first post in the “Using Git” series. I think you have read pretty enough material to start using Git.