修改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查看版本号
相关推荐
天硕国产存储技术站32 分钟前
DualPLP 双重掉电保护赋能 天硕工业级SSD筑牢关键领域安全存储方案
大数据·人工智能·安全·固态硬盘
雷文成.思泉软件40 分钟前
以ERP为核心、企微为门户,实现一体化集成
大数据·低代码·创业创新
weixin_377634841 小时前
【Git使用】PyCharm中的Git使用
ide·git·pycharm
东哥说-MES|从入门到精通2 小时前
数字化部分内容 | 十四五年规划和2035年远景目标纲要(新华社正式版)
大数据·人工智能·数字化转型·mes·数字化工厂·2035·十四五规划
爱吃泡芙的小白白3 小时前
vscode、anaconda、git、python配置安装(自用)
ide·git·vscode·python·anaconda·学习记录
南飞测绘视界3 小时前
上市公司绿色专利申请、授权数据(1999-2024年)
大数据·专利·上市公司
一个天蝎座 白勺 程序猿4 小时前
KingbaseES在政务领域的应用实践——武汉人社大数据平台“数字化服务新模式”
大数据·数据库·政务·kingbasees·金仓数据库
pale_moonlight4 小时前
十、 Scala 应用实践 (上)
大数据·开发语言·scala
第二只羽毛5 小时前
遵守robots协议的友好爬虫
大数据·爬虫·python·算法·网络爬虫
Elastic 中国社区官方博客5 小时前
使用 A2A 协议和 MCP 在 Elasticsearch 中创建一个 LLM agent 新闻室:第二部分
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索