Git/Github的基本操作

由于我之前已经安装好了Git,然后这里就不再叙述之前的相关创建账户的步骤了。直接记录一下如何在本地创建一个项目,并上传到github上面。
1、打开github官网,点击New Repository,新建一个仓库。填写项目信息,Create Repository ,现在完成了一个项目在github上面的构建。
2、我们需要在本地创建一个相同的项目,这个是和github上的是一一对应的,只不过一个是本地,一个是远程。

$ makdir ~/hello-world    //创建一个项目hello-world
$ cd ~/hello-world    //打开这个项目
$ git init    //初始化 
$ touch README
$ git add README   //更新README文件
$ git commit -m 'first commit'//提交更新,并注释信息“first commit”
$ git remote add origin git@github.com:defnngj/hello-world.git   //连接远程github项目,然后这里的地址你要选择一下看是用哪个。
$ git push -u origin master   //将本地项目更新到github项目上去

现在查看github上面的hello world 项目, 应该已经将本地中的README文件更新上来了。

关于可能出现的错误

1、如果在push的时候,发现github.com 无法access, 那么极有可能是你的github.com 没有ping通。
解决办法:修改hosts.txt文件。
我们需要在hosts的最后一行加上:
140.82.114.4 github.com 140.82.114.4 www.github.com

修改的方式不是直接打开C盘下到hosts.txt文件修改哦,而是先把这个hosts.txt 文件复制在桌面或者其他位置,改好了以后,使用管理员的身份打开终端,然后将该文件复制到hosts相应的目录下。
cd 桌面 \\这里根据自己的情况修改

copy hosts C:\Windows\System32\drivers\etc\hosts /y ## /y表示自动确认覆盖原文件否则要手动输入y进行确认

2、在执行
$ git push origin master

错误提示:error:failed to push som refs to.......

解决办法:

$ git pull origin master // 先把远程服务器github上面的文件拉下来,
$ git push -u origin master // 再执行给push 上去。
3、如果执行
$ git remote add origin https://github.com/findingsea/myRepoForBlog.git
出现错误:fatal: remote origin already exists

解决办法:

$ git remote rm origin

学习让我快乐,工作让我快乐。学习和工作都是为了更好的生活!
原文地址:https://www.cnblogs.com/xyuanzi/p/15684988.html