* Switching branches without committing changes
@ 2008-03-21 3:27 Joe Fiorini
2008-03-21 3:52 ` Jeff King
2008-03-21 4:06 ` Shawn O. Pearce
0 siblings, 2 replies; 10+ messages in thread
From: Joe Fiorini @ 2008-03-21 3:27 UTC (permalink / raw)
To: git
He all,
I'm still a newbie to Git (and this list), so if I don't provide
enough details please let me know what you need and I will provide :).
I'm trying to switch branches without committing my changes. Is this
possible? For example, I'm working on a site, I'm testing the
implementation of a new technology (branch B), I'm not quite done
there (or I forget to commit everything) and I want to implement
something else new. I create a new branch off of B, called B.1, and
then make some changes. I commit only the changes that apply to B.1
and then try to go back to B. However, I get an error saying a file I
changed in B is not uptodate and it cannot merge. What am I doing
wrong and how can I get back to B?
Thanks all!
Joe
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Switching branches without committing changes
2008-03-21 3:27 Switching branches without committing changes Joe Fiorini
@ 2008-03-21 3:52 ` Jeff King
2008-03-21 4:06 ` Shawn O. Pearce
1 sibling, 0 replies; 10+ messages in thread
From: Jeff King @ 2008-03-21 3:52 UTC (permalink / raw)
To: Joe Fiorini; +Cc: git
On Thu, Mar 20, 2008 at 11:27:09PM -0400, Joe Fiorini wrote:
> I'm trying to switch branches without committing my changes. Is this
> possible? For example, I'm working on a site, I'm testing the
> implementation of a new technology (branch B), I'm not quite done there
> (or I forget to commit everything) and I want to implement something else
> new. I create a new branch off of B, called B.1, and then make some
> changes. I commit only the changes that apply to B.1 and then try to go
> back to B. However, I get an error saying a file I changed in B is not
> uptodate and it cannot merge. What am I doing wrong and how can I get
> back to B?
It sounds like you still have some changes in your working tree, and
that is preventing the branch switch.
Generally you would have stashed those changes before working on the
second task, like:
git checkout B
hack hack hack
# oops, I want to work on some other topic
git stash
git checkout -b B.1 B
hack hack hack
git commit
# now I'm ready to go back to my original work
git checkout B
git stash apply
That example uses git-stash, but you could just as easily do it with a
"work in progress" commit on a branch (which is how people did it before
git-stash was written). Now in your case, I get the impression you have
done this:
git checkout B
hack hack hack
# oops, I want to work on some other topic
git checkout -b B.1 ;# keeps all of your changes in the working tree
hack hack hack
# now my second topic is ready for commit
git add ;# selectively, or with git add -p
git commit
# now I'm ready to go back to my original work
git checkout B
but the last checkout doesn't work cleanly, because you have some
uncommitted changes in your working tree for some file 'A', but moving
from B.1 to B would also change 'A'.
So you actually need to merge those changes (actually, you are merging
the _undo_ of the B.1 changes) to get back to B. Unfortunately,
git-checkout is smart enough to do merges that don't touch the same
file, but not anything more complex. So instead, we can use stash again.
At this point, you can do:
git stash
git checkout B
git stash apply
which will actually invoke the "real" merge machinery to correctly sort
out the changes.
So what you did isn't wrong, but you probably would have had a much
easier time if you stashed _before_ doing the B.1 work. It would have
made your git-add easier, and it makes testing more accurate (since you
never actually tested the state committed to B.1; you tested B.1 + your
changes that will be commited on top of B).
Make sense?
-Peff
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Switching branches without committing changes
2008-03-21 3:27 Switching branches without committing changes Joe Fiorini
2008-03-21 3:52 ` Jeff King
@ 2008-03-21 4:06 ` Shawn O. Pearce
2008-03-21 4:10 ` Jeff King
1 sibling, 1 reply; 10+ messages in thread
From: Shawn O. Pearce @ 2008-03-21 4:06 UTC (permalink / raw)
To: Joe Fiorini; +Cc: git
Joe Fiorini <joe@faithfulgeek.org> wrote:
> I'm still a newbie to Git (and this list), so if I don't provide
> enough details please let me know what you need and I will provide :).
>
> I'm trying to switch branches without committing my changes. Is this
> possible? For example, I'm working on a site, I'm testing the
> implementation of a new technology (branch B), I'm not quite done
> there (or I forget to commit everything) and I want to implement
> something else new. I create a new branch off of B, called B.1, and
> then make some changes. I commit only the changes that apply to B.1
> and then try to go back to B. However, I get an error saying a file I
> changed in B is not uptodate and it cannot merge. What am I doing
> wrong and how can I get back to B?
Use `git checkout -m` to switch the branch anyway. However, if
there is a merge conflict while you are trying to carry the changes
to the other branch you may be faced with a merge conflict you are
not prepared to resolve, or simply cannot resolve in a reasonable
period of time.
You may want to use `git stash` to save your dirty changes off to
a safe area, then switch branches. Your changes won't be there,
but you can get them back with `git stash apply 0`. If things go
badly, you can go back to B.1 and use `git stash apply 0` to put
the changes back where they were, and figure out what you are going
to do from there.
--
Shawn.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Switching branches without committing changes
2008-03-21 4:06 ` Shawn O. Pearce
@ 2008-03-21 4:10 ` Jeff King
2008-03-21 4:40 ` Joe Fiorini
2008-03-21 4:42 ` Junio C Hamano
0 siblings, 2 replies; 10+ messages in thread
From: Jeff King @ 2008-03-21 4:10 UTC (permalink / raw)
To: Shawn O. Pearce; +Cc: Joe Fiorini, git
On Fri, Mar 21, 2008 at 12:06:47AM -0400, Shawn O. Pearce wrote:
> Use `git checkout -m` to switch the branch anyway. However, if
> there is a merge conflict while you are trying to carry the changes
> to the other branch you may be faced with a merge conflict you are
> not prepared to resolve, or simply cannot resolve in a reasonable
> period of time.
Ah, for some reason I didn't think of '-m' in the advice I gave (I guess
I have just never used it). It is almost certainly simpler than using a
'stash' at this point (but I do think stashing _beforehand_ still has
advantages).
-Peff
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Switching branches without committing changes
2008-03-21 4:10 ` Jeff King
@ 2008-03-21 4:40 ` Joe Fiorini
2008-03-21 4:42 ` Junio C Hamano
1 sibling, 0 replies; 10+ messages in thread
From: Joe Fiorini @ 2008-03-21 4:40 UTC (permalink / raw)
To: Jeff King; +Cc: Shawn O. Pearce, git
Thanks for the replies. I definitely like the stashing approach. Is
there any overhead or caveat to using stash a lot?
-Joe
On Mar 21, 2008, at 12:10 AM, Jeff King wrote:
> On Fri, Mar 21, 2008 at 12:06:47AM -0400, Shawn O. Pearce wrote:
>
>> Use `git checkout -m` to switch the branch anyway. However, if
>> there is a merge conflict while you are trying to carry the changes
>> to the other branch you may be faced with a merge conflict you are
>> not prepared to resolve, or simply cannot resolve in a reasonable
>> period of time.
>
> Ah, for some reason I didn't think of '-m' in the advice I gave (I
> guess
> I have just never used it). It is almost certainly simpler than
> using a
> 'stash' at this point (but I do think stashing _beforehand_ still has
> advantages).
>
> -Peff
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Switching branches without committing changes
2008-03-21 4:10 ` Jeff King
2008-03-21 4:40 ` Joe Fiorini
@ 2008-03-21 4:42 ` Junio C Hamano
2008-03-21 4:58 ` Joe Fiorini
2008-03-23 1:00 ` Xavier Maillard
1 sibling, 2 replies; 10+ messages in thread
From: Junio C Hamano @ 2008-03-21 4:42 UTC (permalink / raw)
To: Jeff King; +Cc: Shawn O. Pearce, Joe Fiorini, git
Jeff King <peff@peff.net> writes:
> On Fri, Mar 21, 2008 at 12:06:47AM -0400, Shawn O. Pearce wrote:
>
>> Use `git checkout -m` to switch the branch anyway. However, if
>> there is a merge conflict while you are trying to carry the changes
>> to the other branch you may be faced with a merge conflict you are
>> not prepared to resolve, or simply cannot resolve in a reasonable
>> period of time.
>
> Ah, for some reason I didn't think of '-m' in the advice I gave (I guess
> I have just never used it). It is almost certainly simpler than using a
> 'stash' at this point (but I do think stashing _beforehand_ still has
> advantages).
The thing is, that -m is really to mollify people who are _too_ accustomed
to CVS/SVN update behaviour. Over there, "scm update" does not give you
any choice other than having to merge.
With git, stashing or creating Park commits are very cheap operation and
unless you are reasonably sure that your local changes do not conflict
with the branch you are switching to, there is no strong reason to prefer
"checkout -m".
Switching branches with dirty state can have three scenarios:
(1) you are getting interrupted and your current local changes do not
belong to what you are going to commit after switching (e.g. "the
boss says fix that right away").
recommendation: stash, or Park commit
(2) you have started working but realized what you are working on belongs
to a new topic.
recommendation: checkout -b
(3) you have started working but realized what you are working on belongs
to an existing topic.
recommendation: checkout -m
In case (1), if the change is small, trivial or independent from what you
are switching branches to work on, you can "git checkout" (if the change
is about an unrelated thing, hopefully there won't be any overlap at the
file level) or "git checkout -m" (again, if the change is about an
unrelated thing, the merge hopefully would be trivial) to switch branches,
perform the unrelated change and commit only that unrelated change, and
"git checkout" (or "git checkout -m") to come back to where you started.
But if you had to use "-m" when switching branches, that means the change
you need to commit in the switched branch may have to include some changes
you will do to that modified file, and you would need per-hunk commit with
"git add -i" to exclude existing changes. In such a case, stashing the
local changes away before branch switching would be much easier workflow.
In case (2), the solution is always "checkout -b". There is no other
choice.
In case (3), the solution is always "checkout -m". Stashing, switching
and then unstashing will give the same conflicts as "checkout -m" would
give you, and the change you were working on has to be done on that
switched to branch, so there is no escaping from conflict resolution,
unless you are willing to redo your change on the breanch you switched to
again.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Switching branches without committing changes
2008-03-21 4:42 ` Junio C Hamano
@ 2008-03-21 4:58 ` Joe Fiorini
2008-03-23 1:00 ` Xavier Maillard
1 sibling, 0 replies; 10+ messages in thread
From: Joe Fiorini @ 2008-03-21 4:58 UTC (permalink / raw)
To: Junio C Hamano; +Cc: Jeff King, Shawn O. Pearce, git
Thanks all for the great info! The scenarios you describe, Junio,
make perfect sense. In fact, that's pretty much the way I think when
I'm coding and decided to branch or not to branch (that is the
question). Along the lines of those scenarios (maybe this should be a
separate post), are there any guidelines or best practices on when/if
to sync your branches with master (hope that's not a stupid question,
I'm still learning)?
-Joe
On Mar 21, 2008, at 12:42 AM, Junio C Hamano wrote:
> Jeff King <peff@peff.net> writes:
>
>> On Fri, Mar 21, 2008 at 12:06:47AM -0400, Shawn O. Pearce wrote:
>>
>>> Use `git checkout -m` to switch the branch anyway. However, if
>>> there is a merge conflict while you are trying to carry the changes
>>> to the other branch you may be faced with a merge conflict you are
>>> not prepared to resolve, or simply cannot resolve in a reasonable
>>> period of time.
>>
>> Ah, for some reason I didn't think of '-m' in the advice I gave (I
>> guess
>> I have just never used it). It is almost certainly simpler than
>> using a
>> 'stash' at this point (but I do think stashing _beforehand_ still has
>> advantages).
>
> The thing is, that -m is really to mollify people who are _too_
> accustomed
> to CVS/SVN update behaviour. Over there, "scm update" does not give
> you
> any choice other than having to merge.
>
> With git, stashing or creating Park commits are very cheap operation
> and
> unless you are reasonably sure that your local changes do not conflict
> with the branch you are switching to, there is no strong reason to
> prefer
> "checkout -m".
>
> Switching branches with dirty state can have three scenarios:
>
> (1) you are getting interrupted and your current local changes do not
> belong to what you are going to commit after switching (e.g. "the
> boss says fix that right away").
>
> recommendation: stash, or Park commit
>
> (2) you have started working but realized what you are working on
> belongs
> to a new topic.
>
> recommendation: checkout -b
>
> (3) you have started working but realized what you are working on
> belongs
> to an existing topic.
>
> recommendation: checkout -m
>
> In case (1), if the change is small, trivial or independent from
> what you
> are switching branches to work on, you can "git checkout" (if the
> change
> is about an unrelated thing, hopefully there won't be any overlap at
> the
> file level) or "git checkout -m" (again, if the change is about an
> unrelated thing, the merge hopefully would be trivial) to switch
> branches,
> perform the unrelated change and commit only that unrelated change,
> and
> "git checkout" (or "git checkout -m") to come back to where you
> started.
> But if you had to use "-m" when switching branches, that means the
> change
> you need to commit in the switched branch may have to include some
> changes
> you will do to that modified file, and you would need per-hunk
> commit with
> "git add -i" to exclude existing changes. In such a case, stashing
> the
> local changes away before branch switching would be much easier
> workflow.
>
> In case (2), the solution is always "checkout -b". There is no other
> choice.
>
> In case (3), the solution is always "checkout -m". Stashing,
> switching
> and then unstashing will give the same conflicts as "checkout -m"
> would
> give you, and the change you were working on has to be done on that
> switched to branch, so there is no escaping from conflict resolution,
> unless you are willing to redo your change on the breanch you
> switched to
> again.
>
>
>
>
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Switching branches without committing changes
2008-03-21 4:42 ` Junio C Hamano
2008-03-21 4:58 ` Joe Fiorini
@ 2008-03-23 1:00 ` Xavier Maillard
2008-03-24 14:46 ` Joe Fiorini
1 sibling, 1 reply; 10+ messages in thread
From: Xavier Maillard @ 2008-03-23 1:00 UTC (permalink / raw)
To: Junio C Hamano; +Cc: peff, spearce, joe, git
Jeff King <peff@peff.net> writes:
> On Fri, Mar 21, 2008 at 12:06:47AM -0400, Shawn O. Pearce wrote:
>
>> Use `git checkout -m` to switch the branch anyway. However, if
>> there is a merge conflict while you are trying to carry the changes
>> to the other branch you may be faced with a merge conflict you are
>> not prepared to resolve, or simply cannot resolve in a reasonable
>> period of time.
>
> Ah, for some reason I didn't think of '-m' in the advice I gave (I guess
> I have just never used it). It is almost certainly simpler than using a
> 'stash' at this point (but I do think stashing _beforehand_ still has
> advantages).
The thing is, that -m is really to mollify people who are _too_ accustomed
to CVS/SVN update behaviour. Over there, "scm update" does not give you
any choice other than having to merge.
This post is *yet* another valuable candidate to put onto the wiki.
Xavier
--
http://www.gnu.org
http://www.april.org
http://www.lolica.org
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Switching branches without committing changes
2008-03-23 1:00 ` Xavier Maillard
@ 2008-03-24 14:46 ` Joe Fiorini
2008-03-24 19:07 ` Jeff King
0 siblings, 1 reply; 10+ messages in thread
From: Joe Fiorini @ 2008-03-24 14:46 UTC (permalink / raw)
To: Xavier Maillard; +Cc: Junio C Hamano, peff, spearce, git
Can you send me a link to the official wiki? If I have access to it,
I will see about updating it today.
-Joe
On Mar 22, 2008, at 9:00 PM, Xavier Maillard wrote:
>
> Jeff King <peff@peff.net> writes:
>
>> On Fri, Mar 21, 2008 at 12:06:47AM -0400, Shawn O. Pearce wrote:
>>
>>> Use `git checkout -m` to switch the branch anyway. However, if
>>> there is a merge conflict while you are trying to carry the changes
>>> to the other branch you may be faced with a merge conflict you are
>>> not prepared to resolve, or simply cannot resolve in a reasonable
>>> period of time.
>>
>> Ah, for some reason I didn't think of '-m' in the advice I gave (I
>> guess
>> I have just never used it). It is almost certainly simpler than
>> using a
>> 'stash' at this point (but I do think stashing _beforehand_ still has
>> advantages).
>
> The thing is, that -m is really to mollify people who are _too_
> accustomed
> to CVS/SVN update behaviour. Over there, "scm update" does not
> give you
> any choice other than having to merge.
>
> This post is *yet* another valuable candidate to put onto the wiki.
>
> Xavier
> --
> http://www.gnu.org
> http://www.april.org
> http://www.lolica.org
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Switching branches without committing changes
2008-03-24 14:46 ` Joe Fiorini
@ 2008-03-24 19:07 ` Jeff King
0 siblings, 0 replies; 10+ messages in thread
From: Jeff King @ 2008-03-24 19:07 UTC (permalink / raw)
To: Joe Fiorini; +Cc: Xavier Maillard, git
On Mon, Mar 24, 2008 at 10:46:06AM -0400, Joe Fiorini wrote:
> Can you send me a link to the official wiki? If I have access to it, I
> will see about updating it today.
http://git.or.cz/gitwiki
-Peff
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2008-03-24 19:08 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-03-21 3:27 Switching branches without committing changes Joe Fiorini
2008-03-21 3:52 ` Jeff King
2008-03-21 4:06 ` Shawn O. Pearce
2008-03-21 4:10 ` Jeff King
2008-03-21 4:40 ` Joe Fiorini
2008-03-21 4:42 ` Junio C Hamano
2008-03-21 4:58 ` Joe Fiorini
2008-03-23 1:00 ` Xavier Maillard
2008-03-24 14:46 ` Joe Fiorini
2008-03-24 19:07 ` Jeff King
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).