首页 > 代码库 > Git学习笔记(1)
Git学习笔记(1)
1、安装Git:
在linux下安装:yum install git
其他系统安装在这里略去~~~
安装完成后,需要设置一下,在命令行输入以下命令:
[root@xwq ~]# git config --global user.name "your name" [root@xwq ~]# git config --global user.email "your email"
注意git config命令的--global参数,用了这个参数,表示你这台机器上所有的Git仓库都会使用这个配置,当然也可以对某个仓库指定不同的用户名和Email地址。
2、创建版本库
创建版本库很简单,找到一个合适的地方,创建一个空目录:
[root@xwq ~]# mkdir testgit [root@xwq ~]# cd testgit/ [root@xwq testgit]# pwd /root/testgit
如果你是在window下运行git,请确保文件路径没有中文!
通过git init命令把这个目录变成Git可以管理的仓库:
[root@xwq testgit]# git init Initialized empty Git repository in /root/testgit/.git/
接着我们在仓库目录下创建一个txt文件:test_01.txt
[root@xwq testgit]# vim test_01.txt
在里面输入以下内容:
Git is a version control system.
Git is free software.
用命令git add告诉Git,把文件添加到仓库:
[root@xwq testgit]# git add test_01.txt
执行上面的命令,没有任何显示,这就对了,Unix的哲学是“没有消息就是好消息”,说明添加成功。
用命令git commit告诉Git,把文件提交到仓库:
[root@xwq testgit]# git commit -m "wrote a readme file" [master (root-commit) 7347c20] wrote a readme file 1 file changed, 2 insertions(+) create mode 100644 test_01.txt
简单解释一下git commit命令,-m后面输入的是本次提交的说明,可以输入任意内容,当然最好是有意义的,这样你就能从历史记录里方便地找到改动记录。
嫌麻烦不想输入-m "xxx"行不行?确实有办法可以这么干,但是强烈不建议你这么干,因为输入说明对自己对别人阅读都很重要。
git commit命令执行成功后会告诉你,1个文件被改动(我们新添加的test_01.txt文件),插入了两行内容(test_01.txt有两行内容)
3、撤销命令
我们将test_01.txt文件的内容改为如下内容:
Git is a distributed version control system.
Git is free software.
运行git status命令查看结果:
[root@xwq testgit]# 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: test_01.txt # no changes added to commit (use "git add" and/or "git commit -a")
git status命令可以让我们时刻掌握仓库当前的状态,上面的命令告诉我们,text_01.txt被修改过了,但还没有准备提交的修改。
虽然Git告诉我们test_01.txt被修改了,如果想查看被修改了什么内容可以用git diff命令查看:
[root@xwq testgit]# git diff test_01.txt diff --git a/test_01.txt b/test_01.txt index 46d49bf..9247db6 100644 --- a/test_01.txt +++ b/test_01.txt @@ -1,2 +1,2 @@ -Git is a version control system. +Git is a distributed version control system. Git is free software.
我们知道被修改的内容后就可以放心的文件提交到仓库了,接下来我们用git add :
[root@xwq testgit]# git add test_01.txt
同样没有返回任何信息就是好结果
我们在用git status查看当前仓库的状态:
[root@xwq testgit]# git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: test_01.txt #
git status 告诉我们将要被提交的修改包括test_01.txt,下一步,就可以放心地提交了:
[root@xwq testgit]# git commit -m "add distributed" [master 6c90823] add distributed 1 file changed, 1 insertion(+), 1 deletion(-)
再次用git status 查看仓库状态:
[root@xwq testgit]# git status # On branch master nothing to commit, working directory clean
4、版本回退
我们再次对test_01.txt文件进行修改,修改内容如下:
Git is a distributed version control system.
Git is free software distributed under the GPL.
然后提交:
[root@xwq testgit]# git add test_01.txt [root@xwq testgit]# git commit -m "append GPL" [master ee985a6] append GPL 1 file changed, 1 insertion(+), 1 deletion(-)
Git每当你觉得文件修改到一定程度的时候,就可以“保存一个快照”,这个快照在Git中被称为commit。一旦你把文件改乱了,或者误删了文件,还可以从最近的一个commit恢复,然后继续工作,而不是把几个月的工作成果全部丢失。
现在,我们回顾一下test_01.txt文件一共有几个版本被提交到Git仓库里了:
版本1:wrote a readme file
Git is a version control system.
Git is free software.
版本2:add distributed
Git is a distributed version control system.
Git is free software.
版本3:append GPL
Git is a distributed version control system.
Git is free software distributed under the GPL.
在实际工作中,我们脑子里怎么可能记得一个几千行的文件每次都改了什么内容,不然要版本控制系统干什么。版本控制系统肯定有某个命令可以告诉我们历史记录,在Git中,我们用git log命令查看:
[root@xwq testgit]# git log commit ee985a69fecf94482280769949290b240b7a0329 Author: wenqiuan <1303460512@qq.com> Date: Thu Apr 13 11:45:14 2017 -0400 append GPL commit 6c9082366df03ee9a47f2bcd18be0105fa1fae86 Author: wenqiuan <1303460512@qq.com> Date: Thu Apr 13 11:33:34 2017 -0400 add distributed commit 7347c2023929935a58f147168a50ce9bf51a2478 Author: wenqiuan <1303460512@qq.com> Date: Thu Apr 13 09:48:56 2017 -0400 wrote a readme file
git log命令显示从最近到最远的提交日志,我们可以看到3次提交,最近的一次是append GPL,上一次是add distributed,最早的一次是wrote a readme file。
如果嫌输出信息太多,看得眼花缭乱的,可以试试加上--pretty=oneline参数:
[root@xwq testgit]# git log --pretty=oneline ee985a69fecf94482280769949290b240b7a0329 append GPL 6c9082366df03ee9a47f2bcd18be0105fa1fae86 add distributed 7347c2023929935a58f147168a50ce9bf51a2478 wrote a readme file
注:类似这些数据ee985a69fecf94482280769949290b240b7a0329的是commit id(版本号)
那么问题来了我们要如何回退版本呢?其实很简单,首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,也就是最新的提交ee985a69fecf94482280769949290b240b7a0329,上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100。
现在,我们要把当前版本“append GPL”回退到上一个版本“add distributed”,就可以使用git reset命令:
[root@xwq testgit]# git reset --hard HEAD^ HEAD is now at 6c90823 add distributed
现在我们查看一下test_01.txt的内容是不是add distributed:
[root@xwq testgit]# cat test_01.txt Git is a distributed version control system. Git is free software. [root@xwq testgit]# git log commit 6c9082366df03ee9a47f2bcd18be0105fa1fae86 Author: wenqiuan <1303460512@qq.com> Date: Thu Apr 13 11:33:34 2017 -0400 add distributed commit 7347c2023929935a58f147168a50ce9bf51a2478 Author: wenqiuan <1303460512@qq.com> Date: Thu Apr 13 09:48:56 2017 -0400 wrote a readme file
很显然我们已经回退到上一个版本了
现在我又想回到最新版本的append GPL了怎么办?这个我们可以用git reflog命令就可以轻松的办到,命令git reflog用来记录你的每一次命令:
[root@xwq testgit]# git reflog 6c90823 HEAD@{0}: reset: moving to HEAD^ ee985a6 HEAD@{1}: commit: append GPL 6c90823 HEAD@{2}: commit: add distributed 7347c20 HEAD@{3}: commit (initial): wrote a readme file
接下来我们只用输入命令 git reset --hard ee985a6(append GPL的id)就可以回到最新的版本了
[root@xwq testgit]# git reset --hard ee985a6 HEAD is now at ee985a6 append GPL [root@xwq testgit]# cat test_01.txt Git is a distributed version control system. Git is free software distributed under the GPL.
5、工作区和暂存区
我们在testgit文件夹下创建一个文件test_02.txt
[root@xwq testgit]# ls test_01.txt test_02.txt
库版本:工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库。
Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD。
前面讲了我们把文件往Git版本库里添加的时候,是分两步执行的:
第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;
第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支。
因为我们创建Git版本库时,Git自动为我们创建了唯一一个master分支,所以,现在,git commit就是往master分支上提交更改。
你可以简单理解为,需要提交的文件修改通通放到暂存区,然后,一次性提交暂存区的所有修改。
现在,我们再练习一遍,先对test_01.txt做个修改,加上一行内容:
[root@xwq testgit]# cat test_01.txt Git is a distributed version control system. Git is free software distributed under the GPL. Git has a mutable index called stage.
然后,在工作区新增的test_02.txt文本文件随便随便写点东西。
用git status查看一下状态:
[root@xwq testgit]# 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: test_01.txt # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # test_02.txt no changes added to commit (use "git add" and/or "git commit -a")
Git告诉我们,test_01.txt被修改了,而test_02.txt还从来没有被添加过,所以它的状态是Untracked。
现在,使用两次命令git add,把test_01.txt和test_02.txt都添加后,用git status再查看一下:
[root@xwq testgit]# git add test_01.txt [root@xwq testgit]# git add test_02.txt [root@xwq testgit]# git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: test_01.txt # new file: test_02.txt #
现在暂存区的状态变成了这样:
所以,git add命令实际上就是把要提交的所有修改放到暂存区(Stage),然后,执行git commit就可以一次性把暂存区的所有修改提交到分支。
[root@xwq testgit]# git commit -m "undeestand how stage works" [master 10820b3] undeestand how stage works 2 files changed, 2 insertions(+) create mode 100644 test_02.txt
现在再来查看工作区,就会发现什么都没有了
[root@xwq testgit]# git status # On branch master nothing to commit, working directory clean
6、管理修改
Git的优秀之处在于他的跟踪并管理的是修改,而非文件。什么是修改?修改就是你对文件内容增删改都叫修改,甚至创建文件夹也算修改,我们通过实验来证明这一点。
我们对文件test_01.txt进行如下修改
[root@xwq testgit]# cat test_01.txt Git is a distributed version control system. Git is free software distributed under the GPL. Git has a mutable index called stage. Git tracks changes.
然后添加:
[root@xwq testgit]# git add test_01.txt [root@xwq testgit]# git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: test_01.txt #
再次对test_01.txt文件进行修改:
[root@xwq testgit]# cat test_01.txt Git is a distributed version control system. Git is free software distributed under the GPL. Git has a mutable index called stage. Git tracks changes of files. 提交: [root@xwq testgit]# git commit -m "git tracks changes" [master d65f079] git tracks changes 1 file changed, 1 insertion(+)
查看状态:
[root@xwq testgit]# 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: test_01.txt #
no changes added to commit (use "git add" and/or "git commit -a")
我们发现第二次修改并没有被提交,我们查看一下操作流程:
第一次修改 -> git add -> 第二次修改 -> git commit
前面我们说到Git管理的是修改,当你用git add命令后,在工作区的第一次修改被放入暂存区,准备提交,但是,在工作区的第二次修改并没有放入暂存区,所以,git commit只负责把暂存区的修改提交了,也就是第一次的修改被提交了,第二次的修改不会被提交。
提交后,用git diff HEAD -- readme.txt命令可以查看工作区和版本库里面最新版本的区别:
[root@xwq testgit]# git add test_01.txt [root@xwq testgit]# git diff HEAD -- test_01.txt diff --git a/test_01.txt b/test_01.txt index a9c5755..9520f67 100644 --- a/test_01.txt +++ b/test_01.txt @@ -1,4 +1,4 @@ Git is a distributed version control system. Git is free software distributed under the GPL. Git has a mutable index called stage. -Git tracks changes. +Git tracks changes of files.
7、撤销修改
假如你对test_01.txt进行了如下修改:
[root@xwq testgit]# cat test_01.txt Git is a distributed version control system. Git is free software distributed under the GPL. Git has a mutable index called stage. Git tracks changes of files. My stupid boss still prefers SVN.
要如何撤销修改呢?我们用git status命令查看一下
[root@xwq testgit]# git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: test_01.txt # # 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: test_01.txt #
其实Git已经告诉你如何撤销修改了,我们只要在命令行输入git checkout -- test_01.txt就可以撤销修改:
命令git checkout -- test_01.txt意思就是,把test_01.txt文件在工作区的修改全部撤销,这里有两种情况:
一种是test_01txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
一种是test_01.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。
总之,就是让这个文件回到最近一次git commit或git add时的状态。
我们现在来查看一下test_01.txt文件内容:
[root@xwq testgit]# git checkout -- test_01.txt [root@xwq testgit]# cat test_01.txt Git is a distributed version control system. Git is free software distributed under the GPL. Git has a mutable index called stage. Git tracks changes of files
假如你不小心把文件已经用git add提交到暂存区了又该怎么办?
我们查看一下状态:
[root@xwq testgit]# git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: test_01.txt #
同样Git也告诉了我们如何做,用命令git reset HEAD file可以把暂存区的修改撤销掉(unstage),重新放回工作区:
[root@xwq testgit]# git reset HEAD test_01.txt Unstaged changes after reset: M test_01.txt
git reset命令既可以回退版本,也可以把暂存区的修改回退到工作区。当我们用HEAD时,表示最新的版本。
8、删除文件
如何在Git删除一个文件呢? 我们创建一个文件来实验一下:
[root@xwq testgit]# vim test_03.txt [root@xwq testgit]# git add test_03.txt [root@xwq testgit]# git commit -m "add test_03.txt" [master 436a7c1] add test_03.txt 1 file changed, 1 insertion(+) create mode 100644 test_03.txt
我们可以用命令git rm test_03.txt直接删除
9、远程仓库
Git有一个杀手级的功能,就是远程仓库!!!没有之一!!!说他是世界最强也不为过!!!
要想使用这个功能我们首先要注册一个GitHub账号。由于本地Git仓库和GitHub仓库之间的传输是通过SSH加密的,所以,需要一点设置:
第1步:创建SSH Key。在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一步。如果没有,打开Shell(Windows下打开Git Bash),创建SSH Key:
[root@xwq testgit]# ssh-keygen -t rsa -C "1303460512@qq.com" Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory ‘/root/.ssh‘. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: 02:58:bd:65:5c:dc:12:78:4f:fe:bf:6e:5e:e9:84:79 1303460512@qq.com The key‘s randomart image is: +--[ RSA 2048]----+ | .. . +oo | | o . = + o | | . . + . = | | .. o | | . S . | | . + .| | o E.| | +.o| | +=.| +-----------------+
输入命令一路回车即可,保持默认即可~~~
如果一切顺利的话,可以在用户主目录里找到.ssh目录,里面有id_rsa和id_rsa.pub两个文件,这两个就是SSH Key的秘钥对,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,可以放心地告诉任何人。
第2步:登陆GitHub,打开GitHub添加key页面:https://github.com/settings/keys
为什么要添加ssh key呢?不解释自行谷歌~~~
10、添加远程库
现在我们既有了本地仓库又有了github里远程仓库,问题来了如何把本地仓里文件备份到远程库里呢?
第一步:我们登陆github,然后我们创建一个新的仓库:
接下来我们按照github的提示将我们本地库添加到远程库里,运行如下命令
[root@xwq testgit]# git remote add origin https://github.com/wenqiuan/test_01.git [root@xwq testgit]# git push -u origin master Username for ‘https://github.com‘: wenqiuan Password for ‘https://wenqiuan@github.com‘: fatal: unable to access ‘https://github.com/wenqiuan/test_01.git/‘: Empty reply from server
发现无法上传文件,我们把linux的防火墙关闭~~~
[root@xwq testgit]# systemctl status firewalld(查看linux防火墙状态) firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled) Active: active (running) since Thu 2017-04-13 19:31:06 EDT; 2h 48min ago Main PID: 1047 (firewalld) CGroup: /system.slice/firewalld.service └─1047 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid Apr 13 19:31:06 xwq systemd[1]: Started firewalld - dynamic firewall daemon. [root@xwq testgit]# systemctl stop firewalld(关闭防火墙) [root@xwq testgit]# systemctl disable firewalld(永久关闭防火墙) rm ‘/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service‘ rm ‘/etc/systemd/system/basic.target.wants/firewalld.service‘
再次运行上传命令:
[root@xwq testgit]# git remote rm origin [root@xwq testgit]# git remote add origin https://github.com/wenqiuan/test_01.git [root@xwq testgit]# git push -u origin master Username for ‘https://github.com‘: wenqiuan Password for ‘https://wenqiuan@github.com‘: Counting objects: 28, done. Delta compression using up to 2 threads. Compressing objects: 100% (22/22), done. Writing objects: 100% (28/28), 2.20 KiB | 0 bytes/s, done. Total 28 (delta 7), reused 0 (delta 0) remote: Resolving deltas: 100% (7/7), done. To https://github.com/wenqiuan/test_01.git * [new branch] master -> master Branch master set up to track remote branch master from origin.
成功的把本地库推送到远程库
从现在起,只要本地作了提交,就可以通过命令:git push origin master
把本地master分支的最新修改推送至GitHub,现在,你就拥有了真正的分布式版本库!
11、从远程库克隆
现在我们有了本地库也有了远程库,我们现在来聊一下如何克隆远程库
第一步:登陆GitHub,创建一个新的仓库,名字就叫test_clone
现在我们有了远程库了,接下来我们把远程库克隆下了,用如下命令:
[root@xwq testgit]# git clone git@github.com:wenqiuan/test_clone.git Cloning into ‘test_clone‘... The authenticity of host ‘github.com (192.30.255.113)‘ 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 Warning: Permanently added ‘github.com,192.30.255.113‘ (RSA) to the list of known hosts. remote: Counting objects: 3, done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Receiving objects: 100% (3/3), done. [root@xwq testgit]# cd test_clone/ [root@xwq test_clone]# ls README.md
成功克隆!!!
Git学习笔记(1)