Git项目合作(remote pull push)学习学习

首先来看看从服务器上取下项目的一个小问题吧:

[root@localhost repository]# git clone root@74.207.254.10:/usr/local/system/repository/blog
Initialized empty Git repository in /root/repository/blog/.git/
bash: git-upload-pack: command not found #竟然报这么个错误,这是怎么回事呢?
fatal: The remote end hung up unexpectedly

通过google得知,git-upload-pack这个命令的path为:/usr/local/git/bin/ ,他不在non-login shell 路径下(/usr/bin),下面是解决办法:

# Fix it with symlinks in /usr/bin
[root@li96-10 ~]# cd /usr/bin/
[root@li96-10 bin]# ln -s /usr/local/git/bin/git* .

首先我们在服务器上初始化一个git 项目:

[root@li96-10 repository]# mkdir blog.git
[root@li96-10 repository]# cd blog.git/
[root@li96-10 blog.git]# git init --bare #一定要加上--bare,且该目录不能有任何文件和目录,否则报错:fatal: Out of memory? mmap failed: No such device
Initialized empty Git repository in /usr/local/system/repository/blog.git/

接下来我们在创建一个git 项目:

[root@li96-10 blog.git]# cd ..
[root@li96-10 repository]# mkdir test.git
[root@li96-10 repository]# cd test.git/
[root@li96-10 test.git]# echo "some content" >> file
[root@li96-10 test.git]# git add file
fatal: Not a git repository (or any of the parent directories): .git
[root@li96-10 test.git]# git init
Initialized empty Git repository in /usr/local/system/repository/test.git/.git/
[root@li96-10 test.git]# git add file
[root@li96-10 test.git]# git commit -am "first commit"
[master (root-commit) 3ea3176] first commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 file

将test.git push到blog.git项目上

[root@li96-10 test.git]# git remote add origin ../blog.git
[root@li96-10 test.git]# git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 213 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To ../blog.git
* [new branch] master -> master

现在我们从blog.git中clone出来:

[root@li96-10 blog.git]# cd ..
[root@li96-10 repository]# git clone blog.git #因为我们的reposi是blog.git,clone下来后就会是blog了
Initialized empty Git repository in /usr/local/system/repository/blog/.git/
[root@li96-10 repository]# cd blog
[root@li96-10 blog]# ll
total 4
-rw-r--r-- 1 root root 13 Jan 4 22:09 file

可以看到项目中果然有个file文件,是刚才我们push进去的
那么下面我们在新clone的branch blog里面增加一个文件看看,

[root@li96-10 blog]# echo 'Second file contents' > file2
[root@li96-10 blog]# git add file2
[root@li96-10 blog]# git commit -am'added second file'
[master 63be855] added second file
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 file2
[root@li96-10 blog]# git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit (working directory clean)

然后我们push到blog.git

[root@li96-10 blog]# git push
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 280 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /usr/local/system/repository/blog.git
3ea3176..63be855 master -> master

这样的话,我们的blog.git已经有两个文件了file,file2
下面我们进到test.git里面,并且将刚才在first的修改合并过来:

[root@li96-10 blog]# cd ../test.git/
[root@li96-10 test.git]# git fetch origin
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From ../blog
3ea3176..63be855 master -> origin/master
[root@li96-10 test.git]# git merge origin/master
Updating 3ea3176..63be855
Fast forward
file2 | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 file2

我们可以看到,已经将在first的修改合并过来了,在上面的操作中我们用了fetch和merge,其实我们可以将两个命令简化为一个pull命令:

[root@li96-10 test.git]# git pull origin master
From ../blog
* branch master -> FETCH_HEAD
Already up-to-date.

总结一下,将版本库拿下来的命令是pull,而将本地修改与版本库合并的命令是push.并且push之前先要pull哦!
当然了,这些操作都是在一台机器上操作的,下面演示一下使用ssh协议连线到真正的remote 机器:
从remote上clone下来:

[root@master repository]# git clone ssh://gituser@xxx.xxx.xxx.xxx/path/to/repository/blog.git
Initialized empty Git repository in /root/repository/blog/.git/
remote: Counting objects: 1055, done.
remote: Compressing objects: 100% (822/822), done.
remote: Total 1055 (delta 115), reused 1055 (delta 115)
Receiving objects: 100% (1055/1055), 1.29 MiB | 19 KiB/s, done.
Resolving deltas: 100% (115/115), done.
[root@master repository]# ll
总计 4
drwxr-xr-x 17 root root 4096 01-05 20:57 blog

然后,当我们在本地做了修改,想要提交到remote的时候,我们就可以直接push了:

[root@master repository]# cd blog/
[root@master blog]# git push
Everything up-to-date

这样就基本OK了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值