git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Prevent switching branches when local modifications exist
@ 2012-06-13 22:52 Christoph Bartoschek
  2012-06-13 23:35 ` Junio C Hamano
  0 siblings, 1 reply; 3+ messages in thread
From: Christoph Bartoschek @ 2012-06-13 22:52 UTC (permalink / raw)
  To: git

Hi,

how can I prevent that git allows me to switch branches when there are 
uncommited local modifications?

[14.1 (steiner_topo)]$ git checkout -b ergo
Switched to a new branch 'ergo'
[14.1 (ergo)]$ echo blub >> src/invtree.C
[14.1 (ergo)]$ git checkout steiner_topo
M       src/invtree.C
Switched to branch 'steiner_topo'
[14.1 (steiner_topo)]$ 

How can the last checkout be prevented?

-- 
Christoph Bartoschek

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Prevent switching branches when local modifications exist
  2012-06-13 22:52 Prevent switching branches when local modifications exist Christoph Bartoschek
@ 2012-06-13 23:35 ` Junio C Hamano
  2012-06-14  7:17   ` Christoph Bartoschek
  0 siblings, 1 reply; 3+ messages in thread
From: Junio C Hamano @ 2012-06-13 23:35 UTC (permalink / raw)
  To: Christoph Bartoschek; +Cc: git

Christoph Bartoschek <ponto@pontohonk.de> writes:

> Hi,
>
> how can I prevent that git allows me to switch branches when there are 
> uncommited local modifications?
>
> [14.1 (steiner_topo)]$ git checkout -b ergo
> Switched to a new branch 'ergo'
> [14.1 (ergo)]$ echo blub >> src/invtree.C
> [14.1 (ergo)]$ git checkout steiner_topo
> M       src/invtree.C
> Switched to branch 'steiner_topo'
> [14.1 (steiner_topo)]$ 
>
> How can the last checkout be prevented?

The question sounds similar to asking "How can I prevent 'rm -r'
from removing anything when I type it in my home directory."  It is
useful for 'rm -r' to remove everything recursively, but sometimes
you would want to prevent you from running it by mistake in a wrong
place. In general, there is no good direct solution for that.

A few usual ways people deal with this kind of issue are (1) to make
it easier to notice that they are "in a wrong place" (e.g. by having
the current directory in their prompt to avoid 'rm -r' in $HOME) and
training themselves to be careful, (2) to make it possible to
recover if that happens by mistake (e.g. by having .snapshot on the
filer).

For "checkout", an approach that corresponds to (1) is to have
branches and status in the prompt (available from git-completion).
Fortunately for (2) there isn't anything special necessary, as
checking out a different branch with "git checkout" does not lose
information, after creating and checking out the steiner_topo
branch, you can use checkout again to come back to ergo branch.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Prevent switching branches when local modifications exist
  2012-06-13 23:35 ` Junio C Hamano
@ 2012-06-14  7:17   ` Christoph Bartoschek
  0 siblings, 0 replies; 3+ messages in thread
From: Christoph Bartoschek @ 2012-06-14  7:17 UTC (permalink / raw)
  To: git

Junio C Hamano wrote:

> Christoph Bartoschek <ponto@pontohonk.de> writes:
> 
>> Hi,
>>
>> how can I prevent that git allows me to switch branches when there are
>> uncommited local modifications?
>>
>> [14.1 (steiner_topo)]$ git checkout -b ergo
>> Switched to a new branch 'ergo'
>> [14.1 (ergo)]$ echo blub >> src/invtree.C
>> [14.1 (ergo)]$ git checkout steiner_topo
>> M       src/invtree.C
>> Switched to branch 'steiner_topo'
>> [14.1 (steiner_topo)]$
>>
>> How can the last checkout be prevented?
> 
> The question sounds similar to asking "How can I prevent 'rm -r'
> from removing anything when I type it in my home directory."  It is
> useful for 'rm -r' to remove everything recursively, but sometimes
> you would want to prevent you from running it by mistake in a wrong
> place. In general, there is no good direct solution for that.
> 
> A few usual ways people deal with this kind of issue are (1) to make
> it easier to notice that they are "in a wrong place" (e.g. by having
> the current directory in their prompt to avoid 'rm -r' in $HOME) and
> training themselves to be careful, (2) to make it possible to
> recover if that happens by mistake (e.g. by having .snapshot on the
> filer).
> 
> For "checkout", an approach that corresponds to (1) is to have
> branches and status in the prompt (available from git-completion).
> Fortunately for (2) there isn't anything special necessary, as
> checking out a different branch with "git checkout" does not lose
> information, after creating and checking out the steiner_topo
> branch, you can use checkout again to come back to ergo branch.


So I assume there is no option for this currently.

Git checkout already behaves different from 'rm -r' by preventing me from 
overwritting modified files with changes in the target branch. I just would 
like to expand this to all files. A branch change is only possible if 
everything is clean. (I see that this does not carry over to untracked 
files).

Maybe I have to go with a wrapper for checkout like:

if [[ $(git status --porcelain) =~ "M " ]]; then echo "NO"; exit 0; else git 
checkout "$@"; fi

A little background: We currently evaluate adoption of git instead of svn 
and some users are scared of any code change by a tool. They even do not 
trust any merge at all. Before they run 'svn update' they copy all their 
changes away, carefully merge manually and then go on. 

Thanks
Christoph

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-06-14  7:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-06-13 22:52 Prevent switching branches when local modifications exist Christoph Bartoschek
2012-06-13 23:35 ` Junio C Hamano
2012-06-14  7:17   ` Christoph Bartoschek

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).