2013年10月15日 星期二

[記事] Git quick installation and setup

屬於阿宅世界的技術文章,想看的再點開,切莫自誤 !


[Git Installation]

The installation is pretty ez.
shell> mkdir -p /home/git/src
shell> cd /home/git/src
shell> wget https://git-core.googlecode.com/files/git-1.8.4.tar.gz
shell> tar zxvf git-1.8.4.tar.gz
shell> cd git-1.8.4
shell> ./configure --prefix=/home/git
shell> make; make install

[Config the git environment]

Check the config first , it should nothing now.
shell> git config --list

Make some changes
shell> git config --global user.name "user1"
shell> git config --global user.email "user1@pc1"
shell> git config --global core.editor vim
shell> git config --global merge.tool vimdiff

Check it again.
shell> git config --list
user.name=user1
user.email=user1@pc1
core.editor=vim
merge.tool=vimdiff

[Making the project repositories]

shell> cd /home/git_repos/
shell> mkdir test.git
shell> cd test.git/
shell> git --bare init
Initialized empty Git repository in /home/git_repos/test.git/
p.s By convention, bare repository directories end in .git

Let's see what have been created
shell> ls -al /home/git_repos/
total 40
drwxr-xr-x 7 git git  4096 Oct  9 14:28 .
drwxr-xr-x 4 git root 4096 Oct  9 14:28 ..
drwxr-xr-x 2 git git  4096 Oct  9 14:28 branches
-rw-r--r-- 1 git git    66 Oct  9 14:28 config
-rw-r--r-- 1 git git    73 Oct  9 14:28 description
-rw-r--r-- 1 git git    23 Oct  9 14:28 HEAD
drwxr-xr-x 2 git git  4096 Oct  9 14:28 hooks
drwxr-xr-x 2 git git  4096 Oct  9 14:28 info
drwxr-xr-x 4 git git  4096 Oct  9 14:28 objects
drwxr-xr-x 4 git git  4096 Oct  9 14:28 refs

Git can use 4 network protocols to transferdata. we use SSH in this example.
To do this ,we create a git user on the git server

shell> user -m -d /home/git_repos git

And remember to set the password of the user (git)

[Initiate project]

You may do this in the same pc , or do it remotely like below

Create an empty project (locally)
shell> mkdir my_git_proj
shell> cd my_git_proj/
shell> git init
Initialized empty Git repository in /<HOME>/my_git_proj/.git/

Check the git status
shell> git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)

Now, add a file in the project and commit it
shell> echo "hello world" > file.txt
shell> git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
# file.txt
nothing added to commit but untracked files present (use "git add" to track)

shell> git add file.txt
shell> git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
# new file:   file.txt
#

shell> git commit -m "first commit"
[master (root-commit) 49bbf09] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 file.txt

Check the status again , ok , it's committed
shell> git status
# On branch master
nothing to commit, working directory clean

Your may also check the commit log
shell> git log
commit 49bbf09354a8738616320b917ab9b9d8c72f4aba
Author: user2 <user2@pc2>
Date:   Wed Oct 9 14:34:58 2013 +0800

    first commit

Put the whole project into remote server,
shell> git remote
(show nothing)

Create the remote repository first.
(p.s origin is the default name of repository)
shell> git remote add origin git@<YOUR_GIT_SERVER>:test.git
shell> git remote
origin

and push local files (put-into) to server
shell> git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 209 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@<YOUR_GIT_SERVER>:test.git
 * [new branch]      master -> master

and it's done.

[Test the version control]

clone the project on pc 1 by user 1
shell> cd /tmp
shell> mkdir git
shell> cd git/
shell> git clone git@<YOUR_GIT_SERVER>:test.git
Cloning into 'test'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
Checking connectivity... done

shell> ls
test

shell> cd test
shell> ls
file.txt

shell> git status
# On branch master
nothing to commit, working directory clean

shell> git log
commit 49bbf09354a8738616320b917ab9b9d8c72f4aba
Author: user2 <user2@pc2>
Date:   Wed Oct 9 14:34:58 2013 +0800

    first commit

ok, we got the project correctly.
Try to modify the file and commit.

shell> echo "line2" >> file.txt
shell> cat file.txt
hello world
line2

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

shell> git add file.txt
shell> git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
# modified:   file.txt
#

shell> git commit -m "Second commit"
[master e81f53b] Second commit
 1 file changed, 1 insertion(+)

shell> git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#   (use "git push" to publish your local commits)
#
nothing to commit, working directory clean

check file on pc 2 changed or not
shell> cat file.txt
hello world

hmm... nope. nothing happens. it's because we need to push the modification to the server

push from pc 1
shell> git push
Counting objects: 5, done.
Writing objects: 100% (3/3), 247 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@localhost:test.git
   49bbf09..e81f53b  master -> master

and pull from pc 2
shell> git remote
origin

shell> git branch
* master
shell> git pull origin master
From <YOUR_GIT_SERVER>:test
 * branch            master     -> FETCH_HEAD
Updating 49bbf09..e81f53b
Fast-forward
 file.txt | 1 +
 1 file changed, 1 insertion(+)

check it again
shell> cat file.txt
hello world
line2
Good. it works.

[config gitweb with lighttpd]

shell> vi /home/lighttpd/etc/lighttpd.conf

"""
(server.modules += ( "mod_cgi", "mod_alias")

alias.url += (
               "/static/git-logo.png" => "/home/git/share/gitweb/static/git-logo.png",
               "/static/gitweb.js" => "/home/git/share/gitweb/static/gitweb.js",
               "/static/gitweb.css" => "/home/git/share/gitweb/static/gitweb.css",
               "/git" => "/home/git/share/gitweb/gitweb.cgi"
             )
"""

shell> vi /home/git/share/gitweb/gitweb.cgi

"""
our $projectroot = "/home/git_repos";
"""

and test it through http://YOUR_DOMAIN/git

p.s you may specify the path at configuration time by

make GITWEB_PROJECTROOT=/home/git_repos ...


[Error]
If you see the following message,
...
bash: git-upload-pack: command not found
fatal: Could not read from remote repository.
...

try to add the GIT bin path to the remote user's PATH variable (eg. git)

0 意見:

張貼留言