1.首先查看有多少提交记录(依次是最新的到最旧的),命令:git log
2.然后输入命令:git rebase -i HEAD~4 注:4 表示要合并的条数,根据自行需要输入
执行出错解决方案,无错忽略
fatal: Needed a single revision invalid upstream –i
解决方法:git rebase -i --root
3.正确执行第二部会出现一个编辑窗口(windows为vim,linux默认为nano(可以更改下默认为vim),响应保存编辑操作自行百度)
注:(最下面的一条是最近提交的记录,与git log命令记录查看相反)
pick 5e187c7dbe8 add center style indent
pick 6d577eb3440 add center style
pick f9b9508a3ab add center style
pick 111ab9cc261 update templates
因此合并正确的操作
pick 5e187c7dbe8 add center style indent # 最旧
squash 6d577eb3440 add center style
squash f9b9508a3ab add center style
squash 111ab9cc261 update templates # 最近的提交记录
可简写为: s f9b9508a3ab add center style
改动完成后执行保存并退出的操作
4.若出现冲突,解决方案如下,若无冲突可忽略
根据提示查看代码,将冲突部分进行删除
然后输入这两条命令
git add .
git rebase --continue
5.冲突解决,以及没有冲突都会进入到这个编辑界面(windows为vim,linux默认为nano,响应保存编辑操作自行百度)
图片参考如下,操作完成请保存退出
# This is a combination of 4 commits.
# The first commit’s message is:
'okmei66666' # 根据需要输入描述
# The 2nd commit’s message is:
add center style # 删除
# The 3rd commit’s message is:
add center style # 删除
# The 4th commit’s message is:
update templates # 删除
# Please enter the commit message for your changes. Lines starting
# with ‘#’ will be ignored, and an empty message aborts the commit.
6.将代码推送到远程服务器
git push -f
或者 git push -f origin 分支名
若命令依旧无法执行
但输入git log 命令的确只剩下一个记录了
C:\Users\Administrator\Desktop\abc\rebase_tadoy>git log
commit 669115a09b4b1ee338bcc1cb6fb3de9e055e14ad (HEAD -> 2019/03/15/origin)
Author: <E5><97><AF><E5><93><BC> <1477861642@qq.com>
Date: Fri Mar 15 20:31:15 2019 +0800
'okmei66666'
就从当前分支新切换一个出去,在推到远程也是一样的,那条还有许多记录的分支可删除 git push origin --delete 远程分支名
git checkout -b 2019/03/06/origin
推送到服务器仓库,看得到只有一个提交记录
git push origin 2019/03/06/origin
具体参考如下
# 查看分支
C:\Users\Administrator\Desktop\abc\rebase_tadoy>git branch
* 2019/03/15/origin
master
# 分支状态
C:\Users\Administrator\Desktop\abc\rebase_tadoy>git status
On branch 2019/03/15/origin
nothing to commit, working tree clean
# 从当前分支切换一个分支出去,当前分支为2019/03/15/origin
C:\Users\Administrator\Desktop\abc\rebase_tadoy>git checkout -b 2019/03/06/origin
Switched to a new branch '2019/03/06/origin'
# 分支建立完毕
C:\Users\Administrator\Desktop\abc\rebase_tadoy>git branch
* 2019/03/06/origin
2019/03/15/origin
master
# 分支记录查看
C:\Users\Administrator\Desktop\abc\rebase_tadoy>git log
commit 669115a09b4b1ee338bcc1cb6fb3de9e055e14ad (HEAD -> 2019/03/06/origin, origin/2019/03/06/origin, 2019/03/15/origin)
Author: <E5><97><AF><E5><93><BC> <1477861642@qq.com>
Date: Fri Mar 15 20:31:15 2019 +0800
'okmei66666'
C:\Users\Administrator\Desktop\abc\rebase_tadoy>git push origin 2019/03/06/origin
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 2.31 KiB | 1.15 MiB/s, done.
Total 5 (delta 0), reused 0 (delta 0)
remote: Powered By Gitee.com
To https://gitee.com/asdf0/rebase_tadoy.git
* [new branch] 2019/03/06/origin -> 2019/03/06/origin
图文参考链接:https://blog.youkuaiyun.com/yangcs2009/article/details/47166361
git合并远程仓库提交记录操作
最新推荐文章于 2025-06-18 15:57:59 发布