Git 添加远程库最新教程与排错


首先登录GitHub,点击“New repository”,

然后输入仓库名称,我这里写“test”,其他默认即可,点击“Create repository”,

现在,新的远程仓库已经创建好了,但是还是空的,GitHub告诉我们:可以从这个仓库克隆出心得仓库,也可以把一个已有的本地仓库与之关联,然后,把本地仓库的内容推送到GitHub仓库。

我们根据GitHub的提示,在本地新建test仓库并且关联

[root@Myhost home]# mkdir test
[root@Myhost home]# cd test/
[root@Myhost test]# echo "# test" >> README.md
[root@Myhost test]# git init
Initialized empty Git repository in /home/test/.git/
[root@Myhost test]# git add README.md
[root@Myhost test]# git commit -m "first commit"
[master (root-commit) 86c11dc] first commit
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 README.md
[root@Myhost test]# git remote add origin https://github.com/username/test.git
[root@Myhost test]# git push -u origin master
error: The requested URL returned error: 403 Forbidden while accessing https://github.com/username/test.git/info/refs

fatal: HTTP request failed

到这一步出现错误,我添加了很多遍,都是这个错误,提示403打不开远程仓库链接。

解决方案:

[root@Myhost test]# vim .git/config

把  url = https://github.com/username/test.git 改为
    url = https://username@github.com/username/test.git

[root@Myhost test]# cat .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = https://username@github.com/username/test.git
        fetch = +refs/heads/*:refs/remotes/origin/*

也就是GitHub提供的远程仓库链接是错误的,只需要改为正确的链接("github.com"前面加上"你的用户名@"即可),保存退出,重新提交,输入密码:

[root@Myhost test]# git push -u origin master
Password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 221 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://username@github.com/username/test.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

再去GitHub上刷新test仓库,就能看到本地文件已经同步了

Done...

本文为本站原创,转载请注明出处: Git 添加远程库最新教程与排错 


I only do what I like, and this is ideal life.