Tuesday 4 March 2014

using local repository git



Git is one of the most powerful (probably the most one) distributed software version control tool. And github (https://github.com/), an internet based git version, has been the largest online software repository.

Here are some basic tutorials about how to use git as a local repository. Although as a distributed version control system, git is more powerful when it is used online. But we can easily get the basic idea how git is working by local repository.

understand basic items in git

To understand how git is working, we need to know the below:
The Git directory is where Git stores the metadata and object database for your project. This is the most important part of Git, and it is what is copied when you clone a repository from another computer.
The working directory is a single checkout of one version of the project. These files are pulled out of the compressed database in the Git directory and placed on disk for you to use or modify.
The staging area is a simple file, generally contained in your Git directory, that stores information about what will go into your next commit.



Create local repository.

Local repository is very handy and easy to use. And it is a good way to practise git.
Create a directory for git working area.
#mkdir testgit; cd testgit
#git init – it will create the initial git repository under the directory called .git. never manually modify the content in the directory as it will destroy the repository You can modify the files in the testgit directory (add, modify, delete) and see the difference between your currect directory and the repository

file modification, add and commit

After you created the file, you can see the changes via command
#git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       mytestfile.txt

nothing added to commit but untracked files present (use "git add" to track)

It shows there is a file but untracked. That mean the change is made on your working directory but not to staging area nor git directory yet.

Now let’s add it to staging area by add command:
[root@X001 testgit]# git add mytestfile.txt
[root@X001 testgit]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   mytestfile.txt

Now it is recorded in staging area and ready for commit to the repository.

Let’s commit it.

[root@X001 testgit]# git commit -m "my first commit with a new file"
[master (root-commit) c53f1ce] my first commit with a new file
 Committer: root <root@X001.(none)>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

If the identity used for this commit is wrong, you can fix it with:

    git commit --amend --author='Your Name <you@example.com>'

 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 mytestfile.txt
 [root@X001 testgit]# git log
commit c53f1ce7756990e78910190a8d1f6a312f61b004
Author: root <root@X001.(none)>
Date:   Tue Mar 4 18:34:43 2014 +1100

my first commit with a new file

after you committed it (with your comment), you will see the commit log shows the commit id and the comment.

Let’s edit the file then use git diff to check the difference between the current file and the file in the staging area (not the repository).

[root@X001 testgit]# git diff -- mytestfile.txt
diff --git a/mytestfile.txt b/mytestfile.txt
index 493021b..0b2939c 100644
--- a/mytestfile.txt
+++ b/mytestfile.txt
@@ -1 +1,2 @@
 this is a test file
+add a new line here                     --             this is my new line

Now add and commit it. See git tracks the commit changes.
[root@X001 testgit]# git log
commit bc07516ed8342b947ad5e0deff4aa7ff040c4416
Author: rafa <root@X001.(none)>
Date:   Tue Mar 4 18:41:34 2014 +1100

    my second commit with a new line

commit c53f1ce7756990e78910190a8d1f6a312f61b004
Author: root <root@X001.(none)>
Date:   Tue Mar 4 18:34:43 2014 +1100

    my first commit with a new file

version reverse

if you want to reverse your change to previous version. There are three ways.
Let modify the file again and add a new line below
  • If the modification has not been added to staging area. Using the command: git checkout – <file> to recover

  • If the file has been added to the staging area. Using the commands:
"git reset HEAD <file>..." to unstage to unstage
“git checkout -- <file>”  to recover

  • If the file has already been submitted. Using the command: git reset –hard [commit id] to rollbak to previous version. You may use git log or git reflog to view the      commit id

delete the file

                If you want just delete the file in repository, you may need to
1.       Delete the original file in working area
2.       git rm [file]
3.       git commit
here is the basic instructions about how to use a local repository we will talk about using remote (internet based) repository soon.

No comments:

Post a Comment