[干货]使用Git命令将本地项目上传至Github

更新

2022.11.18
文章更新内容:
1.github上创建的git branch的名字是main,不是原来的master, 第6步和第7步

git pull --rebase origin main

git push -u origin main

2.提交时需要使用PAT(personal access token)验证,不是之前的用户名加密码

注意,在提交本地项目时,如果使用用户名和密码会有如下提示

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/wayne214/componentization_demo/'

根据提示,在github官网添加pat, 之后使用用户名和pat就可以正常提交了

附:创建Pat链接

3.如遇报错error: src refspec main does not match any
使用如下命令解决:
$ git branch -M main

开发步骤

其实就几个步骤:
1.在电脑上创建一个新的文件夹
2、(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库

git init

3、把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点“.”,意为添加文件夹下的所有文件

git add .

4、用命令 git commit告诉Git,把文件提交到仓库。引号内为提交说明

git commit -m 'first commit'

5、关联到远程库(这里我用的是HHS协议)

git remote add origin 你的远程库地址

如:git remote add origin git@github.com:wayne214/demo.git

如果执行到这步提示出错信息:fatal: remote origin already exists
解决办法:

1、先输入$git remote rm origin 
2、再输入 $git remote add origin git@github.com:lily000000/demo.git 就不会报错了!

6、获取远程库与本地同步合并(如果远程库不为空必须做这一步,否则后面的提交会失败)

git pull --rebase origin master

7、把本地库的内容推送到远程,使用 git push命令。

git push -u origin master

总结

新建项目
echo "# android_demos" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/wayne214/android_demos.git
git push -u origin main
已有项目
git remote add origin https://github.com/wayne214/android_demos.git
git branch -M main
git push -u origin main

欢迎关注订阅公号[君伟说],备注“技术交流”,邀请入群

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值