git使用中的疑难杂症(踩坑合集)

487 阅读2分钟

「这是我参与2022首次更文挑战的第6天,活动详情查看:2022首次更文挑战

一、 error: 'my_project/' does not have a commit checked out fatal: adding files failed

1、情景再现:

git add 提交文件到暂存区时报错

error: 'my_project/' does not have a commit checked out   fatal:
adding files failed

2、原因:

提交项目子目录中有.git文件(可能是隐藏状态),显示隐藏文件即可 在这里插入图片描述

3、解决办法:

找到 .git 文件夹,删除!

二、 src refspec main does not match any

1、情景再现:

git push -u origin main 提交项目到远程仓库时报错

$ ahua win64 ~/Desktop/vue (master)
$ git push -u origin main
error: src refspec main does not match any

2、原因:

提交到远程仓库中的分支和本地的仓库的分支不一样,可以看到上面代码块中的分支是master(现在github主分支是main)

3、解决办法:

修改本地分支名为main即可

git branch -m oldBranchName newBranchName

三、warning: LF will be replaced by CRLF in node/node_project.The file will have its original line endings in your working directory

1、情景再现:

git add添加文件到暂存区时警告

warning: LF will be replaced by CRLF in node/node-project.
The file will have its original line endings in your working directory

2、原因:

这是因为在windows和unix下的换行符不一样,git bush是unix风格设计的,虽然git默认会自动转换,但是有时会出问题

3、解决办法:

禁止自动转换

git config --global core.autocrlf false

四、 ! [rejected] main -> main (fetch first) error: failed to push some refs to 'github.com/xxxxxx'

1、情景再现:

git push origin main 提交代码到远程仓库时报错

 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/xxxxxx'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

2、原因:

远程仓库中的代码比本地仓库中的新(存在本地仓库没有的文件)

3、解决办法:

将远程仓库中的代码合并到本地即可

git pull --rebase origin main