Re: What's The Right Way to Do This?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jon Forrest <nobozo@xxxxxxxxx> 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.
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]