@@ -288,6 +288,10 @@ and the history looks now like this::
288288If it went wrong, recovery is again possible as explained :ref: `above
289289<recovering-from-mess-up>`.
290290
291+ If you have not yet pushed this branch to github, you can carry on as normal,
292+ however if you *have * already pushed this commit see :ref: `force-push ` for how
293+ to replace your already published commits with the new ones.
294+
291295
292296.. _rebase-on-main :
293297
@@ -359,3 +363,55 @@ some related help on merging in the git user manual - see `resolving a merge`_.
359363
360364.. _git rebase : https://git-scm.com/docs/git-rebase
361365.. _resolving a merge : https://schacon.github.io/git/user-manual.html#resolving-a-merge
366+
367+
368+ If you have not yet pushed this branch to github, you can carry on as normal,
369+ however if you *have * already pushed this commit see :ref: `force-push ` for how
370+ to replace your already published commits with the new ones.
371+
372+
373+ .. _force-push :
374+
375+
376+ Pushing, with force
377+ -------------------
378+
379+ If you have in some way re-written already pushed history (e.g. via
380+ :ref: `rewriting-commit-history ` or :ref: `rebase-on-main `), then when you try to
381+ push the new commits to GitHub it will fail with an error that looks like ::
382+
383+ $ git push
384+ Pushing to github.com:YOURFORK/matplotlib.git
385+ To github.com:YOURFORK/matplotlib.git
386+ ! [rejected] cool_feature -> cool_feature (non-fast-forward)
387+ error: failed to push some refs to 'github.com:tacaswell/matplotlib.git'
388+ hint: Updates were rejected because the tip of your current branch is behind
389+ hint: its remote counterpart. Integrate the remote changes (e.g.
390+ hint: 'git pull ...') before pushing again.
391+ hint: See the 'Note about fast-forwards' in 'git push --help' for details.
392+
393+ What is going on is that by default ``git push `` tries to protect you from
394+ accidentally make commits inaccessible by leaving no reference to them so if a
395+ push *would * effectively discard commits on the remote, ``git `` rejects the
396+ push. When this happens GitHub adds the helpful suggestion to pull the remote
397+ changes and then try pushing again. In some cases, such as if you and a
398+ colleague are both committing and pushing to the same branch, this is the
399+ correct course of action (or rebase your local branch but it is a matter of
400+ taste). Although both of things are good ideas, taken together they are not
401+ what we want to do.
402+
403+ In the case of having intentionally re-written history we *want * to make the
404+ commits on GitHub inaccessible and replace them with the new-and-improved versions
405+ on our local machines. In these cases what we want to do is ::
406+
407+ $ git push --force-with-lease
408+
409+ which tells git you are aware of the risks and want to do the push anyway. We
410+ recommend using ``--force-with-lease `` over the ``--force `` flag. The
411+ ``--force `` will do the push no matter what, whereas ``--force-with-lease ``
412+ will only do the push of the remote branch is where the local ``git `` client
413+ thought it was.
414+
415+ Be judicious with force-pushing. It is effectively re-writing published
416+ history and if anyone has fetched the old commits will have a different view
417+ of history which can cause confusion.
0 commit comments