Bo2SS

Bo2SS

5 Git Integration Taboos

39 | Prohibit performing push -f operation on integration branches#

-f / --force: force update

Scenario: When the local and remote branches are not in a fast-forward relationship, it is not allowed to push to the remote branch. However, if the git push -f command is used, the local branch can be forcefully updated to the remote branch.

Demonstration: Forcefully erase the remote commits.

First, let's take a look at the current remote commit history:

image

And the local commit history:

image

At this point, let's reset the local version to a previous commit, such as f21c98a, by executing git reset --hard f21c98a, and then take a look at the local commit history again:

image

It can be seen that the commit history after it has disappeared.

Now, let's try to push again:

image

Directly git push won't work, but after adding -f, it was successfully pushed. Let's take a look at the remote commit history again:

image

No! If you are another developer on the remote, what would you feel when you see this?

So, what mechanisms are there to prevent this operation? You can search for it yourself, both Github and Gitlab provide corresponding protection mechanisms.

📢 In addition, you can also use git reflog to retrieve the lost branch, as shown in the red box in the following figure:

image

Then use git reset --hard HEAD@{n} to restore it~

40 | Prohibit modifying the history of integration branches#

Rebasing on public branches in the team is strictly prohibited.

For example:

Colleague A modifies the commit message through rebase and pushes it to the remote:

image

He added the [feature] prefix to the messages of the two commits.

Meanwhile, another colleague B has already created a test_rebase_inter branch based on the origin/temp branch and submitted 2 new commits before his modification:

image

At this point, colleague B will encounter problems when trying to push his updates.

And every colleague who has already been working on the test_rebase_inter branch will encounter this problem, and they will 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 of the use cases for rebase: Before pushing to the remote, merge multiple local commits into one commit using rebase, and then push.

For integration development, remember two points: don't push -f; don't modify remote history.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.