本地项目根目录下,建立本地git仓库
git init
将项目的所有文件添加到仓库中,想添加某个特定的文件,只需把.换成特定的文件名即可
git add .
将add的文件commit到仓库
git commit -m'备注'
将本地的仓库关联到你的github远程仓库
git remote add origin git@github.com:HelloVansal/test.git
代码上传github之前,要先pull一下
git pull origin master
上传代码到github远程仓库
git push -u origin master
一些报错归类
fatal: remote origin already exists 远程仓库已存在
先删除远程 Git 仓库
git remote rm origin
再添加远程 Git 仓库
git remote add origin git@github.com:HelloVansal/test.git
如果执行 git remote rm origin 报错的话,我们可以直接删除本地的.git文件夹重新执行一遍git init和后面的代码
error: failed to push some refs to 'git@github.com:HelloVansal/test.git'
在建立github远程仓库时,不要打勾下面这句话,
Initialize this repository with a README
This will let you immediately clone the repository to your computer. Skip this step if you’re importing an existing repository.
这句话意思是,用一个readme初始化这个仓库
他会使你可以立即克隆这个仓库当你的电脑,如果你要导入一个已存在的工程请跳过他
所以如果你的仓库已建立,就不要打勾
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
fatal: Could not read from remote repository.Please make sure you have the correct access rights and the repository exists.
出现这个问题是因为没有在github账号添加SSH key
解决方法如下:
1.在终端输入。
ssh-keygen -t rsa -C "username" (注:username为你git上的用户名),一直按回车即可
SSH key就生成了。文件目录就是:/Users/username/.ssh/id_rsa.pub.
将id_rsa.pub.中的内容添加到github账户设置里边的SSH keys即可,就可以执行接下来的其他命令了