Git is a version control system which means that it allows you to keep track of your code or any kind of text throughout the development.
Meaning, you can rewind back and forward to any stage of the code development. Without going into much detail, this is done mainly via few basic commands: clone
,
add
,commit
, push
and pull
.
There are obviously more commands available, but these are the basic commands which we are going to discuss in this short
git how-to. Before you begin, make sure that git
command is available on your system. If not, on Ubuntu or Debian Linux you will install it as root user by a following linux command:
# apt-get install git
Imagine git
as a centralized repository of your code/text which may and very often is shared among many other developers. Let’s get started by cloning my existing git repository:
$ git clone https://linuxconfig@bitbucket.org/linuxconfig/linuxconfig.org.git Cloning into 'linuxconfig.org'... Password for 'https://linuxconfig@bitbucket.org': warning: You appear to have cloned an empty repository. Checking connectivity... done.
I have provided my password and the above git clone
command downloaded all files available in that repository. At the moment the repository is empty:
$ cd linuxconfig.org/ $ ls
Next, I’m going to write some simple text and store it into a text file called mycode.txt
:
$ echo HELLO WORLD > mycode.txt $ cat mycode.txt HELLO WORLD
Before the mycode.txt
becomes a part of our repository it needs to be added to repository. This is done by use of add
command:
$ git add *
The asterisk sign means add all files ( I’m just too lazy to be explicit ). Now we create a first milestone in our development, hence we commit the code using the commit
command:
$ git commit -m "My genius first line of code" [master (root-commit) 01b23f7] My genius first line of code 1 file changed, 1 insertion(+) create mode 100644 mycode.txt
At this stage the code is part of our repository and we can go back to this milestone at any time using the commit number eg. 01b23f7
in the combination with a relevant command not to be discussed here. The -m
option is a message attached to
this commit describing what has been done. Although, our new code is committed, it still resides locally thus cannot be viewed by anybody else. From this reason we need to store our new commit to the centralized
directory using push
command:
$ git push -u origin master Password for 'https://linuxconfig@bitbucket.org': Counting objects: 3, done. Writing objects: 100% (3/3), 236 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://linuxconfig@bitbucket.org/linuxconfig/linuxconfig.org.git * [new branch] master -> master Branch master set up to track remote branch master from origin.
All done. Lastly, we will discuss pull
command which is used to fetch code from a centralized repository. Most of the time you will not be the only developer working on any project. In
the meantime that you are having a break, some of your colleagues may have push new code into a repository, thus you need to keep it synced. This is done by pull
command.
$ git pull Password for 'https://linuxconfig@bitbucket.org': Already up-to-date.
No change, so I can keep working. Always try to perform git pull
before you start working. This way you have a better chance to avoid unnecessary need to code merge. From now on, you will be
repeating the following lines all over:
$ git pull HERE YOU DO YOUR WORK $ git add * $ git commit -m "some meaning description of the genius things you have done" $ git push -u origin master