修改本地库的文件,体验git版本与版本穿梭
初始化git目录
git init
1.1、创建一个文件,并提交到本地本地库
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、修改工作区的文件并将文件提交到本地库
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
#查看版本简略
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 版本号 来切换版本或者分支
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、查看文件变化
cat hello.txt
$ cat hello.txt
123
可以发现之间写入的456已经没有了,回到了最初的时候文章来源:https://www.toymoban.com/news/detail-696403.html
补充:文章来源地址https://www.toymoban.com/news/detail-696403.html
可以打开.git\refs\heads\master查看版本号
到了这里,关于修改git文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!