Linux-git使用

一、git的使用

1.git简介

如果你用Word写过毕业论文,那你一定有这样的经历:

想删除一个段落,又怕将来想恢复找不回来怎么办?有办法,先把当前文件“另存为……”一个新的Word文件,再接着改,改到一定程度,再“另存为……”一个新文件,这样一直改下去,最后你的Word文档变成了这样:

lots-of-docs

过了一周,你想找回被删除的文字,但是已经记不清删除前保存在哪个文件里了,只好一个一个文件去找,真麻烦。

看着一堆乱七八糟的文件,想保留最新的一个,然后把其他的删掉,又怕哪天会用上,还不敢删,真郁闷。

更要命的是,有些部分需要你的财务同事帮助填写,于是你把文件Copy到U盘里给她(也可能通过Email发送一份给她),然后,你继续修改Word文件。一天后,同事再把Word文件传给你,此时,你必须想想,发给她之后到你收到她的文件期间,你作了哪些改动,得把你的改动和她的部分合并,真困难。

于是你想,如果有一个软件,不但能自动帮我记录每次文件的改动,还可以让同事协作编辑,这样就不用自己管理一堆类似的文件了,也不需要把文件传来传去。如果想查看某次改动,只需要在软件里瞄一眼就可以,岂不是很方便?

这个软件用起来就应该像这个样子,能记录每次文件的改动:

版本文件名用户说明日期
1service.doc张三删除了软件服务条款57/12 10:38
2service.doc张三增加了License人数限制7/12 18:09
3service.doc李四财务部门调整了合同金额7/13 9:51
4service.doc张三延长了免费升级周期7/14 15:17

这样,你就结束了手动管理多个“版本”的史前时代,进入到版本控制的20世纪。

Git是目前世界上最先进的分布式版本控制系统(没有之一)。

Git有什么特点?简单来说就是:高端大气上档次!

1.2git的由来

​ 很多人都知道,Linus在1991年创建了开源的Linux,从此,Linux系统不断发展,已经成为最大的服务器系统软件了。

​ Linus虽然创建了Linux,但Linux的壮大是靠全世界热心的志愿者参与的,这么多人在世界各地为Linux编写代码,那Linux的代码是如何管理的呢?

​ 事实是,在2002年以前,世界各地的志愿者把源代码文件通过diff的方式发给Linus,然后由Linus本人通过手工方式合并代码!

​ 你也许会想,为什么Linus不把Linux代码放到版本控制系统里呢?不是有CVS、SVN这些免费的版本控制系统吗?因为Linus坚定地反对CVS和SVN,这些集中式的版本控制系统不但速度慢,而且必须联网才能使用。有一些商用的版本控制系统,虽然比CVS、SVN好用,但那是付费的,和Linux的开源精神不符。

​ 不过,到了2002年,Linux系统已经发展了十年了,代码库之大让Linus很难继续通过手工方式管理了,社区的弟兄们也对这种方式表达了强烈不满,于是Linus选择了一个商业的版本控制系统BitKeeper,BitKeeper的东家BitMover公司出于人道主义精神,授权Linux社区免费使用这个版本控制系统。

​ 安定团结的大好局面在2005年就被打破了,原因是Linux社区牛人聚集,不免沾染了一些梁山好汉的江湖习气。开发Samba的Andrew试图破解BitKeeper的协议(这么干的其实也不只他一个),被BitMover公司发现了(监控工作做得不错!),于是BitMover公司怒了,要收回Linux社区的免费使用权。

​ Linus可以向BitMover公司道个歉,保证以后严格管教弟兄们,嗯,这是不可能的。实际情况是这样的:

​ Linus花了两周时间自己用C写了一个分布式版本控制系统,这就是Git!一个月之内,Linux系统的源码已经由Git管理了!牛是怎么定义的呢?大家可以体会一下。

​ Git迅速成为最流行的分布式版本控制系统,尤其是2008年,GitHub网站上线了,它为开源项目免费提供Git存储,无数开源项目开始迁移至GitHub,包括jQuery,PHP,Ruby等等。

历史就是这么偶然,如果不是当年BitMover公司威胁Linux社区,可能现在我们就没有免费而超级好用的Git了

1.3集中式和分布式

​ git:分布式

​ SVN:集中式【】

2.安装git

sudo apt-get install git

3.创建版本库
3.1版本库

版本库又被称为仓库,英文表示为repository,初期暂时可以理解为目录,该目录中的所有的文件都可以被git管理起来,每个文件的修改,创建,删除等所有的操作。git都能跟踪,记录相应的版本,方便可以随时恢复到某个版本

3.2创建版本库
#1.定位到指定的位置
	cd  xxx
#2.创建一个空目录
	mkdir   xxx
#3.定位到创建好的目录下
	cd xxx
#4.初始化仓库
	git init
  
#说明:创建了一个空仓库,仓库本身是抽象,使用目录进行模拟,该目录下有一个隐藏的目录.git[仓库]
#注意:.git是用来跟踪管理仓库的,不要手动修改其中的内容

演示命令:
lichongchong@ubuntu01:~$ cd Desktop/
lichongchong@ubuntu01:~/Desktop$ mkdir learngit
lichongchong@ubuntu01:~/Desktop$ cd learngit/
lichongchong@ubuntu01:~/Desktop/learngit$ git init
已初始化空的 Git 仓库于 /home/lichongchong/Desktop/learngit/.git/
lichongchong@ubuntu01:~/Desktop/learngit$ cat .git/
cat: .git/: 是一个目录
lichongchong@ubuntu01:~/Desktop/learngit$ ll
总用量 12
drwxr-xr-x 3 lichongchong lichongchong 4096 12月 20 10:42 ./
drwxr-xr-x 3 lichongchong lichongchong 4096 12月 20 10:40 ../
drwxr-xr-x 7 lichongchong lichongchong 4096 12月 20 10:42 .git/
3.3将文件添加到版本库

明确一点,所有的版本控制系统,只能跟踪文本文件的改动【创建文件,删除文件,修改文件,添加内容,删除内容等】

#1.修改文件【创建,删除,内容的增加和删除】
	vim  xxx
#2.添加到缓存区
	git add xxx
#3.提交到仓库
	git commit -m "提交的日志"
  
"""
注意问题:
1.一定要书写提交日志,方便后期可以回退版本
2.可以执行多次add,执行一次commit,每次只要使用commit命令,就相当于产生了一个历史版本
3.commit成功之后,会有信息提示
	changed【改动】
	insertion【插入】
	deletion【删除】
4.add指的是将改动添加到缓存区,commit指的是将缓存区的内容提交到仓库,目的为了保证本地的文本和仓库之间的同步
"""

演示命令:
lichongchong@ubuntu01:~/Desktop/learngit$ touch text.txt
lichongchong@ubuntu01:~/Desktop/learngit$ vim text.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ git add text.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ git commit -m
error: switch `m' requires a value
lichongchong@ubuntu01:~/Desktop/learngit$ git commit

*** 请告诉我你是谁。

运行

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

来设置您账号的缺省身份标识。
如果仅在本仓库设置身份标识,则省略 --global 参数。
lichongchong@ubuntu01:~/Desktop/learngit$ git commit -m "create a file and init"

*** 请告诉我你是谁。

运行

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

来设置您账号的缺省身份标识。
如果仅在本仓库设置身份标识,则省略 --global 参数。
fatal: 无法自动探测邮件地址(得到 'lichongchong@ubuntu01.(none)')
lichongchong@ubuntu01:~/Desktop/learngit$ git config --global user.email "18501970795@163.com"
lichongchong@ubuntu01:~/Desktop/learngit$ git config --global user.name "yangyang-git"
lichongchong@ubuntu01:~/Desktop/learngit$ git commit -m "create a file and init"[master(根提交) e8d0c97] create a file and init
 1 file changed, 2 insertions(+)
 create mode 100644 text.txt
lichongchong@ubuntu01:~/Desktop/learngit$ vim text.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ git add text.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ git commit -m "add abcd"
[master ea71e41] add abc

"""
问题:需要配置github中注册的账号
*** 请告诉我你是谁。

运行

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

来设置您账号的缺省身份标识。
如果仅在本仓库设置身份标识,则省略 --global 参数。
fatal: 无法自动探测邮件地址(得到 'lichongchong@ubuntu01.(none)')
lichongchong@ubuntu01:~/Desktop/learngit$ git config --global user.email "18501970795@163.com"    #邮箱
lichongchong@ubuntu01:~/Desktop/learngit$ git config --global user.name "yangyang-git"   #用户名
"""
4.时光穿梭机【覆水可收/回退版本】
#1.git status  查看仓库当前的状态
	#查看本地目录和仓库之间是否同步

  
#2.git diff    查看具体修改的内容 
	#different,不同,只能查看add之前本地文件的修改
    
 演示命令:
lichongchong@ubuntu01:~/Desktop/learngit$ git status
位于分支 master
尚未暂存以备提交的变更:
  (使用 "git add <文件>..." 更新要提交的内容)
  (使用 "git checkout -- <文件>..." 丢弃工作区的改动)

	修改:     text.txt

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
lichongchong@ubuntu01:~/Desktop/learngit$ git add text.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ git status
位于分支 master
要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

	修改:     text.txt

lichongchong@ubuntu01:~/Desktop/learngit$ git commit -m "delete 123"
[master 3967c38] delete 123
 1 file changed, 1 insertion(+), 1 deletion(-)
lichongchong@ubuntu01:~/Desktop/learngit$ git status
位于分支 master
无文件要提交,干净的工作区
lichongchong@ubuntu01:~/Desktop/learngit$ git diff
lichongchong@ubuntu01:~/Desktop/learngit$ vim text.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ git diff
diff --git a/text.txt b/text.txt
index 733a00c..b001677 100644
--- a/text.txt
+++ b/text.txt
@@ -1,4 +1,4 @@
-hello
+hello123
 1234
 abcd
 yyyyy
lichongchong@ubuntu01:~/Desktop/learngit$ git diff text.txt
diff --git a/text.txt b/text.txt
index 733a00c..b001677 100644
--- a/text.txt
+++ b/text.txt
@@ -1,4 +1,4 @@
-hello
+hello123
 1234
 abcd
 yyyyy
lichongchong@ubuntu01:~/Desktop/learngit$ git add text.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ git diff
lichongchong@ubuntu01:~/Desktop/learngit$ git commit -m "hfjahd"
[master 9f59b7f] hfjahd
 1 file changed, 1 insertion(+), 1 deletion(-)
4.1版本回退

不断地修改文件,然后不断的提交到仓库,

如果想要回退到某个版本,应该怎么做?

工作原理:每次commit,相当于保存了一个快照

如果误删了某个文件或者修改错了某个文件,都可以从最近的commit恢复,根据每次提交的commit id找到某个历史版本

#1.查看提交日志
	git log
 	git log --pretty=oneline 
    #包括commit id,提交的用户,提交日期,提交日志
    
#2.回退版本
#HEAD指向的是最新版本,上一个版本HEAD^,上上一个版本HEAD^^,上到100个版本HEAD~100
#一般情况下,只有上一个版本使用HEAD^表示,其他的版本统统使用commit id
	#回退到上一个版本
  	git reset --hard HEAD^
    #回到某个具体的版本
    git reset --hard commitid   
    #回到未来的版本
    git reset --hard 未来的commitid
    
#3.查看git中执行过的命令
	git reflog
    
#问题说明:如果不知道版本号,可以通过git reflog查询
#回退版本之前,尽量先使用git log或者git log --pretty=oneline 查看历史版本
#要重返未来,用git reflog查看在git中执行过的命令

演示命令:
lichongchong@ubuntu01:~/Desktop/learngit$ git log
commit 9f59b7f3a7c90febfe610f2813eb21ff9da873b6 (HEAD -> master)
Author: yangyang-git <18501970795@163.com>
Date:   Thu Dec 20 11:19:45 2018 +0800

    hfjahd

commit 3967c38dfd901926bc0ee31e8793cb49717f0f20
Author: yangyang-git <18501970795@163.com>
Date:   Thu Dec 20 11:14:13 2018 +0800

    delete 123

commit ef972bce04327838da9f169c5a3b4a7a366cd028
Author: yangyang-git <18501970795@163.com>
Date:   Thu Dec 20 11:02:22 2018 +0800

    modify text.txt

commit ea71e41131b88c485e1b665ca276f50f2245aae8
Author: yangyang-git <18501970795@163.com>
Date:   Thu Dec 20 11:00:05 2018 +0800

    add abcd

lichongchong@ubuntu01:~/Desktop/learngit$ git log --pretty=oneline
9f59b7f3a7c90febfe610f2813eb21ff9da873b6 (HEAD -> master) hfjahd
3967c38dfd901926bc0ee31e8793cb49717f0f20 delete 123
ef972bce04327838da9f169c5a3b4a7a366cd028 modify text.txt
ea71e41131b88c485e1b665ca276f50f2245aae8 add abcd
e8d0c977d36f85ea821ed94679307a78e4d7bd7f create a file and init
lichongchong@ubuntu01:~/Desktop/learngit$ git reset --hard HEAD^
HEAD 现在位于 3967c38 delete 123
lichongchong@ubuntu01:~/Desktop/learngit$ git log --pretty=oneline
3967c38dfd901926bc0ee31e8793cb49717f0f20 (HEAD -> master) delete 123
ef972bce04327838da9f169c5a3b4a7a366cd028 modify text.txt
ea71e41131b88c485e1b665ca276f50f2245aae8 add abcd
e8d0c977d36f85ea821ed94679307a78e4d7bd7f create a file and init
lichongchong@ubuntu01:~/Desktop/learngit$ vim text.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ cat text.txt 
hello
1234
abcd
yyyyy
lichongchong@ubuntu01:~/Desktop/learngit$ git reset --hard 9f59b7
HEAD 现在位于 9f59b7f hfjahd
lichongchong@ubuntu01:~/Desktop/learngit$ git log --pretty=oneline
9f59b7f3a7c90febfe610f2813eb21ff9da873b6 (HEAD -> master) hfjahd
3967c38dfd901926bc0ee31e8793cb49717f0f20 delete 123
ef972bce04327838da9f169c5a3b4a7a366cd028 modify text.txt
ea71e41131b88c485e1b665ca276f50f2245aae8 add abcd
e8d0c977d36f85ea821ed94679307a78e4d7bd7f create a file and init
lichongchong@ubuntu01:~/Desktop/learngit$ git reset --hard ea71e411
HEAD 现在位于 ea71e41 add abcd
lichongchong@ubuntu01:~/Desktop/learngit$ git log --pretty=oneline
ea71e41131b88c485e1b665ca276f50f2245aae8 (HEAD -> master) add abcd
e8d0c977d36f85ea821ed94679307a78e4d7bd7f create a file and init
lichongchong@ubuntu01:~/Desktop/learngit$ git reset --hard 9f59b7
HEAD 现在位于 9f59b7f hfjahd
lichongchong@ubuntu01:~/Desktop/learngit$ git reflog
9f59b7f (HEAD -> master) HEAD@{0}: reset: moving to 9f59b7
ea71e41 HEAD@{1}: reset: moving to ea71e411
9f59b7f (HEAD -> master) HEAD@{2}: reset: moving to 9f59b7
3967c38 HEAD@{3}: reset: moving to HEAD
4.2版本库/工作区/暂存区

工作区:working directory,在当前计算机中创建的目录【learngit】

版本库:Repository,仓库,工作区中有一个隐藏的目录.git

暂存区:使用git add命令将文件的修改添加到暂存区,直到git commit将暂存区中的内容全部一次性提交到仓库

工作流程:工作区中进行开发--------》add到暂存区------》commit到版本库

master:创建版本库的时候,git会默认帮我们创建一个主分支master,所以此时的commit全部提交到版本库中的master分支上

演示命令:
lichongchong@ubuntu01:~/Desktop/learngit$ touch file1.txt
lichongchong@ubuntu01:~/Desktop/learngit$ vim file1.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ git status
位于分支 master
未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)

	file1.txt

提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
lichongchong@ubuntu01:~/Desktop/learngit$ git add file1.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ git status
位于分支 master
要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

	新文件:   file1.txt

lichongchong@ubuntu01:~/Desktop/learngit$ vim text.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ git status
位于分支 master
要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

	新文件:   file1.txt

尚未暂存以备提交的变更:
  (使用 "git add <文件>..." 更新要提交的内容)
  (使用 "git checkout -- <文件>..." 丢弃工作区的改动)

	修改:     text.txt

lichongchong@ubuntu01:~/Desktop/learngit$ git add text.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ git commit -m "modify text and crate file1"
[master 2eaebd1] modify text and crate file1
 2 files changed, 2 insertions(+)
 create mode 100644 file1.txt
lichongchong@ubuntu01:~/Desktop/learngit$ git status
位于分支 master
无文件要提交,干净的工作区
4.3管理修改

git跟踪的是修改,并非文件

什么是修改?新增一行,删除一行,更改某个字符,删了一些内容右加了会去。。。。

实际使用命令:git add 文件

​ git commit -m “xx”

演示命令:
lichongchong@ubuntu01:~/Desktop/learngit$ git status
位于分支 master
无文件要提交,干净的工作区
lichongchong@ubuntu01:~/Desktop/learngit$ cat text.txt 
hello123
1234
abcd
yyyyy
last
lichongchong@ubuntu01:~/Desktop/learngit$ vim text.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ cat text.txt 
hello123
1234
abcd
yyyyy
last
last1111
lichongchong@ubuntu01:~/Desktop/learngit$ git add text.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ git status
位于分支 master
要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

	修改:     text.txt

lichongchong@ubuntu01:~/Desktop/learngit$ vim text.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ cat text.txt 
hello123
1234
abcd
yyyyy
last
last1111
last2222
lichongchong@ubuntu01:~/Desktop/learngit$ git commit -m "last"
[master 4dff2f6] last
 1 file changed, 1 insertion(+)
lichongchong@ubuntu01:~/Desktop/learngit$ git status
位于分支 master
尚未暂存以备提交的变更:
  (使用 "git add <文件>..." 更新要提交的内容)
  (使用 "git checkout -- <文件>..." 丢弃工作区的改动)

	修改:     text.txt

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
lichongchong@ubuntu01:~/Desktop/learngit$ git diff HEAD -- text.txt
diff --git a/text.txt b/text.txt
index 184ca77..e17c743 100644
--- a/text.txt
+++ b/text.txt
@@ -4,3 +4,4 @@ abcd
 yyyyy
 last
 last1111
+last2222
lichongchong@ubuntu01:~/Desktop/learngit$ git add text.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ git commit -m "222"
[master 41decc7] 222
 1 file changed, 1 insertion(+)
lichongchong@ubuntu01:~/Desktop/learngit$ git status
位于分支 master
无文件要提交,干净的工作区
4.4撤销修改

a.改动了工作区,没有add,也没有commit

#git checkout -- file1.txt  撤销工作区

演示命令:
lichongchong@ubuntu01:~/Desktop/learngit$ vim file1.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ git status
位于分支 master
尚未暂存以备提交的变更:
  (使用 "git add <文件>..." 更新要提交的内容)
  (使用 "git checkout -- <文件>..." 丢弃工作区的改动)

	修改:     file1.txt

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
lichongchong@ubuntu01:~/Desktop/learngit$ cat file1.txt 
ryeury
stupid boss
lichongchong@ubuntu01:~/Desktop/learngit$ git checkout -- file1.txt
lichongchong@ubuntu01:~/Desktop/learngit$ cat file1.txt 
ryeury

b.改动了工作区,进行了add,但是还没有commit

#1.git reset HEAD file1.txt	  撤销暂存区
#2.git checkout -- file1.txt   撤销工作区

演示命令:
lichongchong@ubuntu01:~/Desktop/learngit$ vim file1.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ cat file1.txt 
ryeury
stupid boss
lichongchong@ubuntu01:~/Desktop/learngit$ git add file1.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ git status
位于分支 master
要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

	修改:     file1.txt

lichongchong@ubuntu01:~/Desktop/learngit$ git reset HEAD file1.txt
重置后取消暂存的变更:
M	file1.txt
lichongchong@ubuntu01:~/Desktop/learngit$ git status
位于分支 master
尚未暂存以备提交的变更:
  (使用 "git add <文件>..." 更新要提交的内容)
  (使用 "git checkout -- <文件>..." 丢弃工作区的改动)

	修改:     file1.txt

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
lichongchong@ubuntu01:~/Desktop/learngit$ git checkout -- file1.txt
lichongchong@ubuntu01:~/Desktop/learngit$ cat file1.txt 
ryeury

c.改动了工作区,进行了add,也进行了commit

#git reset --hard HEAD^     回退版本

演示命令:
lichongchong@ubuntu01:~/Desktop/learngit$ vim file1.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ git add file1.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ git commit -m "boss"
[master 07714d4] boss
 1 file changed, 1 insertion(+)
lichongchong@ubuntu01:~/Desktop/learngit$ git reset --hard HEAD^
HEAD 现在位于 41decc7 222
lichongchong@ubuntu01:~/Desktop/learngit$ cat file1.txt 
ryeury
4.5删除文件

删除文件对于git而言,被认为是一个修改

#1.删除工作区中的文件
	rm -rf filename
#2.删除版本库中的文件
	git rm filename
#3.将删除的修改提交到版本库
	git commit -m "xxx"
#4.查看版本库的状态
	git status
  
演示命令:
lichongchong@ubuntu01:~/Desktop/learngit$ rm file1.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ ls
text.txt
lichongchong@ubuntu01:~/Desktop/learngit$ git status
位于分支 master
尚未暂存以备提交的变更:
  (使用 "git add/rm <文件>..." 更新要提交的内容)
  (使用 "git checkout -- <文件>..." 丢弃工作区的改动)

	删除:     file1.txt

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
lichongchong@ubuntu01:~/Desktop/learngit$ git rm file1.txt
rm 'file1.txt'
lichongchong@ubuntu01:~/Desktop/learngit$ git status
位于分支 master
要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

	删除:     file1.txt

lichongchong@ubuntu01:~/Desktop/learngit$ git commit -m "delete file1.txt"
[master aabf263] delete file1.txt
 1 file changed, 1 deletion(-)
 delete mode 100644 file1.txt
lichongchong@ubuntu01:~/Desktop/learngit$ git status
位于分支 master
无文件要提交,干净的工作区
lichongchong@ubuntu01:~/Desktop/learngit$ 
  
  
#需求:在工作区中删除了文件,误删了,要恢复
#在git中,无论是内容还是文件,不管做什么样的操作,都可以一键复原
演示命令:
lichongchong@ubuntu01:~/Desktop/learngit$ touch newfile.txt
lichongchong@ubuntu01:~/Desktop/learngit$ vim newfile.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ ls
newfile.txt  text.txt
lichongchong@ubuntu01:~/Desktop/learngit$ git status
位于分支 master
未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)

	newfile.txt

提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
lichongchong@ubuntu01:~/Desktop/learngit$ git add newfile.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ git commit -m "create newfile"
[master da2afdd] create newfile
 1 file changed, 1 insertion(+)
 create mode 100644 newfile.txt
lichongchong@ubuntu01:~/Desktop/learngit$ git status
位于分支 master
无文件要提交,干净的工作区
lichongchong@ubuntu01:~/Desktop/learngit$ rm newfile.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ git checkout -- newfile.txt     #丢弃工作区的改动
lichongchong@ubuntu01:~/Desktop/learngit$ ls
newfile.txt  text.txt
5.远程仓库
5.1准备工作
第一步:创建github账号

第二步:将本地仓库和远程仓库连接起来

	1>生成ssh key 【建立了本机和远程服务器之间的连接】

		ssh-keygen   -t   rsa   -C  "git注册邮箱地址"

	2>验证ssh key是否添加成功

		ssh -T git@github.com
第三步:在github中创建空远程仓库

演示命令:
lichongchong@ubuntu01:~/Desktop/learngit$ ssh-keygen -t rsa -C "18501970795@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/lichongchong/.ssh/id_rsa): 
Created directory '/home/lichongchong/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/lichongchong/.ssh/id_rsa.
Your public key has been saved in /home/lichongchong/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:lD587htbWYq6XfdDpmSkB9LEgPRt0KtY5wv6YOD2pso 18501970795@163.com
The key's randomart image is:
+---[RSA 2048]----+
|      ...o+      |
|       ...o+     |
|        o.oo.    |
|       + o.= .   |
|     .  S * + .  |
|    . .. * + B o |
|     o o. = X =  |
|  . . oo.+ * o o |
|   E..o.+o=.    o|
+----[SHA256]-----+
lichongchong@ubuntu01:~/Desktop/learngit$ cd ..
lichongchong@ubuntu01:~/Desktop$ cd ..
lichongchong@ubuntu01:~$ ls
Desktop    Downloads  Pictures  PycharmProjects  Templates
Documents  Music      Public    Software         Videos
lichongchong@ubuntu01:~$ ls -a
.              Downloads       .nano             .sogouinput
..             .gconf          .pam_environment  .ssh
.bash_history  .gitconfig      Pictures          .sudo_as_admin_successful
.bash_logout   .gnome          .pki              .sunpinyin
.bashrc        .gnupg          .presage          Templates
.cache         .ICEauthority   .profile          Videos
.compiz        .java           Public            .viminfo
.config        .local          .PyCharm2017.3    .virtualenvs
.dbus          .mozilla        PycharmProjects   .Xauthority
Desktop        Music           .python_history   .xinputrc
.dmrc          .mysql          .selected_editor  .xsession-errors
Documents      .mysql_history  Software          .xsession-errors.old
lichongchong@ubuntu01:~$ cd .ssh/
lichongchong@ubuntu01:~/.ssh$ ls
id_rsa  id_rsa.pub
lichongchong@ubuntu01:~/.ssh$ cat id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCcZRSptx+O50fE9hy9a2czH50HDGmG6aMDv1OR9HyberJFGQf6RQh6bkuQYm0bs3Mgp+dLn1TSItaFo62FtqV0mE+xgnkcNlvYgjGSWD1K3Xg5N3Wzwg4r28c3QYLNsv6rhkaPAxrnrUQmrU+8o1dZR8wL6Q6xseQwm0PAgqT5XJPK6ACpTa5ookOqcfYrirt8ZkKkUtiqgcDyNHXGwefNoR3dsNFTRqF01XE8GgrdP/5Gyk+GBLRXPvyIZ/hoMPm/7FvjeIYK+sieFuvWwUOckXF8BcM+RIcuLAkGTt9VqjWeeAZ5LbV2IKcYFmmnkL3vIyzgovfWoq3nodelNX/r 18501970795@163.com
lichongchong@ubuntu01:~/.ssh$ ssh -T git@github.com
The authenticity of host 'github.com (13.250.177.223)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts.
Hi yangyang-git! You've successfully authenticated, but GitHub does not provide shell access.
5.2添加远程仓库【从本地-----》远程】
#1.建立了本地仓库和远程仓库之间的连接
	git remote add origin git@github.com:用户名/远程仓库名.git
    #远程仓库名最好和本地仓库名保持一致

演示命令:
lichongchong@ubuntu01:~$ cd Desktop/
lichongchong@ubuntu01:~/Desktop$ cd learngit/
lichongchong@ubuntu01:~/Desktop/learngit$ git remote add origin git@github.com:yangyang-git/learngitPython1811.git

#origin表示的是远程仓库中的主分支,对应的是本地仓库中的master,origin可以是别的名称,但是建议使用origin,

#2.将本地仓库中的内容推送到远程仓库
	git push -u origin master
  
#3.修改工作区中的内容,然后和远程仓库之间进行同步
	git add
  	git commit
    git push origin master
    
    
演示命令:
lichongchong@ubuntu01:~/Desktop/learngit$ ls
newfile.txt  text.txt
lichongchong@ubuntu01:~/Desktop/learngit$ git push -u origin master
Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.
枚举对象: 28, 完成.
对象计数中: 100% (28/28), 完成.
使用 2 个线程进行压缩
压缩对象中: 100% (14/14), 完成.
写入对象中: 100% (28/28), 2.22 KiB | 758.00 KiB/s, 完成.
总共 28 (差异 1),复用 0 (差异 0)
remote: Resolving deltas: 100% (1/1), done.
To github.com:yangyang-git/learngitPython1811.git
 * [new branch]      master -> master
分支 'master' 设置为跟踪来自 'origin' 的远程分支 'master'。
lichongchong@ubuntu01:~/Desktop/learngit$ vim text.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ git add text.txt 
lichongchong@ubuntu01:~/Desktop/learngit$ git commit  -m "hjghr"
[master a661fd8] hjghr
 1 file changed, 1 insertion(+)
lichongchong@ubuntu01:~/Desktop/learngit$ git push origin master
Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.
枚举对象: 5, 完成.
对象计数中: 100% (5/5), 完成.
使用 2 个线程进行压缩
压缩对象中: 100% (3/3), 完成.
写入对象中: 100% (3/3), 317 bytes | 317.00 KiB/s, 完成.
总共 3 (差异 0),复用 0 (差异 0)
To github.com:yangyang-git/learngitPython1811.git
   da2afdd..a661fd8  master -> master
5.3克隆仓库【从远程----》本地】

模拟从零开发:最好是先创建远程仓库,再从远程仓库进行克隆本地仓库

#1.在github中创建一个仓库,含有一个README.md文件

#2.从github将远程仓库克隆到本地指定的路径下
	git clone git@github.com:用户名/c远程仓库的名称.git
        
演示命令:
lichongchong@ubuntu01:~/Desktop/learngit$ cd ..
lichongchong@ubuntu01:~/Desktop$ git clone git@github.com:yangyang-git/check.git
正克隆到 'check'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
接收对象中: 100% (3/3), 完成.
lichongchong@ubuntu01:~/Desktop$ 
6.分支管理

默认的工作都是在master上完成的,但是,实际开发中,不在master上开发,只用来发布版本

根据不同的任务或者不同的人,可以创建多个不同的分支,

6.1创建分支和合并分支

master:主分支,其他的都被称为xx分支

master分支是一条线,git用master指向最新的提交,再用HEAD指向master

#1.创建子分支并切换到子分支
	git checkout -b xxx
#2.在子分支下进行工作
	git add
  	git commit
#3.查看当前正在工作的分支
	git branch 
#4.切换到主分支
	git checkout master
#5.将子分支合并到主分支
	git merge xxx
#6.删除子分支
	git branch -d xxx  
"""
注意问题:
1.当第一次创建子分支并且需要切换,添加-b选项
  其他时候只需要切换分支,使用git checkout 分支名
  
2.想让主分支上内容和子分支上的内容同步,则需要将子分支合并到主分支

3.只有当确认子分支没用的时候,就可以删除子分支

4.推荐大量使用分支,尽量不要在master上直接开发
"""

演示命令:
lichongchong@ubuntu01:~/Desktop/check$ git chekcout -b dev
git:'chekcout' 不是一个 git 命令。参见 'git --help'。

最相似的命令是
	checkout
lichongchong@ubuntu01:~/Desktop/check$ git checkout -b dev
切换到一个新分支 'dev'
lichongchong@ubuntu01:~/Desktop/check$ git branch
* dev
  master
lichongchong@ubuntu01:~/Desktop/check$ touch file1.txt
lichongchong@ubuntu01:~/Desktop/check$ vim file1.txt `
> ^C
lichongchong@ubuntu01:~/Desktop/check$ vim file1.txt
lichongchong@ubuntu01:~/Desktop/check$ git add file1.txt 
lichongchong@ubuntu01:~/Desktop/check$ git commit  -m "create file1"
[dev 3eed526] create file1
 1 file changed, 1 insertion(+)
 create mode 100644 file1.txt
lichongchong@ubuntu01:~/Desktop/check$ git checkout master
切换到分支 'master'
您的分支与上游分支 'origin/master' 一致。
lichongchong@ubuntu01:~/Desktop/check$ git branch
  dev
* master
lichongchong@ubuntu01:~/Desktop/check$ git merge dev
更新 4705b95..3eed526
Fast-forward
 file1.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 file1.txt
lichongchong@ubuntu01:~/Desktop/check$ ls
file1.txt  README.md
lichongchong@ubuntu01:~/Desktop/check$ cat file1.txt 
heloo
lichongchong@ubuntu01:~/Desktop/check$ git branch -d dev
已删除分支 dev(曾为 3eed526)。
lichongchong@ubuntu01:~/Desktop/check$ git branch
* master
  • 29
    点赞
  • 180
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值