修改git文件

修改本地库的文件,体验git版本与版本穿梭

初始化git目录 git init

1.1、创建一个文件,并提交到本地本地库

go 复制代码
echo "123" >  hello.txt		# 创建文件
git add hello.txt		# 加入到暂存区
git commit -m "first commit" hello.txt		# 提交到本地库
git relog		# 查看分支
复制代码
$ echo "123" > hello.txt
Administrator@ecs-2145 MINGW64 /c/git-demo (master)
$ git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
Administrator@ecs-2145 MINGW64 /c/git-demo (master)
$ git log
fatal: your current branch 'master' does not have any commits yet
Administrator@ecs-2145 MINGW64 /c/git-demo (master)
$ git commit -m "first commit" hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
[master (root-commit) 416f86e] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 hello.txt
Administrator@ecs-2145 MINGW64 /c/git-demo (master)
$ git log
commit 416f86eb3f1837f5ba07710ecb371a879e52c881 (HEAD -> master)		# 当前版本
Author: zhangsan <zhangsan@qq.com>	# 提交人
Date:   Fri Sep 1 10:52:37 2023 +0800		# 提交时间
    first commit			#  提交描述

1.2、修改工作区的文件并将文件提交到本地库

powershell 复制代码
echo "456" >> hello.txt
git add hello.txt	
git commit -m "second commit" hello.txt	
git relog		
复制代码
$ echo "456" >> hello.txt
Administrator@ecs-2145 MINGW64 /c/git-demo (master)
$ git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
Administrator@ecs-2145 MINGW64 /c/git-demo (master)
$ git commit -m "second commit" hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
[master cfcaa42] second commit
 1 file changed, 1 insertion(+)
Administrator@ecs-2145 MINGW64 /c/git-demo (master)
$ git log
commit cfcaa42d561d3375299763306c56bbd6614e0332 (HEAD -> master)		# 当前版本
Author: zhangsan <zhangsan@qq.com>
Date:   Fri Sep 1 11:05:10 2023 +0800
    second commit		
commit 416f86eb3f1837f5ba07710ecb371a879e52c881
Author: zhangsan <zhangsan@qq.com>
Date:   Fri Sep 1 10:52:37 2023 +0800
    first commit

1.3、版本穿梭,将版本回退至指定版本

git log #查看版本详细
git reflog #查看版本简略

powershell 复制代码
git log
复制代码
$ git  log
commit cfcaa42d561d3375299763306c56bbd6614e0332 (HEAD -> master)	# HEAD 指向即为当前版本
Author: zhangsan <zhangsan@qq.com>
Date:   Fri Sep 1 11:05:10 2023 +0800

    second commit

commit 416f86eb3f1837f5ba07710ecb371a879e52c881
Author: zhangsan <zhangsan@qq.com>
Date:   Fri Sep 1 10:52:37 2023 +0800

    first commit

1.3.1、版本切换至 416f86eb3f1837f5ba07710ecb371a879e52c881 这个版本

也可以使用版本前7位

复制代码
语法:git reset --hard 版本号

也可以使用 git checkout 版本号 来切换版本或者分支

powershell 复制代码
git reset --hard 416f86eb3f1837f5ba07710ecb371a879e52c881 
git log
复制代码
$ git reset --hard 416f86eb3f1837f5ba07710ecb371a879e52c881
HEAD is now at 416f86e first commit

$ git log
commit 416f86eb3f1837f5ba07710ecb371a879e52c881 (HEAD -> master)		# 当前所处版本
Author: zhangsan <zhangsan@qq.com>
Date:   Fri Sep 1 10:52:37 2023 +0800

    first commit

1.3.2、查看文件变化

powershell 复制代码
cat hello.txt
复制代码
$ cat hello.txt
123

可以发现之间写入的456已经没有了,回到了最初的时候

补充:

复制代码
可以打开.git\refs\heads\master查看版本号
相关推荐
二二孚日30 分钟前
自用华为ICT云赛道Big Data第四章知识点-Flink流批一体分布式实时处理引擎
大数据·华为
WebGirl32 分钟前
代码Revert后再次Merge会丢失的问题
git
xufwind1 小时前
spark standlone 集群离线安装
大数据·分布式·spark
AI数据皮皮侠2 小时前
中国区域10m空间分辨率楼高数据集(全国/分省/分市/免费数据)
大数据·人工智能·机器学习·分类·业界资讯
DeepSeek大模型官方教程3 小时前
NLP之文本纠错开源大模型:兼看语音大模型总结
大数据·人工智能·ai·自然语言处理·大模型·产品经理·大模型学习
大数据CLUB4 小时前
基于spark的奥运会奖牌变化数据分析
大数据·hadoop·数据分析·spark
Edingbrugh.南空5 小时前
Hadoop高可用集群搭建
大数据·hadoop·分布式
智慧化智能化数字化方案5 小时前
69页全面预算管理体系的框架与落地【附全文阅读】
大数据·人工智能·全面预算管理·智慧财务·智慧预算
小皮侠5 小时前
nginx的使用
java·运维·服务器·前端·git·nginx·github
武子康5 小时前
大数据-33 HBase 整体架构 HMaster HRegion
大数据·后端·hbase