39 | Prohibit pushing -f to integration branches#
-f / --force: force update
Scenario: When there is no fast-forward relationship between local and remote, pushing to remote is generally not allowed. However, if you use the git push -f
command, you can force update the local to the remote.
Demonstration: Forcefully erase the remote commit.
First, let's take a look at the current remote commit history:
And the local commit history:
At this point, we will reset the local version to a previous commit, such as f21c98a, by executing git reset --hard f21c98a
, and then check the local commit history again:
We find that the subsequent commit records have disappeared.
Now, let's push again:
Directly executing git push
will definitely not work, but after adding -f
, it was successfully pushed. Let's take another look at the remote commit history:
No! If you are another developer on the remote, how would you feel seeing this?
So, what mechanisms are in place to prevent such operations? You can search for it yourself; both Github and Gitlab provide corresponding protection mechanisms.
📢 Additionally, you can recover the recently lost branch using git reflog
, as shown in the red box below:
Then use git reset --hard HEAD@{n}
to restore it~
40 | Prohibit operations that change history on integration branches#
Rebase operations are strictly prohibited on public branches in the team.
For example:
Colleague A changes the commit message through rebase and pushes it to the remote:
He added the [feature] prefix to the messages of two commits.
Meanwhile, another colleague B had already created the test_rebase_inter branch based on the origin/temp branch and made 2 new commits before his changes:
At this point, colleague B wants to push his updates and will definitely encounter issues.
Moreover, every colleague who has already been developing on the test_rebase_inter branch will face this problem, and they will also need to use rebase to handle it, which brings a lot of unnecessary trouble.
Alternatively, find colleague A and ask him to restore the original state; the specific method can refer to the git reflog
method mentioned in the previous section.
One suitable scenario for rebase: when you have not yet pushed to the remote, use rebase to combine multiple local commits into a single commit before pushing.
In integrated development, remember two points: do not push -f; do not change remote history.