threads / discuss / 28466

What's The Right Way to Do This?

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

## tl;dr

6 messages between Sep 23, 2011 and Sep 23, 2011.

replies: 5people: 5as markdown or json

Jon Forrest· Sep 23, 2011, 04:48 UTC · lore

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.
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, the files I had modified *and* the files that apparently were
modified by other people in #3 above were reverted. This wasn't what
I wanted. I only wanted to revert the changes I had made.

With the help of someone more experienced than me we were able to get things back to normal but this experience left me wondering what I should have done in the first place. There's a chance I'm going to have to go through all this again as I try to fix the problem with my changes.

Any suggestions would be appreciated.

Thanks, Jon Forrest

Michael Witten· Sep 23, 2011, 05:08 UTC · re: Jon Forrest · lore

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

On Fri, Sep 23, 2011 at 04:48, Jon Forrest <nobozo@gmail.com> wrote:
Show 5 quoted lines
> ... So, I tried to revert using various methods I found by
> googling "git revert"...
> ...
> ...this experience left me wondering what I should have
> done in the first place.

What you should have done in the first place is RTFM; while not fantastic, there is a decent amount of official git documentation.

Show 5 quoted lines
> What happened was that when I tried to revert back to the
> commit before the one I made, the files I had modified
> *and* the files that apparently were modified by other
> people in #3 above were reverted. This wasn't what
> I wanted. I only wanted to revert the changes I had made.

It would be a good idea to reproduce the unexpected behavior you are experiencing by means of a minimal set of commands that you can post here for others to try.

> With the help of someone more experienced than me we were
> able to get things back to normal... There's a chance I'm
> going to have to go through all this again as I try to fix
> the problem with my changes.
Why don't you ask that person about it?
Luke Diamand· Sep 23, 2011, 06:30 UTC · re: Jon Forrest · lore

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

On 23/09/11 05:48, Jon Forrest wrote:
Show 11 quoted lines
> 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.

Someone will be along soon to correct me, but I think you should have rebased against the master branch rather than merged. Something like:

% git rebase origin

That way your changes would remain appended to the tip of the master branch. That will in general make your life much easier for all sorts of reasons.

Show 14 quoted lines
>
> 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.
>
> 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, the files I had modified *and* the files that apparently were
> modified by other people in #3 above were reverted. This wasn't what
> I wanted. I only wanted to revert the changes I had made.

At a guess, git revert created a commit that undid your _merge_, rather than your commit. The merge included all those other changes....

It's a good idea to take a look at what a commit does before pushing it 
- "git show HEAD" is your friend.
Show 6 quoted lines
>
> With the help of someone more experienced than me we were able to get
> things back to normal but this experience left me wondering what I
> should have done in the first place. There's a chance I'm going to
> have to go through all this again as I try to fix the problem with
> my changes.

Use git rebase (*) to keep your changes nicely arranged at the top of the main branch.

Use git show to check the sanity of what you're about to push upstream.

I think you could award yourself a nice cup of tea after an experience like that though, having been on both sides of it, I can imagine you need it :-P

Regards! Luke

(*) But note that rebasing can cause problems for people who are downstream of _you_. See the git-rebase(1) man page.

Jon Forrest· Sep 23, 2011, 13:45 UTC · re: Luke Diamand · lore

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

On 9/22/2011 11:30 PM, Luke Diamand wrote:
Show 8 quoted lines
> Someone will be along soon to correct me, but I think you should have
> rebased against the master branch rather than merged. Something like:
>
> % git rebase origin
>
> That way your changes would remain appended to the tip of the master
> branch. That will in general make your life much easier for all sorts of
> reasons.

In considering this, I'm having trouble understanding when to do a merge as opposed to a rebase. Let's say that other people weren't putting changed back in the remote master. So, my changes would always be at the tip. Is that when I should merge? If so, then this suggest that knowing what to do depends on knowing what other people are doing. But, doesn't this go against the philosophy of a distributed SCM?

Show 5 quoted lines
> At a guess, git revert created a commit that undid your _merge_, rather
> than your commit. The merge included all those other changes....
>
> It's a good idea to take a look at what a commit does before pushing it
> - "git show HEAD" is your friend.

IIRC, I did this and I saw the collection of the changes I had made plus what other people had made. I didn't know how to revert only my changes.

> Use git rebase (*) to keep your changes nicely arranged at the top of
> the main branch.
Thanks. I'll study up on this.
> I think you could award yourself a nice cup of tea after an experience
> like that though, having been on both sides of it, I can imagine you
> need it :-P
It certainly wasn't my finest hour!
Jon
Johannes Sixt· Sep 23, 2011, 06:39 UTC · re: Jon Forrest · lore

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

Am 9/23/2011 6:48, schrieb Jon Forrest:
Show 9 quoted lines
> 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.
OK, so you are basically using a central repo.
Show 8 quoted lines
> 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.

I assume you mention this because with this step you detected that your changes were crap.

Show 6 quoted lines
> 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, the files I had modified *and* the files that apparently were
> modified by other people in #3 above were reverted. This wasn't what
> I wanted. I only wanted to revert the changes I had made.
It looks like you had reset the history, not just reverted your changes.
Show 5 quoted lines
> With the help of someone more experienced than me we were able to get
> things back to normal but this experience left me wondering what I
> should have done in the first place. There's a chance I'm going to
> have to go through all this again as I try to fix the problem with
> my changes.

With a central repository, any push into it marks a "done deal". No matter how wrong and bogus your changes are, as soon as you push them into the central repository, they are cast in stone.

At this point, the best you can do is to revert you changes, and by this I mean that you create and push out another commit on top that backs out the changes that you made.

If this is not what you (or your team) wants, than the problem is not with suboptimal use of the git toolset, but with the push policy within you team. A good policy should forbid that untested stuff can be pushed into the central repository.

My suggestion is that you find a way to move your changes to the test site (the "another server" you mention in step 6) without going through the central repository. For example, if you have ssh access between your development machine and the test site, you can push or pull your changes between them directly.

-- Hannes
Junio C Hamano· Sep 23, 2011, 18:13 UTC · re: Jon Forrest · lore

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

Jon Forrest <nobozo@gmail.com> writes:
Show 18 quoted lines
> 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.

← back to recent threads