git用法小结--建立远程仓库

最近一直在学习使用git来管理自己的程序,总是今天东学一点,明天西凑一点,到用的时候,总是有些茫然不知所措。

在博客园里看见一篇老好的文章,教我们做笔记啦,但是做完笔记还是要记得总结哦!

来吧,让我们一起来总结吧,今天先来看看git远程的仓库是怎么建立的。

当然,使用git嘛,第一步肯定是得新建一个git仓库,总得有个操作的空间吧,巧妇难为无米之炊嘛。

1.初始化一个空的git仓库

  1. 1 software@debian:~$ mkdir yafeng
  2. 2 software@debian:~$ cd yafeng/
  3. 3 software@debian:~/yafeng$ ls
  4. 4 software@debian:~/yafeng$ git init
  5. 5 Initialized empty Git repository in /home/software/yafeng/.git/
  6. 6 software@debian:~/yafeng$

命令注释:

在上面的命令中,真正去初始化的是第四行的那句---git init

当然,还有很多同学会看见加了参数--bare的命令,这个命令会在我们以后慢慢给大家解释,对于不是作为共享仓库,而是作为一个自己操作的仓库,上面这样就足够了。

好了,现在yafeng目录就是我们的据点---git仓库了哦。

下面我们总要做点什么的吧,入宝山总不能光看着哦:

2.向仓库提交我们写的文件

  1. 1 software@debian:~/yafeng$ echo "our first git repository" >> file
  2. 2 software@debian:~/yafeng$ ls
  3. 3 file
  4. 4 software@debian:~/yafeng$ git add file
  5. 5 software@debian:~/yafeng$ git commit -m "the first file to commit" file
  6. 6 [master (root-commit) 0c72641] the first file to commit
  7. 7  1 files changed, 1 insertions(+), 0 deletions(-)
  8. 8  create mode 100644 file
  9. 9 software@debian:~/yafeng$

命令解释:
我们在仓库中新建了一个文件file,作为我们的示例文件。

第4行:将file文件的信息添加到git仓库的索引库中,并没有真正添加到库。当然上例中的file文件只是我们的示例,它是一个路径,因此,可以是文件,更可以是目录。

第5行:将索引库中的内容向git仓库进行提交。这步之后文件file才算真正提交到拉git仓库中。双引号中的内容是根据每次修改的不同内容,由我们自己去填写的,

很多人会看见

  git commit -a -m “ ”

这条的命令是在你已经add了一个或多个文件过之后,然后修改了这些文件,就可以使用该命令进行提交。

好了,不管怎么样,终于是将文件提交到库了。可是现在的仓库只是一个本地的仓库,我们的目标是变成远程仓库哦,继续吧。

3.在本地仓库添加一个远程仓库,并将本地的master分支跟踪到远程分支

  1. 1 software@debian:~/yafeng$ git remote add origin ssh://software@172.16.0.30/~/yafeng/.git
  2. 2 software@debian:~/yafeng$ git push origin master
  3. 3 software@172.16.0.30's password:
  4. 4 Everything up-to-date
  5. 5 software@debian:~/yafeng$

命令注释:

第1行:在本地仓库添加一个远程仓库,当然ssh后面的地址是我们本地仓库的地址.

第2行:将本地master分支跟踪到远程分支,在git仓库建立之初就会有一个默认的master分支,当然你如果建立了其他分支,也可以用同样的方法去跟踪.

对于分支的事情,我们会在以后细细的讲述.

做到拉这一步了吗?我告诉你,你已经完成目的了哦,现在的git仓库已经是一个远程仓库了,

不相信吗?我们来测试一次阿:

4.测试

现在本机上看看:

  1. 1 software@debian:~/yafeng$ git remote show origin
  2. 2 software@172.16.0.30's password:
  3. 3 * remote origin
  4. 4   Fetch URL: ssh://software@172.16.0.30/~/yafeng/.git
  5. 5   Push  URL: ssh://software@172.16.0.30/~/yafeng/.git
  6. 6   HEAD branch: master
  7. 7   Remote branch:
  8. 8     master tracked
  9. 9   Local ref configured for 'git push':
  10. 10     master pushes to master (up to date)
  11. 11 software@debian:~/yafeng$

代码注释:

第1行:显示远程信息

很多看见这还是会不以为然的,这又能说明什么呢?好,那就来点实际的:

在另一个机子上,远程clone

  1. 1 root@yafeng-VirtualBox:~# ls
  2. 2 bin  gittest  read_temp
  3. 3 root@yafeng-VirtualBox:~# git clone ssh://software@172.16.0.30/~/yafeng/.git
  4. 4 Cloning into yafeng...
  5. 5 software@172.16.0.30's password:
  6. 6 remote: Counting objects: 9, done.
  7. 7 remote: Compressing objects: 100% (3/3), done.
  8. 8 remote: Total 9 (delta 0), reused 0 (delta 0)
  9. 9 Receiving objects: 100% (9/9), done.
  10. 10 root@yafeng-VirtualBox:~# ls
  11. 11 bin  gittest  read_temp  yafeng
  12. 12 root@yafeng-VirtualBox:~# cd yafeng/
  13. 13 root@yafeng-VirtualBox:~/yafeng# ls
  14. 14 file
  15. 15 root@yafeng-VirtualBox:~/yafeng#

代码注释:

第3行:就是远程clone仓库.很明显的对比可以知道多了yafeng目录,而这个yafeng目录里的内容和我们另外一台机子上的内容一样

至此,一个简单的git远程仓库就建好了,简单不,试试吧!!


如果:

lijun@ubuntu:~/Git_clone/test_git$ echo "aasdf" > li

lijun@ubuntu:~/Git_clone/test_git$ git add  li

lijun@ubuntu:~/Git_clone/test_git$ git commit -m  "new file li."

lijun@ubuntu:~/Git_clone/test_git$ git  push

Password:
Counting objects: 4,  done.
Delta compression using up to 2 threads.
Compressing objects: 100%  (2/2), done.
Writing objects: 100% (3/3), 263 bytes, done.
Total 3 (delta  0), reused 0 (delta 0)
remote: error: refusing to update checked out branch:  refs/heads/master
remote: error: By default, updating the current branch in a  non-bare repository
remote: error: is denied, because it will make the index  and work tree inconsistent
remote: error: with what you pushed, and will  require 'git reset --hard' to match
remote: error: the work tree to  HEAD.
remote: error:
remote: error: You can set  'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore'  or 'warn' in the remote repository to allow pushing into
remote: error: its  current branch; however, this is not recommended unless you
remote: error:  arranged to update its work tree to match what you pushed in some
remote:  error: other way.
remote: error:
remote: error: To squelch this message  and still keep the default behaviour, set
remote: error:  'receive.denyCurrentBranch' configuration variable to 'refuse'.
To  junmuzi@202.201.12.221:/git/test_git
! [remote rejected] HEAD -> master  (branch is currently checked out)
error: failed to push some refs to  'junmuzi@202.201.12.221:/git/test_git'


错误原因以及解决


这是由于git默认拒绝了push操作,需要进行设置,修改远端仓库中的.git/config文件,在该文件后面添加如下代码:

[receive]

                denyCurrentBranch = ignore

无法查看push后的git中文件的原因与解决方法

在初始化远程仓库时最好使用 git --bare init   而不要使用:git  init    (但是最好还是用git init , 用git --bare init的话,跟上面的做法可能就不一样了,嘿嘿)

如果使用了git init初始化,则远程仓库的目录下,也包含work  tree,当本地仓库向远程仓库push时,   如果远程仓库正在push的分支上(如果当时不在push的分支,就没有问题), 那么push后的结果不会反应在work tree上,  也即在远程仓库的目录下对应的文件还是之前的内容。

Git常用命令 建立代码仓库
  1. git init
  2.         创建一个空的git仓库或初始化一个已存在的仓库,会建一个.git隐藏目录。
  3.         在.git目录下,会创建objects, refs/heads, refs/tags和模板文件。
  4.         $ mkdir facebox.git facebox
  5.         #建立仓库服务器端
  6.         $ cd facebox.git
  7.         $ git init --bare
  8.         $ ls
  9.          branches  config  description  HEAD  hooks  info  objects  refs
  10.         #建立仓库客户端,建立一个空的git仓库或重新初始化仓库
  11.        $ cd ../facebox
  12.          $ git init 
  13.          $ ls -A
  14.            .git

转载自:http://www.cnblogs.com/xiaoya901109/archive/2012/08/03/2620664.html
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值