前言:用Git做过一些项目,但是只是会用一些常用功能。没有真正的涉及复杂分支操作。花些时间系统学习一下Git的理论知识,结合实践,提高水平。
本篇学习Git的基础支持。基于官网提供的中文版入门书籍Pro Git的章节Git基础。
获取 Git 仓库
使用clone命令克隆一个现有的远程仓库,默认clone命令会把远程仓库的所有版本的文件都克隆到本地。如下的示例是克隆一个Github的远程仓库到本地。成功后会在本地建立一个名为GitTestProject的文件夹。首次执行时会提示输入Github的账号密码。
$ git clone https://github.com/Kutilion/GitTestProject.git
Cloning into 'GitTestProject'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
记录每次更新到仓库
在这个工程根目录下添加一个文件
echo 'My project' > Information.txt
使用git status命令确认这个文件的状态发现,这个文件没有被git跟踪
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
Information.txt
nothing added to commit but untracked files present (use "git add" to track)
使用add命令,把文件添加到缓存,进行跟踪
$ git add Information.txt
warning: LF will be replaced by CRLF in Information.txt.
The file will have its original line endings in your working directory
再次确认这个文件的状态
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: Information.txt
修改既存的文件README.md后确认其状态
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: Information.txt
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.md
使用add命令将修改后的README.md文件加入缓存后,确认其状态
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: Information.txt
modified: README.md
再次修改README.md文件后确认其状态
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: Information.txt
modified: README.md
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.md
可以发现README.md出现了两次。这是如若使用commit命令提交文件,只会提交Changed to be committed版本的文件。也就是说,第二次修改README.md文件的内容不会被提交。
查看状态的命令可以加上-s参数,使输出简化:
$ git status -s
A Information.txt
MM README.md
忽略文件
在工程根目录可以创建.gitignore文件,在其中列出不被git管理的文件。
$ cat .gitignore
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
可以看到,列出了很多类型的文件。这些文件都将被git忽略。可以使用正则表达式,更多的示例可以参照
https://github.com/github/gitignore
查看已暂存和未暂存的修改
diff命令是查看最后一次add命令添加到缓存的文件,和最后一次add命令之后修改的文件。
如下,README.md文件被修改了两次, 第一次修改通过add命令缓存了,第二次修改没有执行add命令。所以diff命令的结果输出这两个版本的不同。
$ git diff
diff --git a/README.md b/README.md
index 31fb62b..76de482 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,2 @@
# Kutilion
-My one repository1
+My one repository12
diff --staged命令是查看已经被commit的文件和被缓存的文件。
如下,对于README.md文件来说,示例比较的是第一次修改后的README.md文件和刚刚克隆下来时的README.md文件
$ git diff --staged
diff --git a/Information.txt b/Information.txt
new file mode 100644
index 0000000..74e5e69
--- /dev/null
+++ b/Information.txt
@@ -0,0 +1 @@
+My project
diff --git a/README.md b/README.md
index 6ed3783..31fb62b 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,2 @@
# Kutilion
-My one repository
+My one repository1
提交更新
commit命令可以把缓存后的文件提交。
下面先通过add将README.md第二次修改的内容也缓存,然后用commit提交。注意使用-m命令可以避免进入编辑器模式,用于添加提交注释。笔者对这个编辑器很不感冒。
$ git add README.md
$ git status -s
A Information.txt
M README.md
$ git commit -m "First commit"
[master c4bea31] First commit
2 files changed, 2 insertions(+), 1 deletion(-)
create mode 100644 Information.txt
可以看到提交的分支是master,SHA-1校验码是c4bea31。以及一些文件信息。
跳过使用暂存区域
每次都使用add来把更改的文件加入缓存是很浪费时间的,所以可以使用commit -a命令来跳过缓存步骤。直接把更改后的文件提交。
删除文件
直接使用rm命令删除文件。本地文件夹中的文件也被删除了。
$ git rm Information.txt
rm 'Information.txt'
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: Information.txt
第三次修改README.md文件,然后执行rm命令
$ git rm README.md
error: the following file has local modifications:
README.md
(use --cached to keep the file, or -f to force removal)
出现错误,原因是第三次修改没有通过add来添加到缓存。
使用-f可以无视缓存,强制删除
$ git rm -f README.md
rm 'README.md'
rm --cached 命令可以将缓存的文件版本删除, 而保留本地版本。有些情况会用到。
使用移动文件命令来更改文件名字
使用mv命令可以实现移动文件,也可以实现文件重命名。重命名看起来是一条命令,其实它隐含了三条命令
$ git mv ChangeName NameChanged
隐含的命令。当然隐含的命令不是我们需要关心的。
$ mv ChangeName NameChanged
$ git rm ChangeName
$ git add NameChanged
查看提交历史
log命令查看提交历史。-p参数显示提交内容,-2参数显示最近两次提交,--stat参数显示简略报告。
$ git log
commit c4bea312bd0676dcdf00a40bb38fb2930bc82afe (HEAD -> master)
Author: kutilion <kutilion@gmail.com>
Date: Sun Mar 31 20:47:35 2019 +0800
First commit
commit 6e2fa0997c2ce0bd01c289000c70795d232676ca (origin/master, origin/HEAD)
Author: Kutilion <48250368+Kutilion@users.noreply.github.com>
Date: Thu Mar 14 15:54:53 2019 +0800
Initial commit
--pretty参数可以选择报告的风格比如--pretty=oneline
$ git log --pretty=oneline
c4bea312bd0676dcdf00a40bb38fb2930bc82afe (HEAD -> master) First commit
6e2fa0997c2ce0bd01c289000c70795d232676ca (origin/master, origin/HEAD) Initial commit
关于log命令的参数可以参照原文。