git
什么是git
git是一款源代码管理工具(版本控制工具)
工作区、暂存区、版本库的概念
git的安装
git的打开
- 打开项目文件夹,右键鼠标选择Git Bash Here
初始化git仓库
- 这个仓库会管理我们备份的代码
git init
配置备份人员信息
git config --global user.name "倪成景"
git config --global user.email "chengjing.ni@hotmail.com"
把代码存储到.git仓库中
-
存储单个文件
git add git commit -m "此次操作的描述"
-
存储全部的文件
git add ./ git commit --all -m "这次是全部文件一次提交"
查看当前的状态
git status
设置git中需要忽略的文件
- 在工作区创建'.gitignore'文件,创建时输入'.gitignore.' ,在.gitignore文件中输入要忽略的文件,以'/'开头,红色表示在工作区,绿色表示在暂存区,无则表示都在版本库
查看日志
git log //查看详细历史提交的日志
git log --oneline //简要信息(主用)
回退到以前版本
git reset --hard Head~0
git reset --hard 版本号
git reflog 查看版本切换的记录
分支
默认有一个主分支master,已经完成的代码放在master分支上,未完成的代码放在自己创建的分支上
-
创建分支
git branch dev//此时分支的内容和master是一样的
-
查看分支
git branch//可以查看当前有哪些分支,带*的分支就是现在的用到的分支
-
切换分支
git checkout dev//切换到名为dev的分支
-
改变并存储代码
git add ./ git commit --all -m "第一次提交"
-
切换到master
git checkout master
-
合并分支
git merge dev
-
删除分支
git branch -d dev
冲突
-
什么情况下会发生冲突
master上项目版本是1.1,此时创建分支dev,在dev里对index.html进行了修改,存储到仓库,此时版本是1.2,切换到master,也对index.html进行了修改,此时版本也是1.2,然后合并master和dev,会有冲突,因为存在版本相同的两个项目。 -
手动处理冲突
在master上或dev上再修改一次版本到1.3
报错1
- 如里有报错误:
To git@git.oschina.net:yangzhi/hello.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@git.oschina.net:yangzhi/hello.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushin
hint: to the same ref. You may want to first merge the remote changes (e.g.
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
-可以输入git push -f
https方式使用github
-
代码上传到github上
git push https://github.com/chengjingni/test1.git master
-
得到github上的代码
git pull https://github.com/chengjingni/test1.git master
SSH方式使用github
SSH和https的区别
-
前者可以随意克隆github上的项目,而不管是谁的;而后者则是你必须是你要克隆的项目的拥有者或管理员,且需要先添加 SSH key ,否则无法克隆。
-
https url 在push的时候是需要验证用户名和密码的;而 SSH 在push的时候,是不需要输入用户名的,如果配置SSH key的时候设置了密码,则需要输入密码的,否则直接是不需要输入密码的。
检查电脑是否已经有 SSH key
$ cd ~/.ssh
$ ls
这两个命令就是检查是否已经存在 id_rsa.pub 或 id_dsa.pub 文件,如果文件已经存在,那么你可以跳过下一步骤。
创建一个 SSH key
$ ssh-keygen -t rsa -C "chengjing.ni@hotmail.com"
代码参数含义:
- -t 指定密钥类型,默认是 rsa ,可以省略。
- -C 设置注释文字,比如邮箱。
- -f 指定密钥文件存储文件名。
以上代码省略了 -f 参数,因此,运行上面那条命令后会让你输入一个文件名,用于保存刚才生成的 SSH key 代码,如:
Generating public/private rsa key pair.
# Enter file in which to save the key (/c/Users/you/.ssh/id_rsa): [Press enter]
当然,你也可以不输入文件名,使用默认文件名(推荐),那么就会生成 id_rsa 和 id_rsa.pub 两个秘钥文件。
接着又会提示你输入两次密码(该密码是你push文件的时候要输入的密码,而不是github管理者的密码),
当然,你也可以不输入密码,直接按回车。那么push的时候就不需要输入密码,直接提交到github上了,如:
Enter passphrase (empty for no passphrase):
# Enter same passphrase again:
接下来,就会显示如下代码提示,如:
Your identification has been saved in /c/Users/you/.ssh/id_rsa.
# Your public key has been saved in /c/Users/you/.ssh/id_rsa.pub.
# The key fingerprint is:
# 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com
当你看到上面这段代码的收,那就说明,你的 SSH key 已经创建成功,你只需要添加到github的SSH key上就可以了。
添加SSH key到github上
-
首先你需要拷贝 id_rsa.pub 文件的内容,你可以用编辑器打开文件复制,也可以用git命令复制该文件的内容,如:
$ clip < ~/.ssh/id_rsa.pub
-
登录你的github账号,从又上角的设置( Account Settings )进入,然后点击菜单栏的 SSH key 进入页面添加 SSH key。
-
点击 Add SSH key 按钮添加一个 SSH key 。把你复制的 SSH key 代码粘贴到 key 所对应的输入框中,记得 SSH key 代码的前后不要留有空格或者回车。当然,上面的 Title 所对应的输入框你也可以输入一个该 SSH key 显示在 github 上的一个别名。默认的会使用你的邮件名称。
测试一下该SSH key
在git Bash 中输入以下代码
$ ssh -T git@github.com
当你输入以上代码时,会有一段警告代码,如:
The authenticity of host 'github.com (207.97.227.239)' can't be established.
# RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
# Are you sure you want to continue connecting (yes/no)?
这是正常的,你输入 yes 回车既可。如果你创建 SSH key 的时候设置了密码,接下来就会提示你输入密码,如:
Enter passphrase for key '/c/Users/Administrator/.ssh/id_rsa':
当然如果你密码输错了,会再要求你输入,知道对了为止。
注意:输入密码时如果输错一个字就会不正确,使用删除键是无法更正的。
密码正确后你会看到下面这段话,如:
Hi username! You've successfully authenticated, but GitHub does not
# provide shell access.
如果用户名是正确的,你已经成功设置SSH密钥。如果你看到 “access denied” ,者表示拒绝访问,那么你就需要使用 https 去访问,而不是 SSH 。
验证SSH方式上传下拉文件
以SSH方式push文件夹01到github后再pull到文件夹02上,将文件夹02的内容修改后以https的方式push到github上再pull到文件夹03上,发现内容和文件夹01相同。因此在SSH方式下,SSH相当于会员,能够修改访问github上的文件,而https只能访问。
git push git@github.com:chengjingni/SSH-.git master
git pull git@github.com:chengjingni/SSH-.git master
git add ./
git commit --all -m "文件夹02修改提交"
git push https://github.com/chengjingni/SSH-.git master
git pull https://github.com/chengjingni/SSH-.git master