GIT学习---GIT&github的使用

GIT&github入门

版本控制的原理: 根据md5进行文件的校验【MD5的特性就是每次的输入一致则输出也一致】,对于每次的修改进行一次快照

版本控制的2个功能: 版本管理  +   协作开发

什么是GIT

GIT因为最初是从Linux起家的,非常依赖文件系统的一些特性,这些在 Linux 下表现的很好,而 Windows 下特别糟糕Git 中文教程。
Git是一个开源的分布式版本控制系统,用以有效、高速的处理从很小到非常大的项目版本管理.
Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。
Torvalds 开始着手开发 Git 是为了作为一种过渡方案来替代 BitKeeper,后者之前一直是 Linux 内核开发人员在全球使用的主要源代码工具。开放源码社区中的有些人觉得 BitKeeper 的许可证并不适合开放源码社区的工作,因此 Torvalds 决定着手研究许可证更为灵活的版本控制系统。尽管最初 Git 的开发是为了辅助 Linux 内核开发的过程,但是我们已经发现在很多其他自由软件项目中也使用了 Git。例如 最近就迁移到 Git 上来了,很多 Freedesktop 的项目也迁移到了 Git 上。【Linus在1991年创建了开源的Linux】

GitHub:

· 一个拥有143万开发者的社区。其中不乏Linux发明者Torvalds这样的顶级黑客,以及Rails创始人DHH这样的年轻极客。

· 这个星球上最流行的开源托管服务。目前已托管431万git项目,不仅越来越多知名开源项目迁入GitHub,比如Ruby on Rails、jQuery、Ruby、Erlang/OTP;近三年流行的开源库往往在GitHub首发,例如:BootStrapNode.jsCoffeScript等。

git安装

安装Git

最早Git是在Linux上开发的,很长一段时间内,Git也只能在Linux和Unix系统上跑。不过,慢慢地有人把它移植到了Windows上。现在,Git可以在Linux、Unix、Mac和Windows这几大平台上正常运行了。

下载地址;https://git-scm.com/download/win

安装的时候使用默认的配置即可,next一路到底即可

image

安装完成后启动git

image

【拓展工具下载】

Url: https://www.baidu.com/link?url=-8AHsP5NG3nH4Zozbu4xG26f2n1PLBYrzAHlWaPKnU3mj_IEbuRpM5V4N0bHIfVjqLoS2bJKGAQzPa4aettYXl4gylTFEvGX-4uNiiAqAAS&wd=&eqid=c4ba8e4b00042077000000035ae3233e

image

版本库创建

什么是版本库呢?

版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改、删除,Git都能跟踪,以便任何时刻都可以追踪历史,或者在将来某个时刻可以“还原”。

所以,创建一个版本库非常简单,首先,选择一个合适的地方,创建一个空目录:

Administrator@AFOEC-704092020 MINGW64 ~
$ cd F:/

Administrator@AFOEC-704092020 MINGW64 /f
$ cd GIT_Repository/

Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository
$ ls -a
./  ../

Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository
$ git init
Initialized empty Git repository in F:/GIT_Repository/.git/

Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ ls -a
./  ../  .git/

Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ ls -a .git/
./  ../  config  description  HEAD  hooks/  info/  objects/  refs/

瞬间Git就把仓库建好了,而且告诉你是一个空的仓库(empty Git repository),细心的读者可以发现当前目录下多了一个.git的目录,这个目录是Git来跟踪管理版本库的,没事千万不要手动修改这个目录里面的文件,不然改乱了,就把Git仓库给破坏了。
如果你没有看到.git目录,那是因为这个目录默认是隐藏的,用ls -ah命令就可以看见。

image

把文件添加到版本库

明确一下,所有的版本控制系统,其实只能跟踪文本文件的改动,比如TXT文件,网页,所有的程序代码等等,Git也不例外。版本控制系统可以告诉你每次的改动,比如在第5行加了一个单词“Linux”,在第8行删了一个单词“Windows”。而图片、视频这些二进制文件,虽然也能由版本控制系统管理,但没法跟踪文件的变化,只能把二进制文件每次改动串起来,也就是只知道图片从100KB改成了120KB,但到底改了啥,版本控制系统不知道,也没法知道。

【Microsoft的Word格式是二进制格式,因此,版本控制系统是没法跟踪Word文件的改动的】

真正使用版本控制系统,就要以纯文本方式编写文件

因为文本是有编码的,比如中文有常用的GBK编码,日文有Shift_JIS编码,如果没有历史遗留问题,强烈建议使用标准的UTF-8编码,所有语言使用同一种编码,既没有冲突,又被所有平台所支持。

GIT的工作分为: 工作区  -->  暂存区  --> 版本库

工作区: 编辑文件

暂存区: git add 文件名  添加文件到git的缓存区

版本去: git commit -m “commit” 提交内容到库中

image

image

Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ vim helloworld.txt
Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ cat helloworld.txt
hello world 2020
好好学习,天天向上

image

Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ git add helloworld.txt
warning: LF will be replaced by CRLF in helloworld.txt.
The file will have its original line endings in your working directory.
Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ git commit -m " first commit file to github "
*** Please tell me who you are.
Run
  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'Administrator@AFOEC-704092020.(none)')

image

Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ git config --global user.email "ftl@baidu.com"
Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ git config --global user.name "ftl"

image

Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ git commit -m " first commit file to github "
[master (root-commit) 71474ae]  first commit file to github
 1 file changed, 3 insertions(+)
 create mode 100644 helloworld.txt

image

Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ echo "fftl" >> helloworld.txt
Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ cat helloworld.txt
hello world 2020
好好学习,天天向上

fftl
Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ git status
On branch master
Changes 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:   helloworld.txt

no changes added to commit (use "git add" and/or "git commit -a")
Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ git add helloworld.txt
warning: LF will be replaced by CRLF in helloworld.txt.
The file will have its original line endings in your working directory.
Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   helloworld.txt

image

Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ git commit -m " second commit file to github "
[master f227b64]  second commit file to github
 1 file changed, 1 insertion(+)

Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ git status
On branch master
nothing to commit, working tree clean

在没提交之前可以查看修改的变化

image

Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ cat helloworld.txt
hello world 2020
好好学习,天天向上

fftl
HHHHHHHHHHHHHHHHHHHHHHHH
Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ git diff helloworld.txt
warning: LF will be replaced by CRLF in helloworld.txt.
The file will have its original line endings in your working directory.
diff --git a/helloworld.txt b/helloworld.txt
index 44ed930..0dea578 100644
--- a/helloworld.txt
+++ b/helloworld.txt
@@ -2,3 +2,4 @@ hello world 2020
 好好学习,天天向上

 fftl
+HHHHHHHHHHHHHHHHHHHHHHHH

image

Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ git add *
warning: LF will be replaced by CRLF in helloworld.txt.
The file will have its original line endings in your working directory.
Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ git commit -m "all commit"
[master ec8b78f] all commit
 1 file changed, 1 insertion(+)

image

文件回滚
Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ git log
commit ec8b78f7ff0e97d57c7217f29665aba2a6eb0d4b (HEAD -> master)
Author: ftl <ftl@baidu.com>
Date:   Fri Apr 27 23:14:12 2018 +0800

    all commit
commit f227b64508a389b41797141b581aca47b1fb8fd0
Author: ftl <ftl@baidu.com>
Date:   Fri Apr 27 23:04:21 2018 +0800

     second commit file to github
commit 71474ae7f3e3947e4ffe9a5144b0f83138596b2a
Author: ftl <ftl@baidu.com>
Date:   Fri Apr 27 21:40:38 2018 +0800

     first commit file to github
Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ git log --pretty=oneline
ec8b78f7ff0e97d57c7217f29665aba2a6eb0d4b (HEAD -> master) all commit
f227b64508a389b41797141b581aca47b1fb8fd0  second commit file to github
71474ae7f3e3947e4ffe9a5144b0f83138596b2a  first commit file to github

image

Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ cat helloworld.txt
hello world 2020
好好学习,天天向上

fftl
HHHHHHHHHHHHHHHHHHHHHHHH
Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ git reset --hard HEAD^
HEAD is now at f227b64  second commit file to github
Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ cat helloworld.txt
hello world 2020
好好学习,天天向上

fftl

image

image

Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ git reflog
f227b64 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
ec8b78f HEAD@{1}: commit: all commit
f227b64 (HEAD -> master) HEAD@{2}: commit: second commit file to github
71474ae HEAD@{3}: commit (initial): first commit file to github

image

Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ cat helloworld.txt
hello world 2020
好好学习,天天向上

fftl
Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ git reset --hard 71474ae
HEAD is now at 71474ae  first commit file to github
Administrator@AFOEC-704092020 MINGW64 /f/GIT_Repository (master)
$ cat helloworld.txt
hello world 2020
好好学习,天天向上

image

GitHub的使用

登录并创建一个项目

image

image

在git bash内依次输入上面的内容,表示将文件上传到github内

【更多参考】http://www.cnblogs.com/alex3714/articles/5930846.html

原文地址:https://www.cnblogs.com/ftl1012/p/9427168.html