美文网首页
Git笔记:在过去做Git提交

Git笔记:在过去做Git提交

作者: _百草_ | 来源:发表于2025-06-16 16:49 被阅读0次

回溯到过去版本,在那个时间点创建一个新的提交


1、查看提交历史,获取commit-hash

29447@GW64 /d/myProject (main)
$ git log --oneline -10 --graph
* 64eb279 (HEAD -> main, origin/main, origin/HEAD) main:delete other files 2025/6/17 16:04
*   756290e Merge branch 'main' of github.com:bai-cao/python_clone
|\
| *   3acb00d Merge branch 'main' of github.com:bai-cao/python_clone   # 选中此次提交
| |\
| | * 162e2f6 (test_branch) delete
| | * c4c4c5b test delete branch
| | * 62e1b59 add dev.txt
| * | 666e916 8b58b4c add txt
* | | 45eaab4 delete other files
* | | 271de83 delete
* | | 3ee915f test delete branch

2、针对该提交创建一个新分支

29447@GW64 /d/myProject (main)
$ git branch 3acb00d 3acb00d

3、切换到新分支,并在指定的过去时间点进行提交。

29447@GW64 /d/myProject (main)
$ git checkout 3acb00d
warning: refname '3acb00d' is ambiguous.
Switched to branch '3acb00d'

29447@GW64 /d/myProject (3acb00d)
$ git log --oneline -10 --graph>3ac_202506171610.txt

29447@GW64 /d/myProject (3acb00d)
$ git add 3ac_202506171610.txt
warning: in the working copy of '3ac_202506171610.txt', LF will be replaced by CRLF the next time Git touches it

29447@GW64 /d/myProject (3acb00d)
$ git commit -m "3acb00d branch:添加txt"
[3acb00d 811fa59] 3acb00d branch:添加txt
 1 file changed, 14 insertions(+)
 create mode 100644 3ac_202506171610.txt

29447@GW64 /d/myProject (3acb00d)
$ git log --oneline -10 --graph
* 811fa59 (HEAD -> 3acb00d) 3acb00d branch:添加txt
*   3acb00d Merge branch 'main' of github.com:bai-cao/python_clone
|\
| * 162e2f6 (test_branch) delete
| * c4c4c5b test delete branch
| * 62e1b59 add dev.txt
| *   c4c9b7c fix conflicts in main.txt
| |\
| | * 9449a11 main:add main.txt
| * | 47a9da0 test_branch:add main.txt
* | | 666e916 8b58b4c add txt
| |/
|/|
* | 8b58b4c main:delete lowcase 16:18

4、重置分支

29447@GW64 /d/myProject (3acb00d)
$ git checkout main
Switched to branch 'main'

29447@GW64 /d/myProject (main)
$ git rebase 3acb00d
warning: refname '3acb00d' is ambiguous.
warning: refname '3acb00d' is ambiguous.
dropping 67ed040143b99770e5219a1be98abd19e7ee3cbc 8b58b4c add txt -- patch contents already upstream
dropping 54bcb70021a9906cda6bfff32ee133472347bfa5 add dev.txt -- patch contents already upstream
Successfully rebased and updated refs/heads/main.

29447@GW64 /d/myProject (main)
$ git log --oneline -10 --graph
* 373f4da (HEAD -> main) main:delete other files 2025/6/17 16:04
* 9a1f996 delete other files
* 100c686 delete
* 2993034 test delete branch
* 5290109 c4c9b7c上添加新提交
* 811fa59 (3acb00d) 3acb00d branch:添加txt
*   3acb00d Merge branch 'main' of github.com:bai-cao/python_clone
|\
| * 162e2f6 (test_branch) delete
| * c4c4c5b test delete branch
| * 62e1b59 add dev.txt

29447@GW64 /d/myProject (main)
$

git rebase -i <commit-hash> # 修改过去提交。执行命令后,Git打开一个编辑器,显示我们要修改的提交历史。可以选则

变基操作(git rebase -i):

对提交历史进行重组和修改;可能导致提交的哈希值发生改变
每次对代码的修改提交都会生成一条提交历史。每个提交都包含一个唯一的哈希值,该哈希值是根据提交内容计算出来的。Git使用SHA-1算法生成40个字符长度的哈希值,这个哈希值作为提交的唯一标识,确保每个提交都具有唯一性和完整性

git rebase -i 修改提交历史时,Git会生成新的提交,原有的提交被覆盖或丢弃。因为Git将对提交历史的修改视为一系列的变更操作,顾变基操作同时,会生成新的提交,这就导致了提交哈希的改变(提交内容改变,根据提交内容生成的哈希随之改变)


5、推送远程

git push origin <new_branch>

若修改了过去的提交,则需要强制推送
git push --force origin <new_branch>


6、参考

1、如何在过去做Git提交
2、Git 变基(git rebase -i)为什么会改变提交哈希

相关文章

  • 2019-07-12

    Git使用笔记 0、先将工作分支的内容提交 git add . git commit -m "说明文字" 1、切换...

  • **git** 使用笔记

    git 使用笔记 git原理: 文件(blob)对象,树(tree)对象,提交(commit)对象 tree对象 ...

  • 2019-10-25

    git 使用笔记 git原理: 文件(blob)对象,树(tree)对象,提交(commit)对象 tree对象 ...

  • GIT命令

    新建路径: git clone 提交修改: git add . 本地提交: git comm...

  • 关于Git

    首次提交5条 git init .......git 重复提交3条 git add . git commit -m...

  • git常用命令

    git add . git commit -m "message"提交,其中message是提交的信息。 git ...

  • git 常用的命令

    git status 检测状态 git add . 提交的代码显示 git commit -m "备注" 提交...

  • 05_git版本回退和版本比较

    git log git的历史提交记录 commit 后面的hash值是 git提交id git log --pre...

  • Git学习笔记

    Git学习笔记 常用命令 查看提交历史 git log 一个常用选项是 -p,用来显示每次提交的内容差异。 -n显...

  • git撤销与回滚

    git撤销已经push的提交 git log 查看提交记录 git reset --soft commit_id ...

网友评论

      本文标题:Git笔记:在过去做Git提交

      本文链接:https://www.haomeiwen.com/subject/abhfgjtx.html