github怎么设置本地push代码

从github拉取公开代码使用git pull即可
但是如果想要push则会报错

  1. clone代码
    本次使用个人测试的公开代码
    因为是公开代码无需验证即可clone
# git clone https://github.com/liuyueming/salt.git

修改代码提交即使输入的用户名和密码正确也会报错

# git add test 
[root@docker salt]# git commit -m "change"
[master e9f9832] change
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@docker salt]# git push
warning: push.default 未设置,它的默认值将会在 Git 2.0 由 'matching'
修改为 'simple'。若要不再显示本信息并在其默认值改变后维持当前使用习惯,
进行如下设置:

  git config --global push.default matching

若要不再显示本信息并从现在开始采用新的使用习惯,设置:

  git config --global push.default simple

参见 'git help config' 并查找 'push.default' 以获取更多信息。
('simple' 模式由 Git 1.7.11 版本引入。如果您有时要使用老版本的 Git,
为保持兼容,请用 'current' 代替 'simple' 模式)

Username for 'https://github.com': 274670459@qq.com
Password for 'https://274670459@qq.com@github.com': 
fatal: Authentication failed for 'https://github.com/liuyueming/salt.git/'

原因是github不支持密码验证
在windows git终端有明确报错

Logon failed, use ctrl+c to cancel basic credential prompt.
Username for 'https://github.com': 274670459@qq.com
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/liuyueming/salt.git/'
  1. 设置成ssh验证
    在客户端生成ssh密钥
    一直回车下一步即可
# ssh-keygen

把公钥复制至github
是已pub结尾的文件

# cat .ssh/id_rsa.pub 

image
在客户端测试是否生效
出现以下提示代表密码设置成功

#  ssh -T git@github.com
Hi liuyueming! You've successfully authenticated, but GitHub does not provide shell access.
  1. 测试push代码
    再次clone

注意:需要使用ssh格式不能使用https格式

# git clone git@github.com:liuyueming/salt.git

修改后可以免密push使用的是ssh端口

# git add test 
[root@docker salt]# git commit -m "add"
[master 1a03d36] add
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@docker salt]# git push
warning: push.default 未设置,它的默认值将会在 Git 2.0 由 'matching'
修改为 'simple'。若要不再显示本信息并在其默认值改变后维持当前使用习惯,
进行如下设置:

  git config --global push.default matching

若要不再显示本信息并从现在开始采用新的使用习惯,设置:

  git config --global push.default simple

参见 'git help config' 并查找 'push.default' 以获取更多信息。
('simple' 模式由 Git 1.7.11 版本引入。如果您有时要使用老版本的 Git,
为保持兼容,请用 'current' 代替 'simple' 模式)

Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 276 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: To git@github.com:liuyueming/salt.git
   ff5cfac..1a03d36  master -> master
posted @ 2024-06-21 17:29  minseo  阅读(3)  评论(0编辑  收藏  举报