学习git笔记
**加粗**
、*斜体*
、~~删除线~~`` \
这是单行代码`
在git shell里选一个根目录建立文件夹
$ mkdir learngit
$ cd learngit
$ pwd
/Users/michael/learngit通过git init把这个目录变成git可以管理的仓库
$ git init
Initialized empty Git repository in /Users/michael/learngit/.git/在该文件夹中建立文件,新建即可,把文件添加到版本库,注意,所有版本控制系统只能跟踪文本文件的改动,且由于Word是二进制文件,所有也没办法跟踪。
第一步,用命令git add告诉Git,把文件添加到仓库:
$ git add readme.txt
第二步,用命令git commit告诉Git,把文件提交到仓库:
$ git commit -m "wrote a readme file"
[master (root-commit) cb926e7] wrote a readme file
1 file changed, 2 insertions(+)
create mode 100644 readme.txt
-
查看git 状态
git statusOn 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")
git diff readme.md
diff --git a/readme.txt b/readme.txt
index 46d49bf..9247db6 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. -
提交修改和提交新文件是一样的
- git add readme.md
- git status
- git commit -m "add distributed"
- git status
-
查看历史记录
git log
commit 890d04e4caef8c2b72e79bebf735221f1f40af3a
Author: CooperNiu niuyanno.1@outlook.com
Date: Tue Feb 27 18:05:50 2018 +0800append GPL
commit 5dd3c0826457cd692a1831ec5ce151497a9254f3
Author: CooperNiu niuyanno.1@outlook.com
Date: Tue Feb 27 18:04:42 2018 +0800add distributed
commit b18929832dcb6ca954e26c60eb377773afcfab79
Author: CooperNiu niuyanno.1@outlook.com
Date: Tue Feb 27 17:59:20 2018 +0800wrote a readme file
-
回退到上一个版本
现在我们启动时光穿梭机,准备把readme.txt回退到上一个版本,也就是“add distributed”的那个版本,怎么做呢?首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,也就是最新的提交3628164...882e1e0(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD,上上一个版本就是HEAD,当然往上100个版本写100个比较容易数不过来,所以写成HEAD~100。
现在,我们要把当前版本“append GPL”回退到上一个版本“add distributed”,就可以使用git reset命令:
git reset --hard HEAD^
HEAD is now at 5dd3c08 add distributed
~\Documents\GitHub\learngit [master +1 ~0 -0 !]> cat readme.md
Git is a distributed version control system.
Git is free software.** git log**
commit 5dd3c0826457cd692a1831ec5ce151497a9254f3
Author: CooperNiu niuyanno.1@outlook.com
Date: Tue Feb 27 18:04:42 2018 +0800add distributed
commit b18929832dcb6ca954e26c60eb377773afcfab79
Author: CooperNiu niuyanno.1@outlook.com
Date: Tue Feb 27 17:59:20 2018 +0800wrote a readme file
最新的版本append GPL已经看不见了!但如果想退回去怎么办?
-
撤销回退
通过未来版本的id
** git reset --hard 5dd3c082
HEAD is now at 3628164 append GPL需要注意的是版本号没必要写全,前几位就可以,git会自动去找,只要够git区分开不同的版本即可。
如果是回退了之后,关机,无法看见版本号,还可以用reflog来查看输过的命令,从中找到版本号。
git reflog
5dd3c08 HEAD@{0}: reset: moving to HEAD^
890d04e HEAD@{1}: commit: append GPL
5dd3c08 HEAD@{2}: commit: add distributed
b189298 HEAD@{3}: commit (initial): wrote a readme file