git在windows上的安装就不说了,下载了安装包无脑点下一步就行。
在 开始菜单 -> Git -> Git Bash 打开一个类似linux的终端
写在前面:命令前面带有一个 $ 符号的,是linux终端的提示符,实际输入命令的时候不用输入 $ 这个符号,防止有些不懂linux的小白吧=,=
一:用户配置
1.配置用户名: git config --global user.name "你的用户名"
$ git config --global user.name "gsp1004"
2.配置你的注册邮箱:git config --global user.email "你的注册邮箱"
$ git config --global user.email "123456789@qq.com"
二:新建仓库
1.在合适的地方新建一个文件夹
例如我创建的目录是: /c/Users/gsp/Desktop/git/git-test
2.进入上面的目录中
$ cd /c/Users/gsp/Desktop/git/git-test
3.将这个目录设置成仓库
$ git init
Initialized empty Git repository in C:/Users/gsp/Desktop/git/git-test/.git/
这个时候会在目录中生成一个隐藏目录 .git,千万不要去动这个目录
三:把文件添加到仓库
下面的例子以test.txt为例
1.在仓库目录中创建一个空文件test.txt,可以用windows右键,新建,也可以使用下面的linux命令(如果是其他的已经写好的文件,拖到这个仓库文件夹中即可)
$ touch test.txt
此过程可以理解成将test.txt文件放到了仓库的门口,但是你还没有告诉仓库的管理员说你要保存这个文件。下面的步骤就是告诉仓库管理员,让他给你管理这个文件
2.将此文件添加到仓库中进行管理
$ git add test.txt
3.修改正式提交,并添加注释,你加入/删除/修改 某个文件到底是做了什么工作,写一些描述好让自己和别人以后看的时候容易懂啊
$ git commit -m "测试如何使用git将文件添加到仓库"
[master (root-commit) efc5b66] 测试如何使用git将文件添加到仓库
1 file changed, 1 insertion(+)
create mode 100644 test.txt
四:修改test.txt,提交修改后的test.txt
1.使用命令查看当前仓库状态
$ git status
On branch master
nothing to commit, working tree clean
显示当前状态是:在主干上。无需提交,工作树干净
2.修改test.txt,往文件末尾添加当前时间
$ date >> test.txt
3.再次查看仓库状态
$ 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.txt
no changes added to commit (use "git add" and/or "git commit -a")
google翻译如下:
未提交更改的更改:
(使用“git add <file> ...”来更新将要提交的内容)
(使用“git checkout - <file> ...”来丢弃工作目录中的更改)
修改:test.txt
没有更改添加到提交(使用“git add”和/或“git commit -a”)
此时git告知我们test.txt文件被修改了。
4.查看具体修改了写什么内容(但是一般不常用,代码修改了那么多,这样去看修改的地方是要疯的)
$ git diff
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory
diff --git a/test.txt b/test.txt
index eb03bc8..6ea9bb8 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
2019年05月31日 15:47:12
+2019年05月31日 16:03:26
从上面的信息可以看到,修改的内容是增加了 “2019年05月31日 16:03:26” 这一行(前面的+表示增加了这一行)
5.把上述的修改重新添加到仓库中去
上面仓库管理员不是发现test.txt文件和之前不一样了么,这个时候你要告诉他,说:哥,没事儿,这个文件是我修改的,我就是要把他修改成这个样子,以后这个文件里面的内容就是这样子的。
$ git add test.txt
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory
6.再次正式提交
$ git commit -m "第二次修改"
[master a0752b8] 第二次修改
1 file changed, 1 insertion(+)
7.再次查看仓库状态
$ git status
On branch master
nothing to commit, working tree clean
8.查看提交日志:
$ git log
commit a0752b8d68d68076ed17a6c636ac225bbbe52014 (HEAD -> master)
Author: gsp1004 <564170877@qq.com>
Date: Fri May 31 16:20:14 2019 +0800
第二次修改
commit efc5b6658e24e783858c75fa3f1d429e8dda9acf
Author: gsp1004 <564170877@qq.com>
Date: Fri May 31 15:53:53 2019 +0800
测试如何使用git将文件添加到仓库
一行显示
$ git log --pretty=oneline
a0752b8d68d68076ed17a6c636ac225bbbe52014 (HEAD -> master) 第二次修改
efc5b6658e24e783858c75fa3f1d429e8dda9acf 测试如何使用git将文件添加到仓库
五:版本回退
1.回退到上一个版本:
$ git reset --hard HEAD^
HEAD is now at efc5b66 测试如何使用git将文件添加到仓库
2.回退到上上个版本:
$ git reset --hard HEAD^^
或者
$ git reset --hard HEAD-2
3.回退到指定版本
git reset --hard 版本编号(版本编号就是git log --pretty=oneline 前面的那一长串数字)
例如:我现在又想变成“第二次修改”的代码:
$ git reset --hard a0752b8d68d68076ed17a6c636ac225bbbe52014
HEAD is now at a0752b8 第二次修改
六:查看每一次的版本修改记录
$ git reflog
a0752b8 (HEAD -> master) HEAD@{0}: reset: moving to a0752b8d68d68076ed17a6c636ac225bbbe52014
efc5b66 HEAD@{1}: reset: moving to HEAD^
a0752b8 (HEAD -> master) HEAD@{2}: commit: 第二次修改
efc5b66 HEAD@{3}: commit (initial): 测试如何使用git将文件添加到仓库
七:checkout
未完待续。。。