Git is a Distributed Version Control System (DVCS) (as Mercurial about which I wrote earlier). As well as Mercurial, Git mirrors your repository: every time when you check out code you make a full copy of a repository. Almost all hosting web sites provide you with a possibility to use Git. One of the most widely used Git hosting web sites is github. There is one very good book about Git: Pro Git. You can read it online. 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
--system optionto git config.
~/.gitconfig– contains parameters specific to current user. Works if you pass
--globaloption to git config.
.git/config– contains parameters specifically for the git repository you are currently using. Don’t need to pass any parameters when using git config. Works by default.
All parameter values overwrite values on other levels, i.e. if some value for some parameter is specified in
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
.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:
that 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:
If you want to check the value of any parameter you need to run the next code:
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
1 2 3 4
To initialise git repository go to the directory where you want the repository to be placed and run:
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.
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:
filename is a name of a file to start to track. You can also run:
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:
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:
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 add command)
- 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:
1 2 3 4 5
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:
1 2 3
which means that your local (working) copy is in the same state as a remote repository.
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
.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
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:
1 2 3
In such case
important.txt file will be tracked by Git, even if all .txt files are ignored.
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:
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
git diff --help.
One very important note:
If you have already added a file, say
file1.txt, by running
git add file1.txt command. 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:
And if you run
git commit your latest changes will also not be committed. To add the latest changes you need to run
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 to skip
git add every time you make any change and run it only once.
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:
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:
To move file or rename it you need to run the following command:
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:
1 2 3
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
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:
This is it for the first post in the “Using Git” series. I think you have read pretty enough material to start using Git.