本系列为个人学习Git参照廖雪峰老师的笔记
- 本文内容:Git本地版本控制管理
参考笔记:
https://www.liaoxuefeng.com/wiki/896043488029600
文章目录
1 Git版本控制
1-2 查看修改内容
// 查看当前仓库状态
git status
Practice
➜ learngit git:(master) vim teacher_ma.txt
➜ learngit git:(master) ✗ cat teacher_ma.txt
Teacher Ma eats three wolves
Teacher Ma eats shi tou ren
Teacher Ma is back to the home !!!
➜ learngit git:(master) ✗ git status
位于分支 master
尚未暂存以备提交的变更:
(使用 "git add <文件>..." 更新要提交的内容)
(使用 "git checkout -- <文件>..." 丢弃工作区的改动)
修改: teacher_ma.txt
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
// git diff 是工作区(work dict)和暂存区(stage)的比较
// git diff –cached #是暂存区(stage)和版本库的比较
git diff readme.txt
Practice
➜ learngit git:(master) ✗ git diff
diff --git a/teacher_ma.txt b/teacher_ma.txt
index 19058a2..e05a404 100644
--- a/teacher_ma.txt
+++ b/teacher_ma.txt
@@ -1,3 +1,5 @@
Teacher Ma eats three wolves
Teacher Ma eats shi tou ren
+
+Teacher Ma is back to the home !!!
(END)
Practice
1.比较修改之间的差异
git diff 不加参数即默认比较工作区与暂存区
git diff --cached [<path>...]比较暂存区与最新本地版本库(本地库中最近一次commit的内容)
git diff HEAD [<path>...]比较工作区与最新本地版本库。如果HEAD指向的是master分支,那么HEAD还可以换成master
git diff commit-id [<path>...]比较工作区与指定commit-id的差异
git diff --cached [<commit-id>] [<path>...]比较暂存区与指定commit-id的差异
git diff [<commit-id>] [<commit-id>]比较两个commit-id之间的差异
git add 之后
➜ learngit git:(master) ✗ git add teacher_ma.txt
➜ learngit git:(master) ✗ git status
位于分支 master
要提交的变更:
(使用 "git reset HEAD <文件>..." 以取消暂存)
修改: teacher_ma.txt
➜ learngit git:(master) ✗
git commit 之后
➜ learngit git:(master) ✗ git commit -m "teacher ma is back to home"
[master ce1ee22] teacher ma is back to home
1 file changed, 2 insertions(+)
➜ learngit git:(master) git status
位于分支 master
无文件要提交,干净的工作区
➜ learngit git:(master)
1-3 版本回退
Practice
添加一次修改经历
➜ learngit git:(master) vim teacher_ma.txt
➜ learngit git:(master) ✗ cat teacher_ma.txt
Teacher Ma eats three wolves
Teacher Ma eats shi tou ren
Teacher Ma is back to the home !!!
Teacher Ma gank middle road @@@
➜ learngit git:(master) ✗ git add teacher_ma.txt
➜ learngit git:(master) ✗ git commit -m "gank middle road"
[master 2871442] gank middle road
1 file changed, 2 insertions(+)
git log
– 查看发布历史
git log
git log --pretty=oneline
– 查看命令历史
git reflog
Practice
➜ learngit git:(master) git log
commit 2871442553b0302cfa8d79fc3f39ad9fa8eb70c8 (HEAD -> master)
Author: Ruiskey <18130319287@163.com>
Date: Thu Jul 8 16:48:26 2021 +0800
gank middle road
commit ce1ee223e581dd2bea64162fd498caca41f7a2e9
Author: Ruiskey <18130319287@163.com>
Date: Thu Jul 8 16:44:23 2021 +0800
teacher ma is back to home
commit f2a1a21e1497b588fbccfbcc7bd70642c787fa4a
Author: Ruiskey <18130319287@163.com>
Date: Thu Jul 8 16:29:14 2021 +0800
teacher Ma's lesson
~
~
~
~
~
~
(END)
git log --pretty=oneline
➜ learngit git:(master) git log --pretty=oneline
2871442553b0302cfa8d79fc3f39ad9fa8eb70c8 (HEAD -> master) gank middle road
ce1ee223e581dd2bea64162fd498caca41f7a2e9 teacher ma is back to home
f2a1a21e1497b588fbccfbcc7bd70642c787fa4a teacher Ma's lesson
(END)
回退版本
方式1:
回退上一个版本
git reset --hard HEAD^
回退上上个版本
git reset --hard HEAD^^
方式2:
回退对应的版本号所指向的版本
git reset --hard ce1ee
命令历史
➜ learngit git:(master) git reflog
ce1ee22 (HEAD -> master) HEAD@{0}: reset: moving to ce1ee
2871442 HEAD@{1}: commit: gank middle road
ce1ee22 (HEAD -> master) HEAD@{2}: commit: teacher ma is back to home
f2a1a21 HEAD@{3}: commit (initial): teacher Ma's lesson
(END)
1-4 工作区和暂存区
Practice
➜ learngit git:(master) touch laoye_lu.txt
➜ learngit git:(master) ✗ ls
laoye_lu.txt teacher_ma.txt
➜ learngit git:(master) ✗ git status
位于分支 master
未跟踪的文件:
(使用 "git add <文件>..." 以包含要提交的内容)
laoye_lu.txt
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
➜ learngit git:(master) ✗
初始状态

git add之后
➜ learngit git:(master) ✗ git add laoye_lu.txt
➜ learngit git:(master) ✗ git status
位于分支 master
要提交的变更:
(使用 "git reset HEAD <文件>..." 以取消暂存)
新文件: laoye_lu.txt

git commit之后
➜ learngit git:(master) ✗ git commit -m "create new file laoye_lu.txt"
[master 14bf57d] create new file laoye_lu.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 laoye_lu.txt
➜ learngit git:(master) git status
位于分支 master
无文件要提交,干净的工作区

1-5 管理修改
如果修改没有添加(add)至暂存区,那么也不会被提交(commit)
Practice
➜ learngit git:(master) ✗ cat teacher_ma.txt
Teacher Ma eats three wolves
Teacher Ma eats shi tou ren
Teacher Ma is back to the home !!!
Teacher Ma gank middle road @@@
Teacher Ma is unstoppable %%
➜ learngit git:(master) ✗ git add teacher_ma.txt
➜ learngit git:(master) ✗ git status
位于分支 master
要提交的变更:
(使用 "git restore --staged <文件>..." 以取消暂存)
修改: teacher_ma.txt
继续修改
➜ learngit git:(master) ✗ vim teacher_ma.txt
➜ learngit git:(master) ✗ cat teacher_ma.txt
Teacher Ma eats three wolves
Teacher Ma eats shi tou ren
Teacher Ma is back to the home !!!
Teacher Ma gank middle road @@@
Teacher Ma is unstoppable %%
shutdown $$$$
➜ learngit git:(master) ✗ git commit -m "teacher ma is unstoppable"
[master 176fa53] teacher ma is unstoppable
1 file changed, 2 insertions(+)
➜ learngit git:(master) ✗ git status
位于分支 master
尚未暂存以备提交的变更:
(使用 "git add <文件>..." 更新要提交的内容)
(使用 "git restore <文件>..." 丢弃工作区的改动)
修改: teacher_ma.txt
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
第一次修改 -> git add -> 第二次修改 -> git commit
1-6 撤销修改
撤销工作区的修改
git checkout – laoye_lu.txt
Practice
➜ learngit git:(master) vim laoye_lu.txt
➜ learngit git:(master) ✗ cat laoye_lu.txt
laoye lu is disapper ...
laoye lu is so cai !
➜ learngit git:(master) ✗ git checkout -- laoye_lu.txt
➜ learngit git:(master) cat laoye_lu.txt
➜ learngit git:(master)
命令git checkout -- laoye_lu.txt意思就是,把laoye_lu.txt文件在工作区的修改全部撤销,这里有两种情况:
一种是laoye_lu.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
一种是laoye_lu.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。
总之,就是让这个文件回到最近一次git commit或git add时的状态。
撤销暂存区的修改
如果不幸将文件已经提交至暂存区,可以使用 git reset
撤销git add的操作
- git reset HEAD laoye_lu.txt ## 回退暂存区
- git reset --hard HEAD^ ## 回退版本
Practice
➜ learngit git:(master) vim laoye_lu.txt
➜ learngit git:(master) ✗ cat laoye_lu.txt
laoye lu is disapper ...
laoye lu is so cai !
➜ learngit git:(master) ✗ git add laoye_lu.txt
➜ learngit git:(master) ✗ git reset HEAD laoye_lu.txt
重置后取消暂存的变更:
M laoye_lu.txt
➜ learngit git:(master) ✗ cat laoye_lu.txt
laoye lu is disapper ...
laoye lu is so cai !
➜ learngit git:(master) ✗ git status
位于分支 master
尚未暂存以备提交的变更:
(使用 "git add <文件>..." 更新要提交的内容)
(使用 "git restore <文件>..." 丢弃工作区的改动)
修改: laoye_lu.txt
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
1-7 删除文件
删除
// 添加文件
git add test.txt
// 删除文件
git rm test.txt
// 提交文件
git commit -m “remove test.txt”
Practice
➜ learngit git:(master) ✗ touch test.txt
➜ learngit git:(master) ✗ git add test.txt
➜ learngit git:(master) ✗ git commit -m "create new file test.txt"
[master 0028773] create new file test.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt
➜ learngit git:(master) rm test.txt
➜ learngit git:(master) ✗ git add test.txt
➜ learngit git:(master) ✗ git status
位于分支 master
要提交的变更:
(使用 "git restore --staged <文件>..." 以取消暂存)
删除: test.txt
➜ learngit git:(master) ✗ git commit -m "remove test.txt"
[master 333b891] remove test.txt
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 test.txt
➜ learngit git:(master) git status
位于分支 master
无文件要提交,干净的工作区
还原
错删之后还原
git checkout – test.txt
git checkout其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”。
但是,只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容。
本文详细介绍了Git的版本控制管理,包括查看修改内容、版本回退、工作区与暂存区的区别、管理修改和撤销修改等操作。通过实践案例展示了如何使用gitadd、gitcommit、gitdiff、gitlog和gitreset等命令进行版本控制。此外,还讲解了如何删除和还原文件,确保对Git的全面掌握。
517

被折叠的 条评论
为什么被折叠?



