Git is a very nice and useful distributed version control system. It certainly became a component for the swiss army knife of software engineers. Git is created by the father of Linux, Linus Torvalds. The main advantage of git over SVN and other traditional VCS’s(version control systems) is its distributed nature and performance(git is really fast and written mostly in C). Being distributed is important because all the users have the local copy and if one local copy is somehow deleted or damaged. Also it brings performance benefits. Reduces the load on a machine. You don’t have to do all your commits to the central repository. Others can recover. In this article I’m going to describe you about how to create a remote Git repository to connect with SSH and some basics related to git.
How to create Git Repository
Remote Repository
There are some initial steps to do before creating our repo, if you haven’t done yet. Of course at first you should install git. You can install with following commands:
For Ubuntu and Debian forks:
sudo apt-get install git
For FreeBSD and PCBSD(login with root user):
cd /usr/ports/devel/git && make install && make clean && cd -
After you’ve completed those steps you should now create a user. Let’s say we created a user called git:
adduser git
Now you have created a user and installed git. We can successfully create our repository now. To create our repo now login with the user you have created:
su git
Create a file called repos on your user’s home directory:
mkdir repos && cd repos
Now here comes the crucial parts. Create a directory called which has a relevant name for your project. At the moment we’re just testing so let’s name it test.git(general convention when creating a repo for git is the repo name ends with .git):
mkdir test.git && cd test.git
We’ll initialize our repository with some templates and skeletons for a git repository:
git --bare init
That’s all for remote git server.
Local Git Repository
You should also install git to your local machine too. Then you can do some configurations related with you local computer. For instance:To set you name:
git config --global user.name "Your Name Comes Here"
To set your email:
git config --global user.email you@yourdomain.example.com
To see all configuration settings:
git config --list
You should go to your project directory and initialize git:
cd ~user/Codes/test
git init
Create an example file called test:
touch TEST
Add files to your local git repository:
git add .
Commit to the local repository:
git commit -m "first commit"
Add the remote server’s address with ssh access:
git remote add origin ssh://git@server_address/path_to_repository/test.git
Now push the changes to the server:
git push origin master
The git push commands push the changes to the destination repository you’ve specified. In above we’ve push the changes at master(our repository that we are working on) to origin (remote repository). You should at first do changes in your local repository and after you have completed your changes, push those changes to the remote repository. Try to do your updates commits to local repository atomic. Commit all minor the changes you’ve done in the local repository. Congratulations, you have successfully created your local and remote repository.
Some Git Basics:
Ok we’ve created our repos now let’s check some basic commands.
git add : This command adds the current content of new or modified files to the index, thus staging that content for inclusion in the next commit. We can specifically give file names that’ll be added or with -A option we can add all commands. With -v option this command runs in verbose mode.
Example:
git add test.c
OR:
git add -v .
We should add all our newly created files with this command at first.
git commit:
Stores the current contents of the index in a new commit along with a log message from the user describing the changes. With -m option you can enter some additional comments and logs related to the update. -a option commits all changes.
Example:
git add hw.cpp
git commit hw.cpp -m "Patch added for the 64 bit CPUs."
OR:
git add .
git commit -a -v
git status: This command displays paths that have differences between the index file and the current HEAD commit, paths that have differences between the working tree and the index file, and paths in the working tree that are not tracked by git (and are not ignored by gitignore(5)). All the output lines are prefixed with #. Basically this command gives general information about changes done after the last commit.
Example:
git status
Sample Output:
# On branch master
# Changes to be committed:
# (use “git reset HEAD …” to unstage)
#
# modified: file1
# modified: file2
# modified: file3
#
git pull: Fetchs from and merge with another repository or a local branch and updates the local branch. –log option generates log messages. This command has a similar functionality of “svn update”.
Command format:
git pull
git push: Updates the remote repository by sending the necessary objects to the remote repository.
Command format:
git push [--all | --mirror] [--dry-run] [--tags] [--receive-pack=]
[--repo=] [-f | --force] [-v | --verbose]
[ ...]
Example:
git push origin master
git fetch: Download objects and refs from another repository. This command doesn’t have merge functionality like pull.
Command format:
git fetch
..
Example:
git fetch origin [branchname]
git diff: Shows changes between commits, commit and working tree, etc.
Command format:
git diff [] {0,2} [--] [
...]
Common usages:
You will commonly use git diff for figuring out differences between your last commit, your index, and your current working directory. A common use is to simply run
git diff
which will show you changes in the working directory that are not yet staged for the next commit. If you want to see what is staged for the next commit, you can run
git diff --cached
which will show you the difference between the index and your last commit; what you would be committing if you run "git commit" without the "-a" option. Lastly, you can run
git diff HEAD
which shows changes in the working directory since your last commit; what you would be committing if you run "git commit -a".Detailed info related to "git diff" command
git log : This command shows commit logs.
Example Usages:
At any point you can view the history of your changes using
git log
If you also want to see complete diffs at each step, use
git log -p
Often the overview of the change is useful to get a feel of each step
git log --stat --summary
Stashing
Let's say that you are working in a git tree and have previously made some local commits. Then you started working on other code and modified several other files. However, you then decide to push the previously made commits to the central repository (e.g. those commits fix a critical bug). However, git will not let you push your commits because your tree is not clean.
In the absence of originally using a branch to do the newer hacking, you can easily create a temporary branch, move the work-in-progress to the temporary branch, push your commits to the server, and then re-apply your work-in-progress to the current branch. Git has a wonderful built-in command that will do all this work for you: stash. To stash all work-in-progress on the current tree, use:
git stash
When you need to pull the changes back, use:
git stash apply
For more detailed tutorial about git run the following command:
man gittutorial
For a description related to a specific git command, run this command:
man git-action_name
For example:
man git-fetch

by Kicker
02 Nov 2009 at 23:24
I have already seen it somethere…
Thanks
Kicker
by Mackeran
04 Nov 2009 at 17:28
I liked it. So much useful material. I read with great interest.
by John
16 Nov 2009 at 13:04
Thank you! You often write very interesting articles. You improved my mood.
by Peter
19 Nov 2009 at 00:48
I really like your blog and i respect your work. I’ll be a frequent visitor.
by Bunker
28 Nov 2009 at 10:09
I really like your blog and i respect your work. I’ll be a frequent visitor.
by stirka
24 Apr 2010 at 05:42
I suggest you to visit a site, with a large quantity of articles on a theme interesting you.
In my opinion, it is a false way.
It is not meaningful.
I apologise, but, in my opinion, you are not right. Let’s discuss.
It was specially registered at a forum to tell to you thanks for the information, can, I too can help you something?
by Holly Keate
09 Dec 2010 at 04:25
insiteful entry.