From: Junio C Hamano <gitster@pobox.com>
To: Jeff King <peff@peff.net>
Cc: "Shawn O. Pearce" <spearce@spearce.org>,
Joe Fiorini <joe@faithfulgeek.org>,
git@vger.kernel.org
Subject: Re: Switching branches without committing changes
Date: Thu, 20 Mar 2008 21:42:56 -0700 [thread overview]
Message-ID: <7vod98u1pr.fsf@gitster.siamese.dyndns.org> (raw)
In-Reply-To: <20080321041013.GA2502@coredump.intra.peff.net> (Jeff King's message of "Fri, 21 Mar 2008 00:10:13 -0400")
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.
next prev parent reply other threads:[~2008-03-21 4:43 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
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=7vod98u1pr.fsf@gitster.siamese.dyndns.org \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=joe@faithfulgeek.org \
--cc=peff@peff.net \
--cc=spearce@spearce.org \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.