Home > Enterprise >  Is it safe to push amend on private ranch
Is it safe to push amend on private ranch

Time:11-03

Say I am the sole developer working on a branch. I push and immediately see the wrong message which I want to fix with amend. I know when others are working it can easily do a lot of trouble.

But if I am 100% sure no one touched the commit and hasn't even fetched the changes?

CodePudding user response:

You raise a very reasonable question. We are often told not to change the history of a branch we've already pushed to the remote. But that's overly simplistic. As you rightly infer, the question is whether the branch is truly shared remotely, not merely whether it has been pushed.

In recent years, Git has provided a way to help thread this little needle. Do your amend locally. To push, you will have to push with force. Well, when you do, use --force-with-lease. This will prevent you from pushing if someone else has actually done anything to change this branch. As the docs say:

If the remote ref still points at the commit you specified, you can be sure that no other people did anything to the ref. It is like taking a "lease" on the ref without explicitly locking it, and the remote ref is updated only if the "lease" is still valid.

This doesn't ensure that no one else has fetched the branch (they probably have, since every fetch fetches everything) or that no one else has made a local copy of the branch. But it does ensure that you are the first person to do anything new with the branch, so what you are doing is relatively safe. Combined with common sense, it's the way to go. A typical scenario is that you pushed the branch but did not make a PR out of it; you were just pushing as backup. In that case, this is totally safe.

One final word of advice: there is no substitute for communication. You are probably using Slack or similar with the rest of the team; if you pushed as a PR and submitted the PR for review, simply tell your team members what you are doing, so that the branch doesn't unexpectedly change out from under anybody.

  • Related