笔记内容
学习笔记-段玉磊
Github
Github 命令
写这篇文章主要写一下如何运用终端命令,进行Git的配置以及使用,由于本人我不太习惯使用图形IDE,效率没有命令行高,我还是推荐使用命令行进行Git的使用。
简单介绍
简要说一下Git,Git只是一个版本控制工具,举例来说,我之前写一个多核归并算法,将文件保存命名为MergeSort.c,由于发现一个Bug,将一段段代码注释,显得很糟乱,之后就把自认为错误的代码删掉,保存后,发现之前删掉的代码没有错误,想返回的时候却没有办法回去了。这就要用到Git了。
如果想要删代码的时候,先把代码上传一下,通过 git push
,之后就算把代码全部删掉,也可以通过Git返回上一个时间点,如果修改多次,返回第一次修改的时间点的代码,也可以顺利的看到。
由于Git需要一个服务器,进行上传或者下载代码,因此,Github就是一个免费服务器,注册账户后就可以通过Github的IDE管理代码,并且清晰看到每个git commit
后显示修改的代码。
GitHub的配置
安装并且配置 Git
首先去官方网站下载GitHub App,我是MAC用户,MAC的GitHub 选项中可以自动安装 Git
和 osxkeychain helper
,如果是Linux可能需要手动安装 Git
和 osxkeychain helper
.
如果没有安装Git,就通过官方下载Git版本管理工具,下载并且安装最新版本的Git.
安装好后通过以下命令来配置Git。下面的命令是我用我的用户名和邮箱进行设置。只配置一次就够了,当然不会每次配置用户名密码这么麻烦。
$ git config --global user.name "YuLei"
$ git config --global user.email "suiyun@me.com"
如果是MAC,利用IDE自动装好Git帮助选项,就不需要通过命令行安装osxkeychain
,上面命令配置好后,安装osxkeychain
,通过以下命令:
$ git credential-osxkeychain
# Test for the cred helper
git: 'credential-osxkeychain' is not a git command. See 'git --help'.
$ curl -s -O \
https://github-media-downloads.s3.amazonaws.com/osx/git-credential-osxkeychain
# Download the helper
$ chmod u+x git-credential-osxkeychain
# Fix the permissions on the file so it can be run
$ sudo mv git-credential-osxkeychain \
"$(dirname $(which git))/git-credential-osxkeychain"
# Move the helper to the path where git is installed
Password: [enter your password]
$ git config --global credential.helper osxkeychain
# Set git to use the osxkeychain credential helper
这样就配置好GitHub了。
GitHub 的基本使用
首先通过Git,新建一个项目,并且提交到Github上面去,现在IDE创建一个repository,然后找到giturl,通过 git clone
下载到本地。
$ git clone https://github.com/dyulei/blog.git
# 克隆到本地,但是无法上传更改,需要权限
$ mkdir Github & cd Github
# 创建在本地管理远程项目的目录
$ touch README.md
# 创建一个文件,由于Github需要一个说明文件
$ git add .
# 增加要同步的目录中的所有文件
$ git commit -m 'first commit'
$ git commit -a
# -m是手动添加, -a 自动添加注释上传更新
$ git remote add Blog https://github.com/dyulei/blog.git
# 远程服务器别名:Blog 上传的地址:https://github.com/dyulei/blog.git
$ git pull -u Blog master
# 将服务器数据下载到本地文件夹
$ git push -u Blog master
# 将服务器数据上传到远程服务器
$ git rm README.md
# 从本地库中删除文件
关于分支版本的操作,创建并且合并分支,由于Git很多时候是一个团队在用,每个人都负责一小部分,最后合成一大部分,所以就要通过分支控制,最后进行合并。当然下面的代码是最容易的部分,GitHub具有相当强大的功能在分支版本控制上面。
$ git branch
# 显示当前分支是master
$ git branch new-feature
# 创建分支
$ git checkout new-feature
# 切换到新分支中
$ git add sort.c
$ git commit -a
$ git push origin new-feature
# 将分支结构提交到远程服务器,但是没有和主干进行合并
$ git checkout master
# 切换的主干中去
$ git merge new-feature
# 把分支合并到主干中去
$ git branch
$ git push
# 此时主干和分支已经合并
About Me
微博
:@Levi-小磊邮箱
:suiyun@me.comGitHub
: https://github.com/dyulei
@%28%u5B66%u4E60%u7B14%u8BB0-%u6BB5%u7389%u78CA%29%5BGithub%5D%0AGithub%20%u547D%u4EE4%0A%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%0A%0A%3E%u5199%u8FD9%u7BC7%u6587%u7AE0%u4E3B%u8981%u5199%u4E00%u4E0B%u5982%u4F55%u8FD0%u7528%u7EC8%u7AEF%u547D%u4EE4%uFF0C%u8FDB%u884CGit%u7684%u914D%u7F6E%u4EE5%u53CA%u4F7F%u7528%uFF0C%u7531%u4E8E%u672C%u4EBA%u6211%u4E0D%u592A%u4E60%u60EF%u4F7F%u7528%u56FE%u5F62IDE%uFF0C%u6548%u7387%u6CA1%u6709%u547D%u4EE4%u884C%u9AD8%uFF0C%u6211%u8FD8%u662F%u63A8%u8350%u4F7F%u7528%u547D%u4EE4%u884C%u8FDB%u884CGit%u7684%u4F7F%u7528%u3002%0A%0A-%20-%20-%0A%0A%0A%23%23%20%u7B80%u5355%u4ECB%u7ECD%0A%0A%u7B80%u8981%u8BF4%u4E00%u4E0BGit%uFF0CGit%u53EA%u662F%u4E00%u4E2A%u7248%u672C%u63A7%u5236%u5DE5%u5177%uFF0C%u4E3E%u4F8B%u6765%u8BF4%uFF0C%u6211%u4E4B%u524D%u5199%u4E00%u4E2A%u591A%u6838%u5F52%u5E76%u7B97%u6CD5%uFF0C%u5C06%u6587%u4EF6%u4FDD%u5B58%u547D%u540D%u4E3AMergeSort.c%uFF0C%u7531%u4E8E%u53D1%u73B0%u4E00%u4E2ABug%uFF0C%u5C06%u4E00%u6BB5%u6BB5%u4EE3%u7801%u6CE8%u91CA%uFF0C%u663E%u5F97%u5F88%u7CDF%u4E71%uFF0C%u4E4B%u540E%u5C31%u628A%u81EA%u8BA4%u4E3A%u9519%u8BEF%u7684%u4EE3%u7801%u5220%u6389%uFF0C%u4FDD%u5B58%u540E%uFF0C%u53D1%u73B0%u4E4B%u524D%u5220%u6389%u7684%u4EE3%u7801%u6CA1%u6709%u9519%u8BEF%uFF0C%u60F3%u8FD4%u56DE%u7684%u65F6%u5019%u5374%u6CA1%u6709%u529E%u6CD5%u56DE%u53BB%u4E86%u3002%u8FD9%u5C31%u8981%u7528%u5230Git%u4E86%u3002%0A%0A%u5982%u679C%u60F3%u8981%u5220%u4EE3%u7801%u7684%u65F6%u5019%uFF0C%u5148%u628A%u4EE3%u7801%u4E0A%u4F20%u4E00%u4E0B%uFF0C%u901A%u8FC7%20%60git%20push%60%uFF0C%u4E4B%u540E%u5C31%u7B97%u628A%u4EE3%u7801%u5168%u90E8%u5220%u6389%uFF0C%u4E5F%u53EF%u4EE5%u901A%u8FC7Git%u8FD4%u56DE%u4E0A%u4E00%u4E2A%u65F6%u95F4%u70B9%uFF0C%u5982%u679C%u4FEE%u6539%u591A%u6B21%uFF0C%u8FD4%u56DE%u7B2C%u4E00%u6B21%u4FEE%u6539%u7684%u65F6%u95F4%u70B9%u7684%u4EE3%u7801%uFF0C%u4E5F%u53EF%u4EE5%u987A%u5229%u7684%u770B%u5230%u3002%20%20%0A%20%20%0A%u7531%u4E8EGit%u9700%u8981%u4E00%u4E2A%u670D%u52A1%u5668%uFF0C%u8FDB%u884C%u4E0A%u4F20%u6216%u8005%u4E0B%u8F7D%u4EE3%u7801%uFF0C%u56E0%u6B64%uFF0CGithub%u5C31%u662F%u4E00%u4E2A%u514D%u8D39%u670D%u52A1%u5668%uFF0C%u6CE8%u518C%u8D26%u6237%u540E%u5C31%u53EF%u4EE5%u901A%u8FC7Github%u7684IDE%u7BA1%u7406%u4EE3%u7801%uFF0C%u5E76%u4E14%u6E05%u6670%u770B%u5230%u6BCF%u4E2A%60git%20commit%60%u540E%u663E%u793A%u4FEE%u6539%u7684%u4EE3%u7801%u3002%20%20%0A%20%20%0A%20%20%0A%23%23%20GitHub%u7684%u914D%u7F6E%0A%0A%23%23%23%20%u5B89%u88C5%u5E76%u4E14%u914D%u7F6E%20Git%0A%0A%u9996%u5148%u53BB%u5B98%u65B9%u7F51%u7AD9%u4E0B%u8F7DGitHub%20App%uFF0C%u6211%u662FMAC%u7528%u6237%uFF0CMAC%u7684GitHub%20%u9009%u9879%u4E2D%u53EF%u4EE5%u81EA%u52A8%u5B89%u88C5%20%60Git%60%20%u548C%20%60osxkeychain%20helper%60%20%uFF0C%u5982%u679C%u662FLinux%u53EF%u80FD%u9700%u8981%u624B%u52A8%u5B89%u88C5%20%60Git%60%20%u548C%20%60osxkeychain%20helper%60.%0A%0A%u5982%u679C%u6CA1%u6709%u5B89%u88C5Git%uFF0C%u5C31%u901A%u8FC7%u5B98%u65B9%u4E0B%u8F7DGit%u7248%u672C%u7BA1%u7406%u5DE5%u5177%uFF0C%u4E0B%u8F7D%u5E76%u4E14%u5B89%u88C5%5B%u6700%u65B0%u7248%u672C%u7684Git%5D%28http%3A//git-scm.com/downloads%20%22Git%22%29.%20%20%0A%0A%u5B89%u88C5%u597D%u540E%u901A%u8FC7%u4EE5%u4E0B%u547D%u4EE4%u6765%u914D%u7F6EGit%u3002%u4E0B%u9762%u7684%u547D%u4EE4%u662F%u6211%u7528%u6211%u7684%u7528%u6237%u540D%u548C%u90AE%u7BB1%u8FDB%u884C%u8BBE%u7F6E%u3002%u53EA%u914D%u7F6E%u4E00%u6B21%u5C31%u591F%u4E86%uFF0C%u5F53%u7136%u4E0D%u4F1A%u6BCF%u6B21%u914D%u7F6E%u7528%u6237%u540D%u5BC6%u7801%u8FD9%u4E48%u9EBB%u70E6%u3002%0A%0A%60%60%60%20%0A%24%20git%20config%20--global%20user.name%20%22YuLei%22%0A%24%20git%20config%20--global%20user.email%20%22suiyun@me.com%22%0A%60%60%60%20%20%0A%20%20%0A%u5982%u679C%u662FMAC%uFF0C%u5229%u7528IDE%u81EA%u52A8%u88C5%u597DGit%u5E2E%u52A9%u9009%u9879%uFF0C%u5C31%u4E0D%u9700%u8981%u901A%u8FC7%u547D%u4EE4%u884C%u5B89%u88C5%60osxkeychain%60%uFF0C%u4E0A%u9762%u547D%u4EE4%u914D%u7F6E%u597D%u540E%uFF0C%u5B89%u88C5%60osxkeychain%60%uFF0C%u901A%u8FC7%u4EE5%u4E0B%u547D%u4EE4%uFF1A%0A%0A%60%60%60%0A%24%20git%20credential-osxkeychain%0A%23%20Test%20for%20the%20cred%20helper%0A%20%20git%3A%20%27credential-osxkeychain%27%20is%20not%20a%20git%20command.%20See%20%27git%20--help%27.%0A%24%20curl%20-s%20-O%20%5C%0A%20%20https%3A//github-media-downloads.s3.amazonaws.com/osx/git-credential-osxkeychain%0A%23%20Download%20the%20helper%0A%0A%24%20chmod%20u+x%20git-credential-osxkeychain%0A%23%20Fix%20the%20permissions%20on%20the%20file%20so%20it%20can%20be%20run%0A%0A%0A%24%20sudo%20mv%20git-credential-osxkeychain%20%5C%0A%20%20%22%24%28dirname%20%24%28which%20git%29%29/git-credential-osxkeychain%22%0A%23%20Move%20the%20helper%20to%20the%20path%20where%20git%20is%20installed%0A%20%20Password%3A%20%5Benter%20your%20password%5D%0A%20%20%0A%24%20%20git%20config%20--global%20credential.helper%20osxkeychain%0A%23%20Set%20git%20to%20use%20the%20osxkeychain%20credential%20helper%0A%60%60%60%0A%0A%u8FD9%u6837%u5C31%u914D%u7F6E%u597DGitHub%u4E86%u3002%0A%0A%0A%23%23%23%20GitHub%20%u7684%u57FA%u672C%u4F7F%u7528%0A%0A%u9996%u5148%u901A%u8FC7Git%uFF0C%u65B0%u5EFA%u4E00%u4E2A%u9879%u76EE%uFF0C%u5E76%u4E14%u63D0%u4EA4%u5230Github%u4E0A%u9762%u53BB%uFF0C%u73B0%u5728IDE%u521B%u5EFA%u4E00%u4E2Arepository%uFF0C%u7136%u540E%u627E%u5230giturl%uFF0C%u901A%u8FC7%20%60git%20clone%60%u4E0B%u8F7D%u5230%u672C%u5730%u3002%0A%0A%60%60%60%0A%24%20git%20clone%20https%3A//github.com/dyulei/blog.git%0A%23%20%u514B%u9686%u5230%u672C%u5730%uFF0C%u4F46%u662F%u65E0%u6CD5%u4E0A%u4F20%u66F4%u6539%uFF0C%u9700%u8981%u6743%u9650%0A%0A%24%20mkdir%20Github%20%26%20cd%20Github%0A%23%20%u521B%u5EFA%u5728%u672C%u5730%u7BA1%u7406%u8FDC%u7A0B%u9879%u76EE%u7684%u76EE%u5F55%0A%0A%24%20touch%20README.md%0A%23%20%u521B%u5EFA%u4E00%u4E2A%u6587%u4EF6%uFF0C%u7531%u4E8EGithub%u9700%u8981%u4E00%u4E2A%u8BF4%u660E%u6587%u4EF6%0A%0A%24%20git%20add%20.%0A%23%20%u589E%u52A0%u8981%u540C%u6B65%u7684%u76EE%u5F55%u4E2D%u7684%u6240%u6709%u6587%u4EF6%0A%0A%24%20git%20commit%20-m%20%27first%20commit%27%0A%24%20git%20commit%20-a%20%0A%0A%23%20-m%u662F%u624B%u52A8%u6DFB%u52A0%uFF0C%20-a%20%u81EA%u52A8%u6DFB%u52A0%u6CE8%u91CA%u4E0A%u4F20%u66F4%u65B0%0A%0A%24%20git%20remote%20add%20Blog%20https%3A//github.com/dyulei/blog.git%0A%23%20%u8FDC%u7A0B%u670D%u52A1%u5668%u522B%u540D%uFF1ABlog%20%u4E0A%u4F20%u7684%u5730%u5740%uFF1Ahttps%3A//github.com/dyulei/blog.git%0A%0A%24%20git%20pull%20-u%20Blog%20master%0A%23%20%u5C06%u670D%u52A1%u5668%u6570%u636E%u4E0B%u8F7D%u5230%u672C%u5730%u6587%u4EF6%u5939%0A%0A%24%20git%20push%20-u%20Blog%20master%0A%23%20%u5C06%u670D%u52A1%u5668%u6570%u636E%u4E0A%u4F20%u5230%u8FDC%u7A0B%u670D%u52A1%u5668%0A%0A%24%20git%20rm%20README.md%0A%23%20%u4ECE%u672C%u5730%u5E93%u4E2D%u5220%u9664%u6587%u4EF6%0A%60%60%60%0A%0A%u5173%u4E8E%u5206%u652F%u7248%u672C%u7684%u64CD%u4F5C%uFF0C%u521B%u5EFA%u5E76%u4E14%u5408%u5E76%u5206%u652F%uFF0C%u7531%u4E8EGit%u5F88%u591A%u65F6%u5019%u662F%u4E00%u4E2A%u56E2%u961F%u5728%u7528%uFF0C%u6BCF%u4E2A%u4EBA%u90FD%u8D1F%u8D23%u4E00%u5C0F%u90E8%u5206%uFF0C%u6700%u540E%u5408%u6210%u4E00%u5927%u90E8%u5206%uFF0C%u6240%u4EE5%u5C31%u8981%u901A%u8FC7%u5206%u652F%u63A7%u5236%uFF0C%u6700%u540E%u8FDB%u884C%u5408%u5E76%u3002%u5F53%u7136%u4E0B%u9762%u7684%u4EE3%u7801%u662F%u6700%u5BB9%u6613%u7684%u90E8%u5206%uFF0CGitHub%u5177%u6709%u76F8%u5F53%u5F3A%u5927%u7684%u529F%u80FD%u5728%u5206%u652F%u7248%u672C%u63A7%u5236%u4E0A%u9762%u3002%0A%0A%60%60%60%0A%24%20git%20branch%20%0A%23%20%u663E%u793A%u5F53%u524D%u5206%u652F%u662Fmaster%0A%0A%24%20git%20branch%20new-feature%0A%23%20%u521B%u5EFA%u5206%u652F%0A%0A%24%20git%20checkout%20new-feature%0A%23%20%u5207%u6362%u5230%u65B0%u5206%u652F%u4E2D%0A%0A%24%20git%20add%20sort.c%0A%24%20git%20commit%20-a%0A%24%20git%20push%20origin%20new-feature%0A%23%20%u5C06%u5206%u652F%u7ED3%u6784%u63D0%u4EA4%u5230%u8FDC%u7A0B%u670D%u52A1%u5668%uFF0C%u4F46%u662F%u6CA1%u6709%u548C%u4E3B%u5E72%u8FDB%u884C%u5408%u5E76%0A%0A%24%20git%20checkout%20master%20%0A%23%20%u5207%u6362%u7684%u4E3B%u5E72%u4E2D%u53BB%0A%24%20git%20merge%20new-feature%20%0A%23%20%u628A%u5206%u652F%u5408%u5E76%u5230%u4E3B%u5E72%u4E2D%u53BB%0A%24%20git%20branch%0A%24%20git%20push%0A%23%20%u6B64%u65F6%u4E3B%u5E72%u548C%u5206%u652F%u5DF2%u7ECF%u5408%u5E76%0A%60%60%60%0A%0A-%20-%20-%0A%3E%23%23%23%20%60About%20Me%60%0A%3E%0A-%20%60%u5FAE%u535A%60%20%20%uFF1A%5B@Levi-%u5C0F%u78CA%5D%28http%3A//weibo.com/Levi024%29%0A-%20%60%u90AE%u7BB1%60%20%20%uFF1A%3Csuiyun@me.com%3E%0A-%20%60GitHub%60%uFF1A%20https%3A//github.com/dyulei%0A%0A%0A