添加文件到暂存区
git add file
git add 可以多次调用实现多个文件添加到版本库。
anyang@anyang:~/anyang/learngit$ touch README.md
anyang@anyang:~/anyang/learngit$ ls
README.md
anyang@anyang:~/anyang/learngit$ **git add README.md **
把暂存区的内容提交到本地仓库
git commit -m “提交文件时的说明信息”
在创建 Git 仓库时,Git 自动创建了一个 master 分支,默认情况下git commit
就是往 master 分支上提交更改的,git commit
一次可以提交多个文件更改到当前分支。
anyang@anyang:~/anyang/learngit$ git commit -m "Add README.md"
[master (root-commit) 72f8209] Add README.md
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README.md
查看版本库状态
git status
若文件尚未存入暂存区,则效果如下:
anyang@anyang:~/anyang/learngit$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
nothing added to commit but untracked files present (use "git add" to track)
若文件已经存入暂存区,但尚未提交到本地仓库,则效果如下:
anyang@anyang:~/anyang/learngit$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.md
若所有文件修改已经提交到本地仓库,则效果如下:
anyang@anyang:~/anyang/learngit$ git status
On branch master
nothing to commit, working tree clean
查看文件修改内容
git diff file
当我们在README.md文件中加入Hello, world!
后,输入以上命令效果如下:
anyang@anyang:~/anyang/learngit$ vim README.md
anyang@anyang:~/anyang/learngit$ git diff README.md
diff --git a/README.md b/README.md
index e69de29..af5626b 100644
--- a/README.md
+++ b/README.md
@@ -0,0 +1 @@
+Hello, world!
显示从最近到最远的提交日志
该命令通常用于查看需要回退到哪个版本时使用。
git log
如果觉得输出信息太多,不方便查看,可以加上--pretty=oneline
参数,效果图如下所示:
anyang@anyang:~/anyang/learngit$ git log
commit 513dec66f4313d10909e8ba0336939138957da27
Author: anyang xautanyang@163.com
Date: Fri Dec 2 20:12:52 2016 +0800
Add Hello, world!
commit 72f8209cd1f9bfa3cc279eee5492336b97217178
Author: anyang xautanyang@163.com
Date: Fri Dec 2 19:58:34 2016 +0800
Add README.md
anyang@anyang:~/anyang/learngit$ git log --pretty=oneline
513dec66f4313d10909e8ba0336939138957da27 Add Hello, world!
72f8209cd1f9bfa3cc279eee5492336b97217178 Add README.md
版本回退
Git 中HEAD
表示当前版本,HEAD^
表示上一版本,HEAD^^
表示上上版本,对于多版本回退,可以使用HEAD~n
,例如:HEAD~50
表示回退50个版本。
git reset --hard HEAD^ 或 git reset --hard commit_id
假设我们在 README.md 文件中错误的加入了一行文字 "Wrong code" ,并提交到了版本库,则可以通过 git reset --hard HEAD^
回退到版本库上一状态,效果图如下:
anyang@anyang:~/anyang/learngit$ vim README.md
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
Wrong code
anyang@anyang:~/anyang/learngit$ git add README.md
anyang@anyang:~/anyang/learngit$ git commit -m "Add Wrong code"
[master 89fdd19] Add Wrong code
1 file changed, 1 insertion(+)
anyang@anyang:~/anyang/learngit$ git reset --hard HEAD^
HEAD is now at 513dec6 Add Hello, world!
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
查询历史命令
该命令通常用于查看需要回到未来的哪个版本时使用。
git reflog
效果图如下所示:
anyang@anyang:~/anyang/learngit$ git reflog
513dec6 HEAD@{0}: reset: moving to HEAD^
89fdd19 HEAD@{1}: commit: Add Wrong code
513dec6 HEAD@{2}: commit: Add Hello, world!
72f8209 HEAD@{3}: commit (initial): Add README.md
撤销更改
git checkout -- file
把文件在工作区的修改全部撤销,让文件回到最近一次 git commit 或 git add 时的状态,其分别为:
- 若文件自修改后还没有被放到暂存区,撤销修改就回到和版本库一模一样的状态, 效果图如下所示:
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
anyang@anyang:~/anyang/learngit$ vim README.md
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
haha
anyang@anyang:~/anyang/learngit$ git checkout -- README.md
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
- 若文件已经添加到暂存区,并进行了修改,撤销修改就会回到添加到暂存区后的状态,效果图如下所示:
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
anyang@anyang:~/anyang/learngit$ vim README.md
anyang@anyang:~/anyang/learngit$ git add README.md
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
heihei
anyang@anyang:~/anyang/learngit$ vim README.md
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
heihei
hehe
anyang@anyang:~/anyang/learngit$ git checkout -- README.md
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
heihei
把暂存区的修改回退到工作区,即撤销 git add 命令
git reset HEAD file
效果图如下所示:
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
anyang@anyang:~/anyang/learngit$ vim README.md
anyang@anyang:~/anyang/learngit$ git add README.md
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
haha
anyang@anyang:~/anyang/learngit$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: README.md
anyang@anyang:~/anyang/learngit$ git reset HEAD README.md
Unstaged changes after reset:
M README.md
anyang@anyang:~/anyang/learngit$ 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.md
no changes added to commit (use "git add" and/or "git commit -a")
从版本库中删除文件
git rm file
git commit -m "remove file"
效果图如下所示:
anyang@anyang:~/anyang/learngit$ ls
README.md
anyang@anyang:~/anyang/learngit$ touch file.txt
anyang@anyang:~/anyang/learngit$ ls
file.txt README.md
anyang@anyang:~/anyang/learngit$ git add file.txt
anyang@anyang:~/anyang/learngit$ git commit -m "Add file.txt"
[master 13f3101] Add file.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file.txt
anyang@anyang:~/anyang/learngit$ rm file.txt
anyang@anyang:~/anyang/learngit$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: file.txt
no changes added to commit (use "git add" and/or "git commit -a")
anyang@anyang:~/anyang/learngit$ **git rm file.txt **
rm 'file.txt'
anyang@anyang:~/anyang/learngit$ git commit -m "remove file.txt"
[master 2d88197] remove file.txt
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 file.txt
anyang@anyang:~/anyang/learngit$ git status
On branch master
nothing to commit, working tree clean
相关资料:
- Git 官网
- Git 官方文档
- [廖雪峰的 Git 教程](http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8
067c8c017b000) - Git 常用命令查询文档
- Git 在线学习网址