用git与github建立连接

1.注册账户以及创建仓库

要想使用github第一步当然是注册github账号了。之后就可以创建仓库了(免费用户只能建公共仓库),Create a New Repository,填好名称后Create

2.安装客户端Git

github是服务端,要想在自己电脑上使用git我们还需要一个git客户端, 在本地仓库选择git init here 会多出一个 .git文件夹,右键Git Bash进入git命令行,为了把本地的仓库传到github,还需要配置ssh key

3.配置Git

(1) 首先在本地创建ssh key;

$ ssh-keygen -t rsa -C “[email protected]” 注意ssh-keygen命令中间没有空格,如果在ssh后面加上空格, 会得到Bad escape character ‘ygen’.的错误。 后面的[email protected]改为你的邮箱,之后会要求确认路径和输入密码, 我们这使用默认的一路回车就行。 成功的话会在~/下生成.ssh文件夹,进去,打开id_rsa.pub,复制里面的key。 回到github,进入Account Settings,左边选择SSH Keys,Add SSH Key,title随便填,粘贴key。

(2) 为了验证是否成功,在git bash下输入: $ ssh -T [email protected]
如果是第一次的会提示是否continue,输入yes就会看到:You’ve successfully authenticated, but GitHub does not provide shell access 。这就表示已成功连上github。
(3) 接下来我们要做的就是把本地仓库传到github上去,在此之前还需要设置username和email,因为github每次commit都会记录他们。
gitconfig−−globaluser.name”yourname” git config –global user.email “[email protected]

4. 提交上传

(1) 接下来在本地仓库里添加一些文件,比如README,
gitaddREADME git add README g i t c o m m i t m f i r s t c o m m i t ( 2 ) g i t h u b : git push origin master
git push命令会将本地仓库推送到远程服务器。
git pull命令则相反。
修改完代码后,使用git status可以查看文件的差别,使用git add 添加要commit的文件,也可以用git add -i来智能添加文件。之后git commit提交本次修改,git push上传到github。

5.一些常见错误

我用git add file添加文件时出现这样错误:
fatal: Not a git repository (or any of the parent directories): .git
提示说没有.git这样一个目录,解决办法如下:
git init就可以了!
推送本地库到远程库的命令:
git push origin master origin/mast

猜你喜欢

转载自blog.csdn.net/qq_16122415/article/details/81531563