记录一下git的配置和用法,以及连接github的具体操作步骤。
git介绍
Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。
Git 与 SVN 区别点:
- Git 是分布式的,SVN 不是:这是 Git 和其它非分布式的版本控制系统,例如 SVN,CVS 等,最核心的区别。
- Git 把内容按元数据方式存储,而 SVN 是按文件:所有的资源控制系统都是把文件的元信息隐藏在一个类似 .svn、.cvs 等的文件夹里。
- Git 分支和 SVN 的分支不同:分支在 SVN 中一点都不特别,其实它就是版本库中的另外一个目录。
- Git 没有一个全局的版本号,而 SVN 有:目前为止这是跟 SVN 相比 Git 缺少的最大的一个特征。
- Git 的内容完整性要优于 SVN:Git 的内容存储使用的是 SHA-1 哈希算法。这能确保代码内容的完整性,确保在遇到磁盘故障和网络问题时降低对版本库的破坏。
git安装
window平台直接下载安装包安装。
下面说一下linux平台安装,以CentOS为例,直接安装方式如下。
#安装依赖包 yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel #安装git yum -y install git-core
使用下载的源码安装。
#安装依赖包 yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel #解压 tar -zxf git-2.2.5.tar.gz cd git-2.2.5 #编译 make prefix=/usr/local all sudo make prefix=/usr/local install
git配置
Git 提供了一个叫做 git config 的工具,专门用来配置或读取相应的工作环境变量。
这些环境变量,决定了 Git 在各个环节的具体工作方式和行为。这些变量可以存放在以下三个不同的地方:
- /etc/gitconfig 文件:系统中对所有用户都普遍适用的配置。若使用 git config 时用 –system 选项,读写的就是这个文件。
- ~/.gitconfig 文件:用户目录下的配置文件只适用于该用户。若使用 git config 时用 –global 选项,读写的就是这个文件。
- 当前项目的 Git 目录中的配置文件(也就是工作目录中的 .git/config 文件):这里的配置仅仅针对当前项目有效。每一个级别的配置都会覆盖上层的相同配置,所以 .git/config 里的配置会覆盖 /etc/gitconfig 中的同名变量。
在 Windows 系统上,Git 会找寻用户主目录下的 .gitconfig 文件。主目录即 $HOME 变量指定的目录,一般都是 C:\Users\当前用户。
基本用法如下
#配置个人的用户名称和电子邮件地址 git config --global user.name "runoob" git config --global user.email test@runoob.com #查看已有的配置信息 git config --list #查询单个配置信息 git config user.name
如果用了 –global 选项,那么更改的配置文件就是位于用户主目录下的那个,以后所有的项目都会默认使用这里配置的用户信息。
如果要在某个特定的项目中使用其他名字或者电邮,只要去掉 –global 选项重新配置即可,新的设定保存在当前项目的 .git/config 文件里。
git工作流程
- 克隆 Git 资源作为工作目录。
- 在克隆的资源上添加或修改文件。
- 如果其他人修改了,你可以更新资源。
- 在提交前查看修改。
- 提交修改。
- 在修改完成后,如果发现错误,可以撤回提交并再次修改并提交。
git基础操作命令
//git克隆到本地 git clone https://github.com/yumefx/test.git //使用账号clone git clone https://yumefx:123456@github.com/yumefx/test.git //如果账号为邮箱地址,特殊字符@要用%40来代替 git clone https:// yumefx%40gmail.com:123456@github.com/yumefx/test.git //建立本地仓库 git init //将所有更改的内容 添加到缓冲区 git add . //查看缓冲区状态 git status //确认提交 且提交评论为test ssh git commit -m "test ssh" //连接到远程仓库 git remote add origin git@github.com:yumefx/test.git //将本地的内容提交到github上去 git push origin master
git分支管理
几乎每一种版本控制系统都以某种形式支持分支。使用分支意味着你可以从开发主线上分离开来,然后在不影响主线的同时继续工作。
列出分支
git branch
没有参数时,git branch 会列出你在本地的分支。
当你执行 git init 的时候,默认情况下 Git 就会为你创建 master 分支。
如果我们要手动创建一个分支。执行 git branch (branchname) 即可。
切换分支
git checkout [branchname]
我们也可以使用 git checkout -b (branchname) 命令来创建新分支并立即切换到该分支下,从而在该分支中操作。
删除分支
git branch -d [branchname]
分支合并
git merge
其他操作
添加远程库
要添加一个新的远程仓库,可以指定一个简单的名字,以便将来引用,命令格式如下:
git remote add [shortname] [url]
同理查看当前的远程库就不用加参数:
git remote
提取远程仓库
Git 有两个命令用来提取远程仓库的更新。
1、从远程仓库下载新分支与数据:
git fetch
该命令执行完后需要执行git merge 远程分支到你所在的分支。
2、从远端仓库提取数据并尝试合并到当前分支:
git merge
该命令就是在执行 git fetch 之后紧接着执行 git merge 远程分支到你所在的任意分支。
假设你配置好了一个远程仓库,并且你想要提取更新的数据,你可以首先执行 git fetch [alias] 告诉 Git 去获取它有你没有的数据,然后你可以执行 git merge [alias]/[branch] 以将服务器上的任何更新(假设有人这时候推送到服务器了)合并到你的当前分支。
推送到远程仓库
推送你的新分支与数据到某个远端仓库命令:
git push [alias] [branch]
以上命令将你的 [branch] 分支推送成为 [alias] 远程仓库上的 [branch] 分支,实例如下。
删除远程仓库
删除远程仓库你可以使用命令:
git remote rm [别名]
ssh方式使用github
首先是安装git,然后打开gitbash输入以下命令,生成密钥,默认输出路径为C:\Users\当前用户\.ssh。
ssh-keygen -t rsa
将id_rsa.pub公钥的内容添加到 GitHub 上,这样本地的 id_rsa 私钥跟 GitHub上的 id_rsa.pub 公钥进行配对,授权成功就可以提交代码。
如果要验证是否成功,可以输入以下命令:
ssh -T git@github.com // Hi yumefx! You've successfully authenticated, but GitHub does not provide shell access.
https方式记住账号和密码
全局记住账号和密码
用户目录下的这两个文件增加以下内容
.gitconfig
[user] name = yumefx email = yumefx@163.com [credential] helper = store
.git-credentials
https://yumefx:123456@github.com
针对单个仓库的存储账户密码
.git文件夹内的config文件的[remote “origin”]栏的url改为https://用户名:密码@git地址。
[remote "origin"] url = https://yumefx:123456@github.com/yumefx/test
你原以为自己是风筝,
剪断线后,
发现自己真的是只鸟。
——连岳
评论
Hello there! I know this is kinda off topic but I was wondering if you knew where I could find a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having difficulty finding one? Thanks a lot!
https://youtu.be/eDbbOWUZvIU
What Is Sugar Defender? Sugar Defender is made of natural plant-based ingredients and minerals that support healthy blood sugar levels.
https://youtu.be/icXq1eGswRY
You are my breathing in, I have few blogs and sometimes run out from to post .
https://youtu.be/daFaOzvbsNI
I have recently started a website, the information you offer on this website has helped me tremendously. Thanks for all of your time & work. “There can be no real freedom without the freedom to fail.” by Erich Fromm.
https://www.axilusonline.com/hire-a-hacker-for-social-media/
I like this site so much, saved to fav.
https://www.oraclemobilesecurity.com/hire-a-hacker-for-roblox/
you might have an excellent blog here! would you like to make some invite posts on my blog?
https://youtu.be/UKNdv9kDl2E
whoah this blog is magnificent i love reading your posts. Keep up the great work! You know, a lot of people are looking around for this information, you could aid them greatly.
https://youtu.be/NynRendidYY
Thanks a lot for sharing this with all of us you really know what you’re talking about! Bookmarked. Please also visit my web site =). We could have a link exchange arrangement between us!
https://youtu.be/7CYwxrHhpOM
Thanks for the sensible critique. Me & my neighbor were just preparing to do some research about this. We got a grab a book from our area library but I think I learned more from this post. I’m very glad to see such excellent information being shared freely out there.
https://youtu.be/JVKQ32MPpts
Definitely imagine that that you stated. Your favorite reason appeared to be on the internet the simplest thing to be mindful of. I say to you, I certainly get irked even as other people consider worries that they plainly do not recognize about. You managed to hit the nail upon the top and also defined out the whole thing with no need side effect , other people can take a signal. Will probably be back to get more. Thank you
https://youtu.be/RMfjaV0vkyQ
Lovely just what I was searching for.Thanks to the author for taking his time on this one.
https://youtu.be/ybiqBIibPHw
At this time it looks like Drupal is the best blogging platform out there right now. (from what I’ve read) Is that what you are using on your blog?
https://youtu.be/CroD1bbzmyQ
Hey just wanted to give you a quick heads up. The words in your content seem to be running off the screen in Ie. I’m not sure if this is a formatting issue or something to do with web browser compatibility but I thought I’d post to let you know. The style and design look great though! Hope you get the problem fixed soon. Thanks
https://www.tdsky.com/hire-a-hacker/