您的位置:首页 > 其它

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

2012-10-30 15:53 465 查看
1.初始化一个空的git仓库

1 junmuzi@mybox /git $ mkdir test_git/
2 junmuzi@mybox /git $ cd test_git/
3 junmuzi@mybox /git/test_git $ ls
4 junmuzi@mybox /git/test_git $ git  init
5 Initialized empty Git repository in /git/test_git/
6 junmuzi@mybox /git/test_git $


命令注释:

在上面的命令中,真正去初始化的是第四行的那句---git init (这样做,有可能会有错,要修改.git/config这个文件,后面会将到)

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

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

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

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

命令解释:

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

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

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

很多人会看见

  git commit -a -m “ ”

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

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

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

1 junmuzi@mybox /git/test_git $ git remote add origin junmuzi@202.201.12.221:/git/test_git/.git
2 junmuzi@mybox /git/test_git $ git push origin master
3 Password:
4 Everything up-to-date
5 junmuzi@mybox /git/test_git $


命令注释:

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

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

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

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

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

4.测试

现在本机上看看:

1 junmuzi@mybox /git/test_git $ git remote show origin
2 Password:
3 * remote origin
4   Fetch URL: junmuzi@202.201.12.221:/git/test_git/.git
5   Push  URL: junmuzi@202.201.12.221:/git/test_git/.git
6   HEAD branch: master
7   Remote branch:
8     master tracked
9   Local ref configured for 'git push':
10     master pushes to master (up to date)
11 junmuzi@mybox /git/test_git $


代码注释:

第1行:显示远程信息

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

在另一个机子上,远程clone

1 lijun@ubuntu:~/Git_clone/test_git$ ls
2 bin  gittest  read_temp
3 lijun@ubuntu:~/Git_clone/test_git$ git clone junmuzi@202.201.12.221:/git/test_git
4 Cloning into yafeng...
5 Password:
6 remote: Counting objects: 9, done.
7 remote: Compressing objects: 100% (3/3), done.
8 remote: Total 9 (delta 0), reused 0 (delta 0)
9 Receiving objects: 100% (9/9), done.
10 lijun@ubuntu:~/Git_clone/test_git$ ls
11 bin  gittest  read_temp  test_git
12 lijun@ubuntu:~/Git_clone/test_git$ cd test_git/
13 lijun@ubuntu:~/Git_clone/test_git$ ls
14 file
15 lijun@ubuntu:~/Git_clone/test_git$


代码注释:

第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上, 也即在远程仓库的目录下对应的文件还是之前的内容。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: