Git 使用时遇到的问题汇总(目前8个问题)

6 篇文章 0 订阅

注:本文无特别声明默认都在Windows系统中出现的问题,在其它操作系统操作标题前会标注。 

 1、git commit错误:unable to auto-detect email address

(1)执行命令及错误信息

$ git commit -m 'first commit with solidity scripts'

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  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 'Tracy@DESKTOP-101.(none)')

(2)解决方案

1)修改此项目配置(推荐)

方法一:使用命令

git config user.name username
git config user.email you@example.com

方法二:修改配置文件

进入项目下的 .git 目录,打开文件 config,在最前面添加一下代码

 [user]
 email=you@example.com
 name=username


2)修改全局配置(使用命令)

git config --global user.name username
git config --global user.email you@example.com

2、(CentOS系统)每次pull等操作都要用户名密码

(1)执行命令及提示信息 

[root@Tracy ~]# git pull origin branchName
Username for 'https://gitee.com': xxxxx@xxx.com      #请求输入用户名
Password for 'https://xxxxx@xxx.com@gitee.com':      #请求输出密码
From https://gitee.com/talent-chain/hr-contracts
 * branch            branchName -> FETCH_HEAD
Already up-to-date.

(2)解决方案

1)执行命令,生成.gitconfig文件

$ git config --global credential.helper store

执行以上命令会在/root目录下生成一个.gitconfig的隐藏文件,查看一下

[root@Tracy ~]# ls -a            #查看root目录下文件(包含隐藏文件)
.   .bash_logout   .bashrc  .cshrc   .gitconfig  .pki .tcshrc
..  .bash_profile  .cache   .ethash  .pip        .pydistutils.cfg  .ssh
[root@Tracy ~]# cat .gitconfig   #查看.gitconfig文件内容
[credential]
	helper = store

2)编辑文件.gitconfig

[root@Tracy ~]# vi .gitconfig 

在文件头部增加下面内容(加[user]部分即可):

[user]
	name = username
	email = email
[credential]
	helper = store

按Esc退出编辑,输入:wq保存退出 

3)再次执行git pull或相关命令,生成.git-credentials文件

执行git pull(或之前执行的命令)时还会提示一次输入用户名密码,之后就不再需要了。

此时,再次查看root目录下的文件,此时多了一个.git-credentials文件,有了这个文件以后就不再一个请求输入用户名密码了

[root@Tracy ~]# ls -a 
.   .bash_logout   .bashrc  .cshrc   .gitconfig        .pip  .pydistutils.cfg  .ssh
..  .bash_profile  .cache   .ethash  .git-credentials  .pki  .tcshrc

3、git add . 执行完直接执行git reset --hard,add中从未被提交的文件(即新增文件)全部被删除了

(1)执行操作 

$ git add .

$ git reset --hard
Deletion of directory 'contract' failed. Should I try again? (y/n) n
HEAD is now at ece38f4 Initial commit

上面的操作导致新增文件全被删除,使用git reset --hard前请仔细思考再操作

(2)找回文件方法

 恢复文件

$ git fsck --full --unreachable --no-reflog
Checking object directories: 100% (256/256), done.
Checking objects: 100% (357/357), done.
unreachable blob a4f0f1b2b339bf255504bb6929f83a5476c19740
unreachable blob ba98e9463e9f44f63b3b23532070b3c78ecadb8e
unreachable blob f7eca1298a7febf048a7d66e8453f5471355695e
unreachable blob 45457832306f2459671dad6df2a5c4263089f19c
# 略去许多被删除的文件blob......

通过以下指令可优雅的查看文件内容 

git cat-file -p SHA  #SHA为blob对象的哈希值,即上面查看到unreachable blob后面的值

$ git cat-file -p 45457832306f2459671dad6df2a5c4263089f19c
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)

pragma solidity ^0.8.7;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}

可将查询的文件内容复制粘贴到文件中。

但是奇怪的是,.sol与.json文件都能查看文件内容,但是.go文件只能看到第一行,其它内容全看不到了,以下是一个.go文件的查看结果:

$ git cat-file -p f7eca1298a7febf048a7d66e8453f5471355695e
package tools

再查看下.go文件的大小,发现只是14,与上面第一行大小一致,这个是还原不了了,也很悲催了,大家要是有什么方法,给我留言哈

$ git cat-file -s f7eca1298a7febf048a7d66e8453f5471355695e
14

4、git pull 报错(github):fatal: unable to access 'https://github.com/xxxxx.git/': Failed to connect to github.com port 443: Timed out

(1)执行操作与错误信息

$ git pull origin main

错误信息 

fatal: unable to access 'https://github.com/xxxxx.git/': Failed to connect to github.com port 443: Timed out

(2)原因

原因未知,之前一直使用gitee,现在使用github,不知是否有关

(3)解决方案

1)取消代理(未成功)

$ git config --global --unset http.proxy
$ git config --global --unset https.proxy

取消代理后,再次git pull,仍然报原来的错误。 

2)解除验证(成功)

$ git config --global http.sslVerify "false"

再次git pull成功

5、git push报错(github):Logon failed, use ctrl+c to cancel basic credential prompt.

(1)执行操作与错误信息

输入2次用户密码,报错如下: 

$ git push origin main
Logon failed, use ctrl+c to cancel basic credential prompt.
remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.
remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information.
fatal: Authentication failed for 'https://github.com/tracyzhang1998/web3js-demo.git/'

(2)原因

以上第2次输入密码时应该为github上设置的token,即只有有权限的用户才能做push操作的验证。

(3)解决方案

1)生成token

登录github,点击右上角头像 ->  Settings -> Developer settings(在左侧菜单最下方)-> Personal access tokens -> click "Generate a personal access token"

 再次确认密码

 新建token,如下两图所示:

 

 2)保存token

  复制保存好这个token,关闭这个页面后token再也出不来了(如果没记住,重新生成即可)

3)token验证

再次git push,会输入2次用户名密码,2次用户名是一样的,第1次密码输入登录密码,第2次密码输入刚刚生成的token

下图是第1次输入的用户名密码

用户名密码输入正确,会提示:Logon failed, use ctrl+c to cancel basic credential prompt.再次输入第2次用户名密码,密码为token(第2次没有截图下来)

6、git push报错(github): error: src refspec main does not match any

(1)执行命令及错误信息

Tracy@DESKTOP-101 /f/web3js/github/tracyzhang1998.github.io (master)
$ git push origin main
error: src refspec main does not match any
error: failed to push some refs to 'https://github.com/tracyzhang1998/tracyzhang1998.github.io.git'

(2)原因

在github仓库中,分支名称为main,当本地clone仓库中分支名称默认为master,但是github上没有master,导致提交出错( add与commit时没有出错 )

(3)解决方案

重命名分支名称

Tracy@DESKTOP-101 /f/web3js/github/tracyzhang1998.github.io (master)
$ git branch -m master main     # 重命令分支

Tracy@DESKTOP-101 /f/web3js/github/tracyzhang1998.github.io (main)
$ git push origin main          # 再次push
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 8 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (7/7), 230.40 KiB | 9.60 MiB/s, done.
Total 7 (delta 0), reused 0 (delta 0)
To https://github.com/tracyzhang1998/tracyzhang1998.github.io.git
   e24e89b..90afdb0  main -> main

7、git clone报错(github): OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054

(1)执行命令及错误信息

Tracy@DESKTOP-101 /f/Golang/sl/contract
$ git clone https://github.com/tracyzhang1998/smartcontract.git
Cloning into 'smartcontract'...
fatal: unable to access 'https://github.com/tracyzhang1998/smartcontract.git/': OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054

(2)原因

服务器证书SSL没有经过第三方机构签署

(3)解决方案

$ git config --global http.sslVerify "false"

再次git clone成功。

8、.gitignore文件配置忽略文件不起作用

(1)问题

已提交过的配置文件,之后不想再提交

(2)解决方案

$ git update-index --assume-unchanged config/*

9、移除登录的git账户(windows10)

(1)问题

临时在某台电脑上使用的账户下载源码,下载后想将账户删除,不保留在这台电脑上

(2)解决方案

打开控制面板 -> 用户账户 -> 凭据管理器

找到git开头的协议,查看要删除的账户,点击“删除”即可,如下图所示

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值