当远程仓库拒绝强制推送时,Git 回滚和合并代码操作教程

149 阅读5分钟

Git 回滚和合并代码操作教程

目录

  1. Git 回滚操作
  2. 合并代码操作
  3. 常见问题和解决方案
  4. 实际案例分析

Git 回滚操作

1. 回滚方法对比

方法影响范围是否保留历史使用场景风险等级
git reset --soft本地,不影响工作区不保留修改最近的提交
git reset --mixed本地,重置暂存区不保留取消暂存,保留工作区修改
git reset --hard本地,完全重置不保留完全回滚到指定提交
git revert创建新提交保留撤销某些提交但保留历史

2. 具体操作步骤

2.1 查看提交历史
# 查看提交历史
git log --oneline -10

# 查看图形化历史
git log --graph --oneline --all
2.2 使用 git reset 回滚(本地操作)
# 软重置:只移动HEAD指针,保留暂存区和工作区
git reset --soft <commit-hash>

# 混合重置:移动HEAD,重置暂存区,保留工作区
git reset --mixed <commit-hash>
# 或者简写为
git reset <commit-hash>

# 硬重置:完全回滚到指定提交,丢弃所有后续修改
git reset --hard <commit-hash>
2.3 使用 git revert 回滚(推荐用于远程分支)
# 撤销单个提交
git revert <commit-hash>

# 撤销多个提交(按相反顺序)
git revert <commit-hash-1> <commit-hash-2>

# 撤销一个范围的提交
git revert <start-commit>..<end-commit>

# 不自动提交,手动编辑
git revert --no-commit <commit-hash>

3. 远程分支回滚

3.1 强制推送(需要权限)
# 回滚本地分支
git reset --hard <target-commit>

# 强制推送到远程
git push origin <branch-name> --force

# 更安全的强制推送
git push origin <branch-name> --force-with-lease
3.2 使用 revert(推荐)
# 使用 revert 撤销提交
git revert <commit-hash>

# 推送到远程
git push origin <branch-name>

合并代码操作

1. 分支合并策略

1.1 Fast-forward 合并
# 切换到目标分支
git checkout main

# 合并功能分支
git merge feature-branch
1.2 三方合并
# 禁用 fast-forward
git merge --no-ff feature-branch
1.3 压缩合并
# 将多个提交压缩成一个
git merge --squash feature-branch
git commit -m "合并功能分支的所有修改"

2. 解决合并冲突

2.1 查看冲突状态
# 查看冲突文件
git status

# 查看冲突详情
git diff
2.2 解决冲突
# 手动编辑冲突文件,然后添加到暂存区
git add <conflicted-file>

# 继续合并
git commit

# 或者中止合并
git merge --abort

3. 变基操作(Rebase)

3.1 交互式变基
# 变基最近3个提交
git rebase -i HEAD~3

# 变基到指定提交
git rebase -i <commit-hash>
3.2 分支变基
# 将当前分支变基到主分支
git rebase main

# 解决冲突后继续
git rebase --continue

# 中止变基
git rebase --abort

常见问题和解决方案

1. 远程仓库拒绝强制推送

问题: remote: error: denying non-fast-forward

解决方案:

# 方案1:使用 revert 代替 reset
git revert <commit-hash>
git push origin <branch-name>

# 方案2:联系管理员临时关闭分支保护
# 方案3:创建新分支进行操作
git checkout -b hotfix/<branch-name>
git reset --hard <target-commit>
git push origin hotfix/<branch-name>

2. 误删提交的恢复

问题: 使用 git reset --hard 后想要恢复

解决方案:

# 查看操作历史
git reflog

# 恢复到指定状态
git reset --hard <reflog-hash>

3. 合并后发现错误

问题: 合并完成后发现有问题

解决方案:

# 查看合并提交
git log --merges -1

# 撤销合并
git revert -m 1 <merge-commit-hash>

实际案例分析

案例1:回滚到指定提交(我们的实际操作)

场景: 需要将 develop 分支回滚到"【修改】注释中保留本地测试方法"这个提交

步骤:

  1. 查看提交历史
git log --oneline -20
  1. 尝试本地回滚
git reset --hard 3dc4dc1
  1. 尝试强制推送(失败)
git push origin develop --force
# 结果:remote rejected
  1. 采用 revert 方案
# 重置到远程最新状态
git reset --hard origin/develop

# 按相反顺序 revert 不需要的提交
git revert a30a77a --no-edit
git revert c13f444 --no-edit

# 推送到远程
git push origin develop

结果: 成功回滚,保留完整历史记录

案例2:功能分支合并

场景: 将功能分支合并到主分支

步骤:

# 1. 切换到主分支并更新
git checkout main
git pull origin main

# 2. 合并功能分支
git merge --no-ff feature/new-function

# 3. 解决冲突(如有)
# 编辑冲突文件
git add .
git commit

# 4. 推送到远程
git push origin main

# 5. 删除功能分支
git branch -d feature/new-function
git push origin --delete feature/new-function

最佳实践建议

1. 操作前的准备

  • 始终先备份重要分支
  • 确认当前工作区是干净的
  • 与团队成员沟通重要操作

2. 分支保护

  • 对主要分支设置保护规则
  • 要求代码审查后才能合并
  • 禁止直接推送到主分支

3. 提交规范

  • 使用清晰的提交信息
  • 遵循约定的提交格式
  • 避免过大的提交

4. 紧急情况处理

# 创建紧急修复分支
git checkout -b hotfix/urgent-fix

# 进行修复
# ...

# 合并到主分支
git checkout main
git merge --no-ff hotfix/urgent-fix

# 同时合并到开发分支
git checkout develop
git merge --no-ff hotfix/urgent-fix

常用 Git 命令速查

# 查看状态
git status
git log --oneline
git reflog

# 分支操作
git branch
git checkout <branch>
git checkout -b <new-branch>

# 暂存操作
git stash
git stash pop
git stash list

# 远程操作
git remote -v
git fetch
git pull
git push

# 撤销操作
git checkout -- <file>      # 撤销工作区修改
git reset HEAD <file>        # 撤销暂存区修改
git reset --hard HEAD       # 撤销所有本地修改

注意: 在执行任何可能影响代码历史的操作前,请务必备份重要数据并与团队成员充分沟通。