Git学习笔记(2)

工作区和暂存区

我们在testgit文件夹下创建一个文件test_02.txt

[root@xwq testgit]# ls

test_01.txt  test_02.txt

库版本:工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库。
Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD。

git笔记3

前面讲了我们把文件往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笔记4

所以,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

管理修改

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.

撤销修改

假如你对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时,表示最新的版本。

删除文件

如何在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直接删除

远程仓库

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

git笔记5

git笔记6

为什么要添加ssh key呢?不解释自行谷歌

添加远程库

现在我们既有了本地仓库又有了github里远程仓库,问题来了如何把本地仓里文件备份到远程库里呢?

第一步:我们登陆github,然后我们创建一个新的仓库
git笔记7

git笔记8

接下来我们按照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笔记9

成功的把本地库推送到远程库
从现在起,只要本地作了提交,就可以通过命令:git push origin master
把本地master分支的最新修改推送至GitHub,现在,你就拥有了真正的分布式版本库!

从远程库克隆

现在我们有了本地库也有了远程库,我们现在来聊一下如何克隆远程库
第一步:登陆GitHub,创建一个新的仓库,名字就叫test_clone

git笔记10

git笔记11

现在我们有了远程库了,接下来我们把远程库克隆下了,用如下命令:

[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

成功克隆!!!

|| 版权声明
作者:废权
链接:https://blog.yjscloud.com/archives/14
声明:如无特别声明本文即为原创文章仅代表个人观点,版权归《废权的博客》所有,欢迎转载,转载请保留原文链接。
THE END
分享
二维码
Git学习笔记(2)
工作区和暂存区 我们在testgit文件夹下创建一个文件test_02.txt [root@xwq testgit]# ls test_01.txt test_02.txt 库版本:工作区有一个隐藏目录.git,这个……
<<上一篇
下一篇>>
文章目录
关闭
目 录