准备
现在github创建仓库,都是默认main分支了,好多教程都还是master分支,这里可以使用
git branch branch_name ----创建分支
git checkout branch_name -----切换分支
步骤
1、设置username和email,因为github每次commit都会记录他们
输入以下两个命令:
$ git config --global user.name “your name”(注册时填写的名字)
$ git config --global user.email “email@qq.com”(自己的邮箱)
2、开始建立连接:
git remote add origin git@github.com:yourName/yourRepo.git
yourName和yourRepo表示你再github的用户名和刚才新建的仓库,加完之后进入.git
如何使用git上传代码到github
如果出现了以下情况fatal: remote origin already exists.说明以前已经连接过此项目,此时需要输入命令:git remote rm origin(移除连接,一般新建的仓库不会出现此问题),然后重新连接
3、在要上传的文件夹下,右键git bash here,然后输入git init。再输入:git add test.txt (将test文件上传,如果需要上传所有文件请输入:
git add . )
4、如何使用git上传代码到github
开始提交刚才添加的文件,输入:
git commit -m “first commit” (会看到这里列出了所有你需要上传的文件)
这里注意:
如果一开始没成功,第二次尝试的时候,不要还输入git commit -m “first commit” ,如果一样的话,可能什么都不会上传。换一个,比如git commit -m “second commit”
5、最后使用git push命令,将本地仓库推送到远程服务器
输入:git push origin master