1、合并两次提交
1)git rebase -i HEAD~2
- 如果报这个错误 cannot rebase: You have unstaged changes.
- 你本地有未提交(commit)的更改,可以使用git stash 存起来,或者把这些再提交一次
2)随后进入类似vim打开的一个文件,将pick修改成fixup(保留应用,丢弃commit描述)或者squash(保留应用,合并commit描述)等关键字。修改后wq保存
不能将最近一次commit修改为fixup或squash,否则会有问题,
- 问题1、git branch结果
(no branch, rebasing feature_optimize_monitor) - 问题2、git log发现两次commit都不见了
回滚此次git rebase 操作即可: git rebase --abort
3)修改commit message,然后wq保存即可
此处只是自己学习记录一下,原作者 https://blog.youkuaiyun.com/qq_36544876/article/details/88624105
2、修改commit message提交
1)git commit --amend
然后修改提交信息,wq即可
此处只是自己学习记录一下,原作者
https://blog.youkuaiyun.com/Muscleape/article/details/105637401
git 常用操作
git操作 | 作用 | 常用示例 | 撤销 |
---|---|---|---|
git add file1 file2 | 将file1 file2添加到暂存区 | git add file | git reset HEAD file1 file2 |
git commit -m “注释” | 将暂存区的文件提交,可添加本次提交的注释 | git commit-m “添加配置” | git reset --hard commitId(代码回滚到此次commitId,不保留改动) git reset --soft 撤销此次commit,保留本地改动 |
git push | 将本地的commit推送到远程仓库(非必要不加-f参数) | git push | 配合git reset --hard ,然后git push -f |
git checkout xxx | 切换到xxx分支 | ||
git checkout file1 file2 | 将本地修改的文件的改动撤销 | ||
git log | 查看该分支commit历史 | ||
git reflog | 查看本地操作(我理解是commit维度) |
git reset 参数
–mixed:暂存区会更新到指定的commit。但是工作目录不受影响。默认参数
–soft:不会改变暂存区或工作目录。(即原本的更新内容还在工作目录和暂存区,但是 commit id 回到之前的。可以重新 commit)
–hard:暂存区和工作目录都会更新到指定的 commit。(即指定commit之后的更新会全部消失)
撤销 git reset --hard xxx
git reflog. //会记录你的reset操作
可以找到reset操作前一次的commitid,然后git reset --hard commitid,即可撤销
git revert -n commitid //撤销此次commitid,然后 git commit -m “撤销某次commit”
这文章讲的不错 https://blog.youkuaiyun.com/LIQIANGEASTSUN/article/details/124632656