Git与Github的使用

来源:互联网 发布:淘宝扶持政策 编辑:程序博客网 时间:2024/06/09 07:23

Git与Github的连接使用


git的使用参考文章:http://blog.jobbole.com/78960/
待学习git的使用,文件的更新,同步等


git是一个分布式版本控制系统 存在于客户端
github是一个用git做版本控制的项目托管平台。服务器


一、注册账户以及创建仓库
不详细讲解


二、安装git客户端
windows:http://msysgit.github.com/
安装过程可以参考:http://blog.csdn.net/renfufei/article/details/41647875/
安装完成后,在文件夹中点鼠标右键会多出一些菜单
如 Git Bash、Git Gui , 说明安装成功


三、配置Git

1 第一次在本地仓库操作需要先执行git init命令

$ git init

你要同步的仓库处用Git Bash Here 打开命令行,输入,Git init
再点击文件夹显示隐藏文件 就可以看到.git后缀的文件夹了


2 为了把本地的仓库传到github,还需要配置SSHKey

2.1在本地创建ssh key

$ ssh-keygen -t rsa -C "your_email@youremail.com"

输入你的邮箱

然后说明会在默认文件id_rsa上生成ssh key,直接点回车

然后系统要求输入密码,输入密码时不会显示密码
然后重复输入密码,也是不会显示密码

之后创建成功,会提示:

The key's randomart image is:

在存放key 的文件夹下,会有id_rsa.pub和id_rsa的文件
id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥


2.2
打开id_rsa.pub,复制,进入到github官网, 点击Settings,左边选择SSH Keys,New SSH Key
tietle填sshkey1
key填你复制的内容


2.3 验证是否成功,在git bash下输入

ssh -T git@github.com

可能会出现:
*The authenticity of host ‘github.com (192.30.255.112)’ can’t be established.
RSA key fingerprint is*
原因是: 在存放key的文件夹下,缺失known_hosts文件,直接输入yes,然后输入你的密码即可
出现:

Hi 5ingwings! You've successfully authenticated, but GitHub does not provide shell access.

就成功了


3 接下来我们要做的就是把本地仓库传到github上去,在此之前还需要设置username和email,因为github每次commit都会记录他们
这是global参数,有了这个参数,表示你这台机器上所有的Git仓库都会使用这个配置,当然你也可以对某个仓库指定的不同的用户名和邮箱
name可以为github上的名 只要有标识性即可

$ git config --global user.name "your name"$ git config --global user.email "your_email@youremail.com"

成功会显示

SiHao@SiHao-PC MINGW64 /g/Git/MyRepository (master)

要修改直接再重写执行一遍上面的代码即可(至少我的是这样,如果有的人改不了,可以参考:http://blog.csdn.net/sky_miange/article/details/60881282)


4 进入本地仓库(创建了.git文件的目录),右键git bash,添加远程地址

$ git remote add origin git@github.com:yourName/yourRepo.git

后面的yourName和yourRepo表示你在github的用户名和步骤一新建的仓库,加完之后进入.git,打开config,这里会多出一个remote “origin”内容,这就是刚才添加的远程地址,也可以直接修改config来配置远程地址
成功后没有提示


5 提交上传
5.1 使用命令 git add XXX 添加到暂存区里面去

git add 要添加的文件名

没有任何提示,说明已经添加成功了


5.2 用命令 git commit告诉Git,把文件提交到仓库

git commit -m "first commit"

first commit 为提交的注释

成功会
XX files changed
createXXX
createXXX



5.3 我们可以通过
git status
查看是否还有文件未提交或者更新了未被提交


5.3 上传到github

git push -u origin master

git push命令会将本地仓库推送到远程服务器。实际上是把当前分支master推送到远程
由于远程库是空的,我们第一次推送master分支时,加上了 –u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令

git pull命令则相反。

注意:若你在创建github仓库时,勾选了创建README文件,则需要先把文件同步到本地,再上传提交

git pull --rebase origin master

这时,你的本地就会有README文件

再去用git push -u origin master上传


5.4 从现在起,只要本地作了提交,就可以通过如下命令:

git push origin master

把本地master分支的最新修改推送到github上了


6 新建文件
若新建了文件
使用git status
会出现:

$ git statusOn branch masterUntracked files:  (use "git add <file>..." to include in what will be committed)        test.txt

git add test.txt

去加入文件到暂存区
再用命令 git commit告诉Git,把文件提交到仓库

git commit -m "test commit"

最后上传到github上

这里写代码片

7 内容修改上传
7.1通过git status 查看是否有修改了未上传的
若出现

On branch masterChanges not staged for commit:  (use "git add <file>..." to update what will be committed)  (use "git checkout -- <file>..." to discard changes in working directory)        modified:   test.txtno changes added to commit (use "git add" and/or "git commit -a")

则说明有文件修改了但是没有上传到暂存区
7.2 查看修改了但没有上传到暂存区的文件

git diff test.txt

7.3 将文件添加到暂存区

git add  test.txt

7.4 commit上传到仓库

git commit -m "test commit2"

7.5 添加到远程仓库

git push origin master

常见错误ERROR:
1 git push origin master时,出现

ERROR: Repository not found.fatal: Could not read from remote repository.Please make sure you have the correct access rightsand the repository exists.

解决:
1 用户名或者仓库写错
git remote add origin git@github.com:yourName/repositoryName.git

2 git remote 了 , 却说已经exists了
2.1 可能是你的仓库只有read权限,没有read/write权限 去修改权限
3 可能是你的github的文件没有同步到仓库中,要先pull出来,再push进去


参考:
http://blog.jobbole.com/78960/
http://1ke.co/course/194