新手问题 2 个人使用 Git 做版本控制,如何合并代码

johnlu · 2013年07月20日 · 最后由 lvjian700 回复于 2013年07月20日 · 12467 次阅读

克隆代码后,修改! 再通过命令:git push origin master 提交合并时报错:

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 [email protected]:/var/git.server/.../web

! [remote rejected] master -> master (branch is currently checked out)

error: failed to push some refs to '[email protected]:/var/git.server/.../web'

到底是什么原理?

updating the current branch in a non-bare repository

你的 origin repo 不是 bare 的,也就是说远程 repo 有工作区,且已经被 checkout 某个 branch,这种情况下 git 不允许你 push,因为如果你要 push 的代码跟远程代码有 conflict,在你这边是没有办法 resolve 的。

你们需要先创建一个 bare repo,然后各自从 bare repo clone,再开发,等 push 的时候是 push 到 bare repo,而不是对方进行开发的那个 repo

#1 楼 @loveky 换句话说,还要第三台服务器做 bare repo?

#2 楼 @JohnLu 或者你直接就在你的电脑上开另外一个 repo,作为 bare repo

#2 楼 @JohnLu 不需要,在你俩的随便一台电脑上就可以,如果使用的用户很多,也可以考虑做一台专门的 git server

我们平时就两个人。 1 * Gitosis 做服务器 2 * Git 做 client 服务器端使用 git init . --bare 初始化仓库. 客户端初始化 push 项目 git remote add origin git_At_hostname:repositories/project.git git commit -a -m "****" git push origin refs/head/master

#4 楼 @mouse_lin 开了另外的 bare repo,那么本地的工作树如何提交到 bare repo

#5 楼 @loveky 就 2 台电脑,如何创建呢?

#6 楼 @lvjian700 “1 * Gitosis 做服务器”,使用着个服务器的人的工作如何提交代码?

#8 楼 @JohnLu


# 首先从现有repo clone出一个bare repo
64 [email protected]@10:18:53:~/test/demo 
=> git clone --bare ~/temp_project/sandbox/ bare-repo.git
Cloning into bare repository 'bare-repo.git'...
done.
65 [email protected]@10:18:59:~/test/demo 
=> cd bare-repo.git/

# 可以看到repo是bare的
66 [email protected]@10:19:02:~/test/demo/bare-repo.git (test)
=> git config --get core.bare
true
67 [email protected]@10:19:10:~/test/demo/bare-repo.git (test)
=> cd ..

# 模拟某个用户
68 [email protected]@10:19:27:~/test/demo 
=> mkdir UserA
69 [email protected]@10:19:48:~/test/demo 
=> cd UserA/

# 用户从bare repo clone
70 [email protected]@10:19:50:~/test/demo/UserA 
=> git clone ../bare-repo.git/ repoA
Cloning into 'repoA'...
done.
71 [email protected]@10:20:09:~/test/demo/UserA 
=> cd repoA/
72 [email protected]@10:20:11:~/test/demo/UserA/repoA (test)
=> git config user.name userA
73 [email protected]@10:20:21:~/test/demo/UserA/repoA (test)
=> git config user.email '[email protected]'
74 [email protected]@10:21:58:~/test/demo/UserA/repoA (test)
=> ls
README.md     file_b_dev    file_b_master file_b_test

# 进行开发。。
75 [email protected]@10:22:00:~/test/demo/UserA/repoA (test)
=> touch a b c
76 [email protected]@10:22:04:~/test/demo/UserA/repoA (test)
=> git add .
77 [email protected]@10:22:06:~/test/demo/UserA/repoA (test)
=> git commit -m 'userA add new file'
[test 8d6efc1] userA add new file
 0 files changed
 create mode 100644 a
 create mode 100644 b
 create mode 100644 c

# push回bare repo
78 [email protected]@10:22:18:~/test/demo/UserA/repoA (test)
=> git push origin test:test
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 358 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /Users/gewang/test/demo/UserA/../bare-repo.git/
   b46828c..8d6efc1  test -> test

#10 楼 @loveky 明白了,本地的用户就用 127.0.0.1 添加远程仓库。谢谢

我是这样操作的! 在本地新建分支,fetch 远程,合并到主分支,上传到远程!

#10 楼 @loveky git config --get core.bare,这句操作有什么作用?

#13 楼 @JohnLu 只是查看一下当前 repo 是不是 bare,你熟悉了以后不需要运行这个命令

#14 楼 @loveky 明白了,谢谢!

#9 楼 @JohnLu 我的 Gitosis 架在 ubuntu 11.10, 用户 git, 平时使用自己用户 lvjian 需要创建仓库的时候: su git 然后到 gitosis 目录 mkdir & cd & git init . --bare 我的目录是 ~/repositories (Git 用户)

lvjian 使用: git remote add [email protected]:repositories/project.git git push origin master

其他用户 clone git clone [email protected]:repositories/project.git

需要 登录 后方可回复, 如果你还没有账号请 注册新账号