日常开发中,你需要掌握的git使用报错解决

1、不允许将代码推送到该项目上受保护的分支

- 问题发生:

remote: GitLab: You are not allowed to push code to protected branches on this project.
To https://git.lianjingkeji.com/backend/trade.git
 ! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to 'https://git.lianjingkeji.com/backend/trade.git'

- 问题原因:
该分支为受保护分支,用户没有权限推送代码到这个分支。

- 解决方案:

1. 解决方法一:修改提交代码人员角色

进入代码仓库详情页,单击“成员列表”页签,搜索目标用户,修改成员为主程或以上管理权限角色

2. 解决方法二:修改分支保护设置

进入代码仓库详情页,选择“设置 > 仓库管理 > 保护分支管理”,解除对该分支的保护。

2、代码拉取失败

- 问题发生:

用git pull来更新代码,遇到了下面的问题:

# git pull
Updating fc1d61e..e4f2867
error: Your local changes to the following files would be overwritten by merge:
	main.go
Please, commit your changes or stash them before you can merge.
Aborting

- 问题原因:

团队其他成员修改了某文件并已提交入库,你在pull之前修改了本地该文件,等你修改完代码再pull时,这时会报错如下错误:

- 解决方案:

1. 解决方法一:保留修改

执行以下三条命令

git stash #封存修改
git pull origin master 
git stash pop #把修改还原

注:

git stash:备份当前工作区内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致。同时,将当前工作区内容保存到Git栈中

git pull:拉取服务器上当前分支代码

git stash pop:从Git栈中读取最近一次保存的内容,恢复工作区相关内容。同时,用户可能进行多次stash操作,需要保证后stash的最先被取到,所以用栈(先进后出)来管理;pop取栈顶的内容并恢复

git stash list:显示Git栈内的所有备份,可以利用这个列表来决定从那个地方恢复。

git stash clear:清空Git栈

2. 解决方法二:废弃修改:

核心思想就是版本回退,具体命令如下

git reset --hard 
git pull origin master

注:不建议使用第二种。除非你再三确定不需要本地的修改了。

3、电子邮箱未配置

- 问题描述:

使用git控制代码版本时,出现如下无法检测到邮箱的错误

# git stash

*** Please tell me who you are.

Run

  git config --global user.email "[email protected]"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'root@ljkj-dev-00.(none)')
Cannot save the current index state

- 问题原因:

因为我们没有在 git 中分配我们的用户名和电子邮件 ID,所以我们要做的是在 git 中分配它

- 解决方案:

配置你的github用户名和和邮箱,邮箱是你github的注册邮箱,用户名是你github的用户名

git config --global user.email "[email protected]"
git config --global user.name "lijie"

再次运行,成功

4、git add 警告

- 问题描述:

git add:添加至暂存区,但并未提交至服务器。git add . 是表示把当前目录下的所有更新添加至暂存区。有时在终端操作这个会提示:

warning: LF will be replaced by CRLF in xxxx(需要提交的文件名).
The file will have its original line endings in your working directory

- 问题原因:

这是因为文件中换行符的差别导致的。这个提示的意思是说:会把windows格式(CRLF(也就是回车换行))转换成Unix格式(LF),这些是转换文件格式的警告,不影响使用。
git默认支持LF。windows commit代码时git会把CRLF转LF,update代码时LF换CRLF。

此时只需要执行如下代码:

git rm -r --cached .
git config core.autocrlf false
git add .

或者在Gitshell中输入如下命令解决:

git config --global core.autocrlf false

使用以下命令查看Git所有配置。

git config --list

5、提交代码分支错误

- 问题描述:

当我们在github上传创建仓库以后默认生成的页面提示我们的是推送到main分支。

error: src refspec main does not match any 
error: failed to push some refs to

- 问题原因:
但是实际我们创建的项目是在master分支下面。我们只要
把git push orgin main改成git push origin master就好了。

6、提交代码分支错误

- 问题描述:

使用 github 打 tag 的时候失败,错误提示为

We weren’t able to create the release for you. Make sure you have a valid tag.

- 解决方案:

需要先手动创建,再进行输入,就可以成功打tag

7、存在隐藏文件夹

- 问题描述:

提交代码时,报错有文件未提交

$ git add .
error: 'lijie/' does not have a commit checked out
fatal: adding files failed

- 解决方案:

原因是出现了一个隐藏文件夹lijie/ 在你的项目文件夹中找到并且删除,就OK啦

猜你喜欢

转载自blog.csdn.net/cljdsc/article/details/127112417
今日推荐