git push提交时报错,拒绝提交

本文介绍了一种常见的Git错误——提交时遇到远程仓库拒绝更新已检出分支的问题,并提供了解决方案,即通过修改服务器上的.git/config文件来允许推送当前分支。

今天初次创建了git项目,并尝试将代码提交,但是报错,一直拒绝提交,错误如下:

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 git@xxx.xxx.xxx.xxx:ldlogistics.git  
 ! [remote rejected] master -> master (branch is currently checked out)  
error: failed to push some refs to 'git@xxx.xxx.xxx.xxx:ldlogistics.git'


在网上查了很久,最终解决办法如下:

在服务器的ldlogistics.git中有隐藏文件夹.git  ,修改.git下的config文件,在文件末尾添加

[receive]
    denyCurrentBranch = ignore  

然后就能提交了(先是修改了本地的该文件,但是没有生效,后来修改了服务器上的就好了,也不知道是否两个地方都需要改或是只需要改服务器的)

通过 VSCode 向 Git 提交代码是一个非常直观且高效的过程,VSCode 内置了强大的 Git 集成功能,你无需使用命令行即可完成 **初始化仓库、添加文件、提交更改、推送远程** 等操作。 --- ## ✅ 前提条件 1. 已安装 [Visual Studio Code](https://code.visualstudio.com/) 2. 已安装 [Git](https://git-scm.com/downloads) 3. 已配置好 Git 用户名和邮箱(可选但推荐) ```bash git config --global user.name "YourName" git config --global user.email "your.email@example.com" ``` 4. 你的项目文件夹已经是一个 Git 仓库(或准备初始化) --- ## 🧭 步骤一:打开项目文件夹 1. 打开 VSCode 2. 点击 **“文件” → “打开文件夹”** 3. 选择你的项目目录(例如 `my-project`) > 如果该目录未初始化为 Git 仓库,VSCode 会提示是否初始化。 --- ## 📦 步骤二:初始化 Git 仓库(如果尚未初始化) ### 方法 A:自动提示初始化 - 打开项目后,如果发现没有 `.git` 文件夹,VSCode 右下角会弹出提示: > “此文件夹尚未初始化为 Git 仓库。立即初始化?” - 点击 **“初始化仓库”** ### 方法 B:手动初始化 1. 打开 VSCode 的终端: - 菜单栏 → 终端 → 新建终端 2. 输入: ```bash git init ``` ✅ 成功后,你会在左侧看到 **源代码管理(Source Control)图标上出现数字**,表示有未提交的更改。 --- ## 🔀 步骤三:进行代码修改并暂存更改 1. 修改一些文件(比如 `index.js`、`README.md`) 2. 切换到左侧 **源代码管理视图**(快捷键 `Ctrl + Shift + G`) 你会看到: - **更改(Changes)**:列出所有被修改但未暂存的文件 - 点击文件名可以预览差异(diff) ### 暂存更改(相当于 `git add`) - 👉 点击文件旁边的 `+` 图标 → 将该文件加入暂存区(Staged Changes) - 或点击顶部的 `+` → 暂存所有更改 > 💡 暂存后的文件会出现在 “Staged Changes” 区域 --- ## 📤 步骤四:提交代码(Commit) 1. 在 **消息框(Message Box)** 中输入提交信息(commit message),例如: ``` feat: add login functionality ``` 2. 点击 ✔️ **提交按钮**(对勾图标),或按 `Ctrl + Enter` > 这相当于执行了: > > ```bash > git commit -m "feat: add login functionality" > ``` ✅ 提交成功后,“Staged Changes” 会清空。 --- ## 🌐 步骤五:连接远程仓库并推送(Push) 如果你还没有关联远程仓库(如 GitHub、GitLab、Gitee),需要先添加。 ### 1. 添加远程仓库地址 在终端中运行: ```bash git remote add origin https://github.com/your-username/your-repo.git ``` > 替换为你自己的仓库 URL ### 2. 推送代码到远程 #### 第一次推送(设置上游分支) 在 VSCode 中: - 右键点击源代码管理面板中的任意空白处 - 或点击右上角的 **省略号菜单(...)→ PushPush to Origin** - 如果是首次推送,系统会提示你设置上游分支 或者使用命令行: ```bash git push -u origin main ``` > `-u origin main` 表示将本地 `main` 分支与远程 `origin/main` 关联,以后可以直接用 `git push` --- ## 🔄 后续同步流程(日常开发) | 操作 | 如何在 VSCode 中完成 | |------|------------------------| | 查看修改 | 左侧源代码管理图标,查看文件变化 | | 暂存文件 | 点击 `+` 添加到暂存区 | | 提交 | 输入消息后点击 ✔️ | | 推送 | 点击右上角 `...` → Push,或状态栏显示 ↑ 数字点击它 | --- ## 🎨 额外技巧:VSCode Git 快捷操作 | 功能 | 操作方式 | |------|-----------| | 查看某次提交的详情 | 在 “COMMITS” 视图中点击历史记录 | | 回退到某个提交 | 右键提交 → “Reset HEAD to here” | | 创建新分支 | 左下角显示当前分支的地方 → 点击 → “Create New Branch” | | 切换分支 | 左下角点击分支名 → 选择其他分支 | | 拉取最新代码 | `...` → Pull | --- ## 🧪 示例截图说明(文字版) ``` [源代码管理] ─────────────────────────────── * 暂存的更改 (1) ➤ index.js * 更改 (2) ➤ README.md ➤ styles.css 📝 消息: fix: update style and add login logic ✔️ [提交] ... [放弃] ``` 👉 输入消息 → 点击 ✔️ → 完成提交 --- ## ⚠️ 注意事项 | 问题 | 解决方法 | |------|----------| | 提交报错“Please tell me who you are” | 没设置 `user.name/email`,运行 `git config` 设置 | | 推送失败(权限拒绝) | 检查 SSH 密钥或 HTTPS 凭据是否正确 | | 分支名是 `main` 还是 `master`? | 现代仓库多用 `main`,注意匹配 | | 忘记保存文件? | VSCode 会自动保存,记得 `Ctrl + S` 或启用 Auto Save | > 启用自动保存:菜单 → 文件 → 自动保存 ✅ --- ## ✅ 总结流程图(简化版) ```text 打开项目 → 初始化 Git → 修改文件 → ↓ 暂存更改 (+) → 输入提交信息 → 点击 ✔️ 提交 → ↓ 设置 remote origin → 推送 (Push) → 完成! ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值