Search code examples
gitgithubatlassian-sourcetree

Problem trying to reset remote branch with git


I have the master branch that I want to use behind origin/master. How can I delete the latest commit on remote? I cannot do a push because the tip of my current branch is behind its remote counterpart but I don't want to merge both.

enter image description here


Solution

  • How can I delete the latest commit on remote?

    Technically speaking, you can't—but you can make the other Git have its master point to the commit before the last one. That will make the other Git "forget" the latest commit. That commit will eventually get removed, whenever the other Git gets around to running a maintenance command. That's good enough, as long as no one else re-introduces that commit. (Anyone else could have picked up the commit from that server in the meantime.)

    I cannot do a push because the tip of my current branch is behind its remote counterpart ...

    You can, and usually must, do a push. What you need to tell your Git is to send its final request in the form of a command, rather than a polite request.

    A git push works by:

    1. having your Git call up some other Git;
    2. having your Git give that Git new commits if and as needed;
    3. ending the session with your Git asking that other Git, politely: Please, if it's OK, set your branch _____ to commit _____ (fill in the blanks with branch names and hash IDs).

    Your Git will send their Git no commits (because you have nothing new to them), then ask them to set their master to point to commit Rediseño del header y del overlay ... (whatever its actual hash ID is, as found in your name master).

    They will say no, and the reason they say no—which they also pass along—is: If I do that, I'll lose access to the commit whose subject is "Cabiados componentes y header" (whatever its hash ID actually is). This refusal takes the form of rejected (non-fast-forward), which means I could lose some commits.

    But that's precisely what you want their Git to do: to "lose" that commit, so that it is "forgotten" and will eventually be removed for real.

    What you need to do, then, is stop asking politely. Send them instead a forceful command: Set your master! They can still refuse, but if you have the permissions that allow this kind of forceful push, they will obey, and lose the commit.

    There are two forms of this forceful command:

    • One—the less-careful one—is what you get if you just run git push --force origin master. This just sends the command: Set your master to _____! (filling in the blank by finding the right hash ID in your own Git repository).

    • The more-careful way is git push --force-with-lease. This one sends the command, but in the form: I think your master is _______. If so, set it to _______! If not, tell me I was wrong about what I thought it was. Your Git fills in the first blank with what you have in your origin/master: the hash ID your Git remembers from the last conversation your Git had with their Git. Your Git fills in the second hash ID in the usual way.

      If no one else has added any new commits to the other Git repository, your origin/master will still match their master. If the --force-with-lease fails, this means someone else probably made use of that commit you're trying to discard; you should git fetch origin to pick up the new commits, and have an actual human-to-human conversation with the other person, to figure out what to do about that.

    If you know no one else is using that other Git repository, or at least that branch in that Git repository, the git push --force master suffices. If you're not sure, it's best to confer with the other users first, even if you're going to use the --force-with-lease option for safety.