Git - Fast Version Control System

This post is for the sake of getting rid of one of our old habits. I remember my first days of programming. While I was writing a code for a  project, after each progress I made, I used to copy the entire project, give it a new name(project1, project2, project3, etc.) and save it. This was my way to keep track of my working code. If after changing the latest version of my code, the project gets worse, I used to delete it, extract the latest running version of the project and move on modifying it.

There are better ways of controlling the versions of our projects. Git is the one I am going to write about. You can find a detailed documentation about it from here. Download and install it from http://git-scm.com/download . After installing it, it will be available to run from your command prompt window or terminal.  I will write the commands I frequently use and explain the purpose of each.

As a Linux user, I will use Unix commands to explain it.

1. Lets first create a directory that will contain our project and name it FirstGitProject.

mkdir FirstGitProject


2. Now change the current working directory to FirstGitProject.


cd  FirstGitProject


3. Now we can initialize our git repository. ( Since this is the first time we use this command it will create  an empty repository. That is, the .git directory, its sub-directories and templates that will be used by git. )


git init


4. Lets introduce ourselves to git, so that it knows who will do the changes, and commit into the repository.



git config --global user.name "Mahir Atmis"
git config --global user.email "atmismahir@gmail.com"


5. Lets create two files in our project. There are many ways to create a file under unix environment, and I will use output redirecting.


echo "content of foo" > foo.txt
echo "content of bar" > bar.txt


6. Now we created two files, but git doesn't track them. We want it to track our files, so we type


git add .


This will tell git to track all the files under the current directory. In technical terms this will add these files to the index.


7. Everything is ready to record into repository. Lets commit the project and save it in repository

git commit -m "My first Commit"


Here, the commit option saves into the repository the files that have been added . That is, to commit a change first you have to add it using the command at 6th step.  The -m flag is optional. If you use this flag, the message in between the parenthesis will be your commit message. If you don't use that flag,  git will open a page for you to write your commit message. Commit messages are extremely important for a programmer to know what changes he/she has  done at each commit.

8. Now lets make some changes in foo.txt file.

echo "Updated the content of foo.txt" > foo.txt

We changed the content of foo.txt.

9. Lets see if git is aware of the change we just made.

git status

This command shows the status of the git repository. That is, checks if there are any untracked or modified files to commit. When we  run the above command, we will get the following output:

# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified:   foo.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

The output says foo.txt is modified, but this change is not reflected into the repository. It asks for adding this change using git add , or git commit -a commands.  

10. For easiness we will use the latter one. That is,

git commit -a -m "foo.txt has been modified"

Providing the -a flag to git commit makes git save all the files that has been changed into the repository. NOTE: It won't save newly created files, it will only save the changed files. To add newly created files use "git add"

2 comments:

  1. Hey very nice..Keep up the good work! :)

    ReplyDelete
  2. Thank you Anagha :) I will add more as I search and learn :)

    ReplyDelete

Python Line Profilers using Decorator Pattern

You can use any of the following decorators to profile your functions line by line.  The first one(Profiler1) is a decorator as a class and...