git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Jon Forrest <nobozo@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: What's The Right Way to Do This?
Date: Fri, 23 Sep 2011 11:13:06 -0700	[thread overview]
Message-ID: <7vr537unyl.fsf@alter.siamese.dyndns.org> (raw)
In-Reply-To: <loom.20110923T064720-366@post.gmane.org> (Jon Forrest's message of "Fri, 23 Sep 2011 04:48:36 +0000 (UTC)")

Jon Forrest <nobozo@gmail.com> writes:

> I'm just now starting to use git for more than trivial things.
> Today I got myself in trouble. Here's what happened:
>
> 1) I pulled the master branch from the IT repository from our
> main git server.
>
> 2) I created a branch from this called "J" and started making changes.
>
> 3) Other people pulled master from IT and then pushed changes back.
>
> 4) I merged J with my master branch.
>
> 5) I tried pushing my master back to origin but this failed with
> the usual message saying I first needed to pull from origin.
> So, I pulled and then pushed. This worked.
>
> 6) On another server where I was going to use my changes I pulled
> master from IT.

Up to this point you are doing perfectly fine as far as I can tell.

> 6) It turned out that my changes were incorrect. So, I tried to revert
> using various methods I found by googling "git revert". What happened
> was that when I tried to revert back to the commit before the one I
> made,...

Now, in git you do not "revert back TO the commit".  You could reset back
to the commit but that is to discard everything that is done since that
commit.

Imagine this history (time flows from left to right).

At point 4, 'master' in your working repository is at M, and you haven't
seen others' work 'o' yet:

    ---M---o---o---o (others' work)
        \
         x---y---z (your work on J branch)

You merge your work your master and then your 'master' now points at 'z'.

If you push and replace 'master' at the central repository with your
'master', others' work 'o' will be lost. So you pulled in point 5 to make
the history look like this:

    ---M---o---o---o (others' work)
        \           \
         x---y---z---X

You pulled to grab these 'o' commits, and made a merge with your master
(now at J) to create 'X', and then pushed it as 'master' at the central
repository.

Time passes and there may be more changes on top of 'X' now and the
current tip of the shared 'master' may be 'A'.

    ---M---o---o---o (others' work)
        \           \
         x---y---z---X---o---o---A

Even if the changes 'x', 'y', and 'z' later turns out to be unwanted, you
do not want discard others' work 'o'. Rewinding the history to make the
'master' point at 'M' is not what you want.

Instead, you would make commits that revert the effect of them on top of
the current history. That is what "git revert" does.


    ---M---o---o---o (others' work)
        \           \
         x---y---z---X---o---o---A-(z)-(y)-(x)

That is, on top of 'A', you would run "git revert z" to create an "anti-z"
commit "(z)" to cancel the effect of 'z', and do the same for y and x, to
eradicate them.

Another possibility is to revert the merge 'X', but I'll leave it as an
advanced exercise to the readers.

      parent reply	other threads:[~2011-09-23 18:13 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-23  4:48 What's The Right Way to Do This? Jon Forrest
2011-09-23  5:08 ` Michael Witten
2011-09-23  6:30 ` Luke Diamand
2011-09-23 13:45   ` Jon Forrest
2011-09-23  6:39 ` Johannes Sixt
2011-09-23 18:13 ` Junio C Hamano [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7vr537unyl.fsf@alter.siamese.dyndns.org \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=nobozo@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).