教程来源于https://www.liaoxuefeng.com/wiki/896043488029600/896827951938304
1.安装git
以Windows为例,安装msysgit,官网地址https://gitforwindows.org/。msysgit以命令行操作为主,图形化操作TortoiseGit后续总结。
2. 创建版本库
选定一个目录,右键选中“GIT Bash Here”,会出现命令行(本机示例为路径为E:Work)
在命令行输入
$ mkdir LearnGit
$ cd LearnGit
$ pwd
/e/Work/LearnGit
$ git init
Initialized empty Git repository in E:/Work/LearnGit/.git/
上述命令创建了一个名为LearnGit,并且通过git init命令把这个目录变成Git可以管理的仓库(当前目录下会多了一个.git的目录,是Git来跟踪管理版本库的,不要手动修改这个目录里面的文件,不然改乱了,就把Git仓库给破坏了)。
3. 添加(add)和提交(commit)文件
第一步,在LearnGit目录下新建文件GitTest.txt,输入内容“This is my first gittest.”
第二步,用命令git add告诉Git,把文件添加到仓库
$ git add GitTest.txt
第三步,用命令git commit告诉Git,把文件提交到仓库:
$ git commit -m "First GitTest"
[master (root-commit) e01e07a] First GitTest
1 file changed, 1 insertion(+)
create mode 100644 GitTest.txt
-m后面输入的是本次提交的说明,可以输入任意内容,当然最好是有意义的,这样你就能从历史记录里方便地找到改动记录。
4. 提交改动
修改GitTest.txt的内容,添加一行文字“This is my first modify.”
然后在命令行输入git status查看文件状态
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: GitTest.txt
no changes added to commit (use "git add" and/or "git commit -a")
可以看到提示GitTest.txt已被修改,然后可以使用git diff查看差异
$ git diff
diff --git a/GitTest.txt b/GitTest.txt
index cea9dbd..cf91a5b 100644
--- a/GitTest.txt
+++ b/GitTest.txt
@@ -1 +1,2 @@
-This is my first gittest.
No newline at end of file
+This is my first gittest.
+This is my first modify.
No newline at end of file
然后重复提交操作,先add再commit
$ git add GitTest.txt
$ git commit -m "First Modify"
[master 15c7ddd] First Modify
1 file changed, 2 insertions(+), 1 deletion(-)