回溯到过去版本,在那个时间点创建一个新的提交
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>








网友评论