git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Prevent switching branches when local modifications exist

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 13, 2012, 23:35 UTC
Message-ID
<7vfw9yg4z1.fsf@alter.siamese.dyndns.org>
In-Reply-To
<h5ina9-g9n.ln1@homer.bruehl.pontohonk.de>
Christoph Bartoschek <ponto@pontohonk.de> writes:
Show 14 quoted lines
> 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.

Previous: Christoph BartoschekNext: Christoph Bartoschek
Message 2 of 3 in “Prevent switching branches when local modifications exist”
  1. Christoph BartoschekJun 13, 2012
  2. Junio C HamanoJun 13, 2012
  3. Christoph BartoschekJun 14, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.