Git常见报错

常见报错

由于github经常出现网络方面的报错,所以就先拿国内的gitee练手

git add报错

  • warning: in the working copy of '2023/08/07/Hello World!/index.html', LF will be replaced by CRLF the next time Git touches it

Git在文件中检测到一些换行符LF(Line Feed),将会被替换为CRLF(Carriage Return+Line Feed)回车换行。这种问题通常出现在跨操作系统的协作中。Windows使用CRLF,Unix系统使用LF。此警告可以忽略

git push报错

  • 运行git push gitee pages,报错error: src refspec pages does not match any

表示Git找不到pages此分支。注意:并不是在云端新建一个pages分支,而是在本地仓库使用git checkout -b pages创建并切换分支,最后push云端自动生成一个分支。(创建新分支时,新分支的内容和当前分支是完全一致的,包括提交历史和文件状态)

refspec是指reference specification,在Git中用于指定引用,指某个特定提交的指针,如分支、标签等

git push中,refspec指定了该将哪个本地引用(分支)推送到远程引用(分支),refspec的格式:<src>:<dst>。例如,把本地的a分支推送到远程的b分支:git push gitee a:b,要确保本地已存在a分支。如果远程分支b不存在,则会自动创建

  • 运行git push gitee master,报错:
1
2
3
4
5
6
7
8
To https://gitee.com/lanan23/gitstudy.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://gitee.com/lanan23/gitstudy.git'
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.

表示推送被拒绝。因为远程分支包含了本地分支没有的提交,例如远程有README.md文件,但是本地却没有,解决方法:

  • git push -f gitee master简单粗暴,-f(force)表示强制覆盖远程分支的内容(突然发现Gitee官网还有比较详细的教程

git branch

  • git branch -a显示:

    1
    2
    3
    4
    5
    6
    *master # 代表本地分支
    remotes/origin/HEAD -> origin/master # 表示远程仓库的默认分支是master
    remotes/origin/bbbb
    remotes/origin/master
    remotes/origin/pages
    remotes/origin/pg

remotes/origin/HEAD的用途:在git pull/push等操作中,如果未指定分支,Git会自动根据remotes/origin/HEAD的设置选择默认分支。注意:这个设置并不能在本地Git仓库中直接修改,而是由远程的Git服务器自动设置的,只能在github/gitee上进行修改。

关于本地的默认分支,查看.git/HEAD,内容ref: refs/heads/master表示默认分支master。如refs/tags/v1.0表示标签v1.0

如果想克隆其他分支,加上-b参数:git clone -b <branch_name> <url>

注意

在git bash中每次ctrl+v会出现多于字符^[[200~,解决方法:鼠标中键直接粘贴

疑问

  • 在用户目录下修改.gitconfig后再进行推送,但是身份信息并未改变,可能的原因:
    • 本地仓库配置(局部)优先级高于全局配置,即使用git config user.name进行的配置,这些局部配置会覆盖全局配置(排除
    • 身份信息的缓存(累了 不想琢磨了 不重要