一直以来,我都是"git init"之后,就立即使用git仓库了。今天需要为自己的git仓库另外clone到一个地方并修改和提交,然后在提交时发现有如下错误:
zx+Administrator@zx MINGW64 /d/my_work/my_code_clone (master)
$ git push
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 367 bytes | 0 bytes/s, 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 D:/my_work/my_code_origin/
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'D:/my_work/my_code_origin/'
zx+Administrator@zx MINGW64 /d/my_work/my_code_clone (master)
$
然后,我才知道 git repository 有 bare 和 non-bare 之分。我之前创建的都是 non-bare 的。
建立 Git 仓库:http://www.ituring.com.cn/article/179426
下面是创建和使用 bare 的 git repository 的步骤:
zx+Administrator@zx MINGW64 /e/learn_git
$ git init --bare /e/learn_git/myProject.git
Initialized empty Git repository in E:/learn_git/myProject.git/
注:创建一个bare的仓库。仓库的名字为"myProject"。
注:举例来说,一个叫做 my-project 的裸版本项目应该存储在名为 my-project.git 的目录下。
zx+Administrator@zx MINGW64 /e/learn_git
$ git clone /e/learn_git/myProject.git/ /e/learn_git/myProject
Cloning into 'E:/learn_git/myProject'...
warning: You appear to have cloned an empty repository.
done.
注:克隆"/e/learn_git/myProject.git/"到路径"/e/learn_git/myProject"下。
注:一般不指定路径。不指定时,默认克隆到当前路径的 my-project 目录下。
zx+Administrator@zx MINGW64 /e/learn_git
$ cd /e/learn_git/myProject
zx+Administrator@zx MINGW64 /e/learn_git/myProject (master)
$ echo "12345qwert" > first_file.txt
注:创建一个名为"first_file.txt"的文件。
zx+Administrator@zx MINGW64 /e/learn_git/myProject (master)
$ git add first_file.txt
zx+Administrator@zx MINGW64 /e/learn_git/myProject (master)
$ git commit -am 'add my first file to [myProject] repository.'
[master (root-commit) 4200a1a] add my first file to [myProject] repository.
1 file changed, 1 insertion(+)
create mode 100644 first_file.txt
注:提交"first_file.txt"到本地版本库。
zx+Administrator@zx MINGW64 /e/learn_git/myProject (master)
$ git pull --rebase
Your configuration specifies to merge with the ref 'refs/heads/master'
from the remote, but no such ref was fetched.
注:从远程库那里拉取数据。
zx+Administrator@zx MINGW64 /e/learn_git/myProject (master)
$ git push
Counting objects: 3, done.
Writing objects: 100% (3/3), 256 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To E:/learn_git/myProject.git/
* [new branch] master -> master
注:将本地库的更新推送到远程库。
zx+Administrator@zx MINGW64 /e/learn_git/myProject (master)
$ git pull --rebase
Current branch master is up to date.
zx+Administrator@zx MINGW64 /e/learn_git/myProject (master)
$ git push
Everything up-to-date
zx+Administrator@zx MINGW64 /e/learn_git/myProject (master)
$
完。