Git 删除提交\commit的log记录、修改历史提交消息、删除历史提交、修改所有提交的邮箱地址、从所有提交中删除一个文件

您所在的位置:网站首页 所有的邮箱 Git 删除提交\commit的log记录、修改历史提交消息、删除历史提交、修改所有提交的邮箱地址、从所有提交中删除一个文件

Git 删除提交\commit的log记录、修改历史提交消息、删除历史提交、修改所有提交的邮箱地址、从所有提交中删除一个文件

2023-08-13 00:42| 来源: 网络整理| 查看: 265

Git使用小结 git教程推荐删除历史提交log记录(合并提交)情景如下操作步骤 修改历史提交消息删除历史提交修改所有提交的邮箱地址从所有提交中删除一个文件

git教程推荐

折腾git,被git折腾,还是保持够用就好的原则?

https://backlog.com/git-tutorial/cn/stepup/stepup7_1.html

https://git-scm.com/book/zh/v2

http://gitbook.liuhui998.com/index.html

以下情景都属于改写提交

基本都是基于git rebase命令

删除历史提交log记录(合并提交) 情景如下

在这里插入图片描述

操作步骤

回到第一次提交commit 1请使用git rebase -i --root master

1 . git rebase -i到commit 2的上一次提交commit 1

git rebase -i 5a25878 # 执行完毕会弹出编辑器

2 . 修改commit 3 pick命令为squash, 和commit2 合并(squash命令: use commit, but meld into previous commit/使用提交,但合并进先前的提交)

# 编辑前内容 pick 8a98f84 commit 2, add 2.txt pick 5d2b9ba commit 3, add 3.txt pick 0768163 commit 4, add 4.txt pick 22e45ec commit 5, add 5.txt # Rebase 5a25878..22e45ec onto 5a25878 (4 command(s)) # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # d, drop = remove commit # # 编辑后内容 pick 8a98f84 commit 2, add 2.txt # 修改了改行 squash 5d2b9ba commit 3, add 3.txt pick 0768163 commit 4, add 4.txt pick 22e45ec commit 5, add 5.txt

3 . 保存上一步文本内容, 会弹出编辑器提示修改两次合并后的消息内容, 删除 commit 2的消息, commit 3不变, 然后保存

在这里插入图片描述

效果如下,仅删除了提交记录log,文件内容保持不变

在这里插入图片描述

修改历史提交消息

修改最近一次提交信息

git commit --amend # 修改保存即可

在这里插入图片描述

修改以前的提交信息

回到第一次提交commit 1请使用git rebase -i --root master

# git rebase -i 要修改的commit的上一级commit id # 使用reword命令 # 22e45ec commit 5, add 5.txt # 0768163 commit 4, add 4.txt # 5d2b9ba commit 3, add 3.txt # 8a98f84 commit 2, add 2.txt # 5a25878 commit 1, add 1.txt # 以修改commit 2为例, 先rebase到commit 2的上一级commit 1 git rebase -i 5a25878 # 修改commit 2的rebase命令pick为reword # 保存,弹出编辑器提示修改提交信息,修改为要更正的信息保存即可

在这里插入图片描述

删除历史提交

回到第一次提交commit 1请使用git rebase -i --root master

无冲突

git rebase -i 要修改的commit的上一级commit id使用drop命令应用到commit 2

在这里插入图片描述

有冲突

情景

需要删除commit 2提交,但commit 6依赖2.txt, 而 commit 2创建的2.txt

在这里插入图片描述

git rebase -i 要修改的commit的上一级commit id使用drop命令应用到commit 2 leo@demo:~/tutorial$ git rebase -i 5a25878 error: could not apply cb75f2e... commit 6, 修改了2.txt When you have resolved this problem, run "git rebase --continue". If you prefer to skip this patch, run "git rebase --skip" instead. To check out the original branch and stop rebasing, run "git rebase --abort". Could not apply cb75f2ecf6ab2d837a61bd95531b306ed22258d8... commit 6, 修改了2.txt # git rebase 执行" pick cb75f2e commit 6, 修改了2.txt "检测到与"删除commit 2"有冲突,需要手动解决冲突后才能继续进行 修改完冲突后git add 冲突的文件/ git add . 然后git rebase --continue提交。(本例中冲突处理是, 保留2.txt和commit 6修改的内容,然后提交)

在这里插入图片描述

修改所有提交的邮箱地址 git filter-branch -f --commit-filter ' if [ "$GIT_AUTHOR_EMAIL" = "[email protected]" ]; then GIT_AUTHOR_NAME="leo2"; GIT_AUTHOR_EMAIL="[email protected]"; git commit-tree "$@"; else git commit-tree "$@"; fi' HEAD

在这里插入图片描述

# 可以限定范围commit xx到当前head执行替换操作 git filter-branch -f --commit-filter '....command' 5a258787..HEAD # 但是如何限定范围commit xx 到中间的某个commit xx而不是HEAD如何实现不清楚 从所有提交中删除一个文件 git filter-branch --tree-filter 'rm -f passwords.txt' HEAD


【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3