通常实际项目中会使用一台独立的机器作为 git 服务器, 然后在 git 服务器中建立一个远程的仓库,这样项目中所有的人都可以通过局域网来访问这个 git 服务器。下面通过本机模拟一台git服务器。
1.git服务器端得操作(本机终端)
首先建立一个仓库目录,然后初始化这个 git 仓库。假设我们在“/opt/git/”目录下面来创建。通过 git --bare init 命令创建了一个空的远程仓库.
$ cd /opt/git/
$ mkdir test.git
$ cd test.git/
$ git --bare init Initialized empty Git repository in /opt/git/test.git/
2.客户端操作。在本机打开另外一个终端,然后编译代码,
mkdir -p rlk/test
vim test.c
3.初始化本地的git仓库
book@Vlinux:~/rlk/test$ git init
Reinitialized existing Git repository in /home/book/rlk/test/.git/
4.查看当前工作目录状态
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.c
nothing added to commit but untracked files present (use "git add" to track)
5.用git add命令添加test.c到缓存区,并提交
$ git add test.c
$ git commit -s
6.把本地的 git 仓库推送到远程仓库中,首先需要通过 git remote add 命令添加刚才远程仓库的地址。
book@Vlinux:~/rlk/test$ git remote add origin ssh://book@192.168.0.101:/home/book/opt/git/test.git
7.查看关联的远程仓库的详细信息
book@Vlinux:~/rlk/test$ git remote -v
origin ssh://book@192.168.0.101:/home/book/opt/git/test.git (fetch)
origin ssh://book@192.168.0.101:/home/book/opt/git/test.git (push)
8.如果发现地址不对,可以用git remote remove <name>或者git remote rm origin
9.git push来推送工作内容
book@Vlinux:~/rlk/test$ git push origin master
book@192.168.0.101's password:
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 304 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://book@192.168.0.101:/home/book/opt/git/test.git
* [new branch] master -> master
10.复制远端仓库,这样就可以从远端仓库拉取源码了
book@Vlinux:~/rlk$ git clone ssh://book@192.168.0.101:/home/book/opt/git/test.git git
Cloning into 'git'...
book@192.168.0.101's password:
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
Checking connectivity... done.
book@Vlinux:~/rlk$ ls
git test
book@Vlinux:~/rlk$ cd git/
book@Vlinux:~/rlk/git$ ls
test.c