用git创建和使用 bare 的 git repository 的一个示例

本文介绍了解决Git中遇到的一种特定推送错误的方法。该错误发生于尝试向非裸仓库推送更新时,文章详细解释了裸仓库与非裸仓库的区别,并通过实例演示如何创建和使用裸仓库来避免此类错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一直以来,我都是"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)
$
完。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值