What and where does one potentially lose stuff when git says “forced update”?

前端 未结 3 1889
广开言路
广开言路 2021-01-01 09:47

I recently received a \"forced update\" warning from git on a repository which only I commit to. I haven\'t done any re-basing so I don\'t know why this happened. What I w

3条回答
  •  离开以前
    2021-01-01 10:27

    What I want to know is, where should I look to find changes that have potentially been lost?

    Kevin is right when writing that no local history will be lost. Still, some remote history may be lost, though it will be lost on purpose.

    Example

    For example:

    • D commits and pushes master to S.
    • L fetches.
    • D changes mind (amends a commit, or changes history of master in whatever way).
    • D pushes the modified (non-fast-forward) master to S.

    On fetch L will warn "forced update" because the previous branch tip is no longer reachable through the branch reference master. But that's what D wanted anyway.

    What to look for

    From Kevin's answer:

     + 7193788...a978889 master     -> origin/master  (forced update)
    

    That line will appear only once. The reference to the possibly lost commit is 7193788.

    How to explore it

    If L wants to keep a reference to what might be lost, L might in above example issue: git branch whateverbranchname 7193788. This may be done whatever the current state of the local checkout.

    Or just git checkout 7193788 to explore it in detached head, then e.g. git checkout master to get back to master. This may require to first commit any local change.

    Good practice

    Notice that it's considered bad practice to push a changed history on a shared repository without proper cooperation with other users (because it causes extra work for people who have changes not yet shared).

    In other words, no one should be surprised to see a "forced update" when doing a fetch on a shared repository. If someone pushed a bad commit, they should consider just pushing a corrected commit on top, not change existing history. Alternatively, they should get into agreement with others before pushing a changed history. That latter option is not possible on a public repository.

提交回复
热议问题