Git学习笔记
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Git学习笔记相关的知识,希望对你有一定的参考价值。
1、Git的介绍及安装
Git:全宇宙最牛的分布式版本控制软件,Git是目前世界上最先进的分布式版本控制系统
#CentOS7下git的安装 [[email protected] ~]# yum -y install git #设置git账号信息 [[email protected] ~]# git config --global user.name "molewan" [[email protected] ~]# git config --global user.email "[email protected]" a)因为Git是分布式版本控制系统,所以,每个机器都必须报家:你的名字和Email地址。你也许 会担,如果有故意冒充别怎么办?这个不必担,先我们相信家都是善良 知的群众,其次,真的有冒充的也是有办法可查的。 b)注意git config命令的--global参数,了这个参数,表你这台机器上所有的Git仓库都会使 这个配置,当然也可以对某个仓库指定不同的户名和Email地址。
2、查看git的配置:
[[email protected] ~]# git config --list user.name=molewan [email protected]
3、为git配置颜色
[[email protected] ~]# git config --global color.ui true [[email protected] ~]# git config --list user.name=molewan [email protected] color.ui=true
4、创建一个本地的git库
[[email protected] ~]# mkdir molewan [[email protected] ~]# cd molewan [[email protected] molewan]# ll total 0 [[email protected] molewan]# git init Initialized empty Git repository in /root/molewan/.git/ 说明:仓库初始化一个git仓库,使用git init命令,将这个目录变成git可以管理的 [[email protected] molewan]# ls -la total 8 drwxr-xr-x. 3 root root 17 Nov 26 17:04 . dr-xr-x---. 12 root root 4096 Nov 26 17:04 .. drwxr-xr-x. 7 root root 4096 Nov 26 17:04 .git
5、将文件添加到版本库:
将一个文件放到Git仓库只需要两步:
a)git add告诉git,将文本添加到仓库
b)用git commit告诉git,把文件提交到仓库
[[email protected] molewan]# vim readme.txt You have new mail in /var/spool/mail/root [[email protected] molewan]# cat readme.txt hehe [[email protected] molewan]# git status # On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # #readme.txt nothing added to commit but untracked files present (use "git add" to track) 添加一个新文件,readme.txt [[email protected] molewan]# git add readme.txt [[email protected] molewan]# git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # #new file: readme.txt # [[email protected] molewan]# git commit -m "the first commity" [master (root-commit) 24a5b30] the first commity 1 file changed, 1 insertion(+) create mode 100644 readme.txt You have new mail in /var/spool/mail/root 说明,commit -m 后面的内容只是针对本次提交的一个描述 [[email protected] molewan]# git status # On branch master nothing to commit, working directory clean [[email protected] molewan]# vim deply.sh You have new mail in /var/spool/mail/root [[email protected] molewan]# git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # #deply.sh nothing added to commit but untracked files present (use "git add" to track) [[email protected] molewan]# git add deply.sh [[email protected] molewan]# git commit -m "2th commit" [master f30d737] 2th commit 1 file changed, 2 insertions(+) create mode 100644 deply.sh [[email protected] molewan]# git status # On branch master nothing to commit, working directory clean [[email protected] molewan]# ls -l total 8 -rw-r--r--. 1 root root 22 Nov 26 17:15 deply.sh -rw-r--r--. 1 root root 5 Nov 26 17:07 readme.txt
6、 使用git log进行查看
[[email protected] molewan]# git log commit f30d737d24b2c04f8ce70a8c88b0071bdcc069c8 Author: molewan <[email protected]> Date: Sat Nov 26 17:16:27 2016 -0500 2th commit commit 24a5b3094f55e815ad46561f4b741c23bc7ad371 Author: molewan <[email protected]> Date: Sat Nov 26 17:13:37 2016 -0500 the first commity
7、git diff进行对比
[[email protected] molewan]# vim readme.txt # 添加一行文字,hehe [[email protected] molewan]# cat readme.txt hehe hehe [[email protected] molewan]# 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: readme.txt # no changes added to commit (use "git add" and/or "git commit -a")
[[email protected] molewan]# git diff readme.txt diff --git a/readme.txt b/readme.txt index 91ca0fa..197cac2 100644 --- a/readme.txt +++ b/readme.txt @@ -1 +1,2 @@ hehe +hehe [[email protected] molewan]# git add readme.txt [[email protected] molewan]# git commit -m "add 2hehe" [master c33cc4f] add 2hehe 1 file changed, 1 insertion(+) [[email protected] molewan]# git log commit c33cc4f3c6a1dc6741bce035de920f5a17c82b4b Author: molewan <[email protected]> Date: Sat Nov 26 17:22:05 2016 -0500 add 2hehe commit f30d737d24b2c04f8ce70a8c88b0071bdcc069c8 Author: molewan <[email protected]> Date: Sat Nov 26 17:16:27 2016 -0500 2th commit commit 24a5b3094f55e815ad46561f4b741c23bc7ad371 :...skipping... commit c33cc4f3c6a1dc6741bce035de920f5a17c82b4b Author: molewan <[email protected]> Date: Sat Nov 26 17:22:05 2016 -0500 add 2hehe commit f30d737d24b2c04f8ce70a8c88b0071bdcc069c8 Author: molewan <[email protected]> Date: Sat Nov 26 17:16:27 2016 -0500 2th commit commit 24a5b3094f55e815ad46561f4b741c23bc7ad371 Author: molewan <[email protected]> Date: Sat Nov 26 17:13:37 2016 -0500 the first commity
小结:
a)所有的版本控制系统,其实只能跟踪本件的改动,如TXT件,网页,所有的程序代码等等,Git也不 例外。 b)版本控制系统 可以告诉你每次的改动,如在第5加了个单词“Linux”删了个单词“Windows” c)图片视频这些二进制文件,虽然也能由版本 控制系统管理,但没法跟踪件的变化,只能把二进制文 件每次改动串起来,也就是只知道图从100KB改成了120KB,但到底改了啥,版本控制系统不知道,没法 知道。 d)不幸的是,Microsoft的Word格式是进制格式,因此,版本控制系统是没法跟踪Word件的改动的, 我们举的例只是为了演示,真正使版本控制系统,就要以纯本式编写件。
8、版本回退(回退到上一个版本)
[[email protected] molewan]# git reset --hard HEAD^ HEAD is now at f30d737 2th commit # 说明:HEAD^代表上一个版本,^^代表上上个版本 [[email protected] molewan]# cat readme.txt hehe [[email protected] molewan]# git log commit f30d737d24b2c04f8ce70a8c88b0071bdcc069c8 Author: molewan <[email protected]> Date: Sat Nov 26 17:16:27 2016 -0500 2th commit commit 24a5b3094f55e815ad46561f4b741c23bc7ad371 Author: molewan <[email protected]> Date: Sat Nov 26 17:13:37 2016 -0500 the first commity
9、回退到指定的某个版本
[[email protected] molewan]# git reflog f30d737 [email protected]{0}: reset: moving to HEAD^ c33cc4f [email protected]{1}: commit: add 2hehe f30d737 [email protected]{2}: commit: 2th commit 24a5b30 [email protected]{3}: commit (initial): the first commity [[email protected] molewan]# git reset --hard 24a5b30 HEAD is now at 24a5b30 the first commity [[email protected] molewan]# ls -l total 4 -rw-r--r--. 1 root root 5 Nov 26 17:25 readme.txt 说明:Git的版本回退速度常快,因为Git在内部有个指向当前版本的HEAD指针,当你回退版本 的时候,Git仅仅是把HEAD从指向“append GPL”
小结:
a)HEAD指向的版本就是当前版本,因此,Git允许我们在版本的历史之间穿梭,使命令 git reset --hard commit_id。 b)整理、排版: numbbbbb 穿梭前,git log可以查看提交历史,以便确定要回退到哪个版本。 c) 要重返未来,git reflog查看命令历史,以便确定要回到未来的哪个版本
10、工作区、版本库、暂存区:
工作区:就是你在电脑能看到的目录,如我的某个文件夹
版本库(Repository):作区有个隐藏目录“.git”,这个不算作区,是Git的版本库。
暂存区:Git的版本库存了很多东,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为我们动创建的第一个分支master,以及指向master的个指针叫HEAD。
[[email protected] git-data]# pwd /git-data#工作区 [[email protected] git-data]# ls -la total 40 drwxr-xr-x 3 root root 119 Jun 29 11:22 . dr-xr-xr-x. 19 root root 281 Jun 29 09:32 .. -rw-r--r-- 1 root root 10 Jun 29 11:20 dev.txt drwxr-xr-x 8 root root 201 Jun 29 11:21 .git#版本库 -rw-r--r-- 1 root root 1045 Jun 29 11:20 .gitignore -rw-r--r-- 1 root root 8870 Jun 29 11:20 git.txt -rw-r--r-- 1 root root 11357 Jun 29 11:14 LICENSE -rw-r--r-- 1 root root 25 Jun 29 11:20 README.md -rw-r--r-- 1 root root 1774 Jun 29 11:20 谈运维.txt [[email protected] molewan]# git status # On branch master nothing to commit, working directory clean 需要先add,才能commit 说明:要掌握工作区的状态,使用git status命令,而用git diff可以查看修改内容
本文出自 “冰冻vs西瓜” 博客,请务必保留此出处http://molewan.blog.51cto.com/287340/1943143
以上是关于Git学习笔记的主要内容,如果未能解决你的问题,请参考以下文章