注:来源https://www.liaoxuefeng.com/
简单知识:
1.git是用C语言写的
2.git是分布式版本控制系统:每个人的电脑上都有最全的版本库,如果多人合作,只需将修改后的版本库相互交换即可。
3.版本控制系统只能跟踪文本文件的改动。文本编码语言建议UTF-8。
4.Windows下不建议使用默认记事本,可使用替代品如:Notepad++。
安装git,并在git batch中记录姓名与邮箱(如:zxj&147737)
$ git config --global user.name "Your Name"
$ git config --global user.email "email@example.com"
创建repository
1 首先创建一个空目录
$ mkdir GitLearn
$ cd GitLearn
$ pwd
/e/GitLearn
2 将目录变成仓库
$ git init
Initialized empty Git repository in E:/GitLearn/.git/
添加文件至版本库
1 将需要添加的文件readme.txt放入GitLearn文件夹下,然后使用add commit两个人命令完成添加
$ git add readme.txt //将文件添加至仓库,可以添加多次后一次提交
$ git commit -m "wrote a readme file" //将文件提交至仓库,引号是此次提交的说明
2 修改文件内容,查看git状态,提醒文件已修改未提交
$ 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")
3 查看修改内容
$ git diff
diff --git a/readme.txt b/readme.txt
index d8036c1..7b4104a 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,2 @@
-Git is a version control system. //减号“-”表示被修改内容
+Git is a distributed version control system. //加号“+”表示修改后内容
Git is free software. //没有被修改
\ No newline at end of file
4 将修改后的文件添加并提交
$ git add readme.txt
$ git commit -m "add distributed"
5 尝试复习上文的修改,然后添加提交
6 使用log命令查看每次修改的说明。文件版本总是从最新的开始
$ git log --pretty=oneline
cd5aecc536a4dc870cec56af299c1f3da82ae4ce (HEAD -> master) append GPL //当前版本标记:HEAD
ebbf0fd8ecd6b3b1db15c8996dec6cd313e0f494 add distributed //上一个版本:HEAD^
4bfc1c0a6027d27651fac34e3c463b5635d64ee8 wrote a readme file //上上一个版本:HEAD^^
//以此类推,前100个版本为HEAD~100
7 当前版本回退至上一版本,使用命令reset
$ git reset --hard HEAD^ //回退至上一版本,最新版本将被消除
HEAD is now at ebbf0fd add distributed
根据commit id 也可以到达要求的版本
$ git reset --hard cd5a
HEAD is now at cd5aecc append GPL
查看是否为真,查看内容cat
$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
如果不知道commit id ,使用reflog命令可查看使用的命令
$ git reflog
cd5aecc (HEAD -> master) HEAD@{0}: reset: moving to cd5a
ebbf0fd HEAD@{1}: reset: moving to HEAD^
cd5aecc (HEAD -> master) HEAD@{2}: commit: append GPL
ebbf0fd HEAD@{3}: commit: add distributed
4bfc1c0 HEAD@{4}: commit (initial): wrote a readme file