目录
禁止使用git push -f
当我们在当前分支向远程分支push的时候,如果不是fast-forward,Git是禁止我们向远程分支push的
这是Git的一种保护机制
bogon:gitLearning LC$ git status
On branch feature/add_git_commonds
Your branch is behind 'github/feature/add_git_commonds' by 26 commits, and can be fast-forwarded.
(use "git pull" to update your local branch)
nothing to commit, working tree clean
bogon:gitLearning LC$ git push
To github.com:JiangHuHiKe/gitLearning.git
! [rejected] feature/add_git_commonds -> feature/add_git_commonds (non-fast-forward)
error: failed to push some refs to 'git@github.com:JiangHuHiKe/gitLearning.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
bogon:gitLearning LC$
但有一个选项,可以让我们强制将当前分支的内容提交到远程分支
bogon:gitLearning LC$ git push -h
-f, --force force updates
一、git push -f 的危害
1、场景
我们通过reset将HEAD指针重置到当前分支较早的一次commit
(注意与分离头指针的区别,HEAD还是在当前分支上)
使用git push -f 强制提交到远程分支,此时我们中间所做的一些工作就被丢弃了
2、命令演示
#1、
#显示我们所有的提交记录
bogon:gitLearning LC$ git log --oneline
343891a (HEAD -> feature/add_git_commonds, github/main, github/feature/add_git_commonds) resolve conflicts
a7fb5d4 mv index.html index2.html
81e913d mv index.html index1.html
4e55db5 Merge branch 'feature/add_git_commonds' of github.com:JiangHuHiKe/gitLearning into feature/add_git_commonds
5986acd mv index.htm index.html
977a571 update index.htm again
b1be5f3 update index.htm
4c632b4 Merge branch 'feature/add_git_commonds' of github.com:JiangHuHiKe/gitLearning into feature/add_git_commonds
69a34ab modify index.html
e2ddad7 mv index.html to index.htm
82f1d8e Resolved conflict by hand with 4 git commands
dd9d201 Add mv and rm
aceffed Add stash and log
2aaaca0 Merge remote-tracking branch 'origin/feature/add_git_commonds' into feature/add_git_commonds
cf5e4bf add commit
5269dc9 non fast-forward
16a59ac Merge remote-tracking branch 'origin/feature/add_git_commonds' into feature/add_git_commonds
fa370ba update raedme file
9b88a9f Add command
48deee6 Add git commands description in readme
e4cd59c (zhineng/main, main) add .gitignore file
73eb75f Merge remote-tracking branch 'github/main' into main
73a1073 Initial commit
2e7d469 (zhineng/temp, github/temp, temp) Readd readme file
aa4e385 rm readme file
fe9b9f9 Add index.html
8119c2d Add readme
f7eed8c Initial
bogon:gitLearning LC$
#2、
#git reset --hard 重置到某次提交记录
#我们发现之前好多提交已经丢失
bogon:gitLearning LC$ git reset --hard 8119c2d
HEAD is now at 8119c2d Add readme
bogon:gitLearning LC$ git log --oneline
8119c2d (HEAD -> feature/add_git_commonds) Add readme
f7eed8c Initial
bogon:gitLearning LC$
#3、
#如果此时我们使用git push -f 那么远程分支也会丢失
bogon:gitLearning LC$ git push -f
Total 0 (delta 0), reused 0 (delta 0)
To github.com:JiangHuHiKe/gitLearning.git
+ 343891a...8119c2d feature/add_git_commonds -> feature/add_git_commonds (forced update)
#4、
#查看下分支情况,
#如果此时他人也拉取代码,他人本地仓库也会丢失
bogon:gitLearning LC$ git branch -av
* feature/add_git_commonds 8119c2d Add readme
remotes/github/feature/add_git_commonds 8119c2d Add readme
remotes/github/main 343891a resolve conflicts
bogon:gitLearning LC$
3、分析
在团队合作的时候,我们可以禁止某些团队成员使用git push -f ,来提高代码的安全性。
目前GitHub和Gitlab都是具有该功能的
禁止向集成分支执行变更历史的操作
在前边文章中我们说过,通过git rebase -i 交互变基的方式可以做到:
1、修改之前提交的message
2、合并连续commit
3、合并非连续commit
但有一个大前提一定要记住:在自己独自使用的分支上操作
在协作开发过程中,公共分支拉取到本地后,是严禁对历史执行变基操作的
公共的分支是大家一点一点积累起来的,它的历史不允许发生变更,只能往前走,不能后退更改
场景描述
有一个temp分支,A同学和B同学基于这个分支协作开发
A同学:有一天发现提交记录不完美,想整理历史提交message,对本地temp分支的历史message进行了rebase,
从下图发现此时已经破坏了fast-forward
正常push已经会被拒绝,所以使用git push -f 到了远端
B同学:在temp分支上继续开发,执行git fetch指令
此时已经出现问题,没办法fast-forward了
然后执行git pull指令
Merge branch 'temp' of github.com:JiangHuHiKe/gitLearning into temp
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
从下图可以看出提交记录已经错乱了,这还只是B同学,如果还有C同学,D同学可想而知
行者常至,为者常成!