I like the --autostash flag. 
But actually you just could write an alias co="git stash && git checkout" and 
use co dev for the same purpose, rendering the change irrelevant.

-----Original Message-----
From: Junio C Hamano [mailto:gits...@pobox.com] 
Sent: Thursday, November 16, 2017 1:20 AM
To: Ninivaggi Mattia
Cc: git@vger.kernel.org
Subject: Re: [Feature- Request] Option to commit after checking out branch 
command is made

Ninivaggi Mattia <mattia.niniva...@helsana.ch> writes:

> 1. I checkout a branch, without having commited first
>     > git checkout dev
> 2. I get this error message:
>     > error: Your local changes to the following files would be overwritten 
> by checkout:
>     > // List of files
>     > // ..
>     > //
>     > Please commit your changes or stash them before you switch branches.
>
> But I would rather prefer a scenario like this:
> ...
> 1. I checkout a branch, without having commited first
>     > git checkout dev
> 2. I get a message like this:
>     > Your local changes to the following files would be overwritten by 
> checkout:
>     > // List of files
>     > // ..
>     > //
>     > Would you want to commit first? (y/n))
>
> IF y --> prompt for commit message and commit automatically

I do not think you want to do this for a few reasons.

 * The "please commit or stash" is merely a suggestion whose primary
   purpose is to silence clueless newbies who would have complained
   "Git said 'error: ... overwritten by checkout' and I do not know
   what to do next; the error message is so unhelpful" otherwise.
   Majority of the time when I see this message, it is because I
   forgot that I was in the middle of doing something (meaning: I am
   far from finished with the changes I was working on), and I would
   not be ready to commit.  I'd rather keep working to get the work
   into a more reasonable shape before committing, or stash the
   changes first if the task I wanted to do on that "dev" branch is
   more urgent and what I was in the middle of doing is lower
   priority.  

   Because of this, I would expect many users (including the ones
   who are right now newbies but will have gained experience to
   become experts in the future) to appreciate "stash before switch"
   a lot more than "commit first before switch".

 * People write scripts that use "git checkout" to switch branches,
   and they rely on the command to fail in this situation, instead
   of going interactive and gets stuck waiting for an input (which
   may never come).  Because of this, the updated behaviour you
   propose must never be the default, and at least must be protected
   behind a flag, something like "git checkout --autostash dev" (or
   "--autocommit", if you insist).  With that, you could do

        [alias]
                co = checkout --autostash

   and train your fingers to say "git co dev".

Of course, you can have a "git-co" script on your $PATH, which runs "git 
checkout $1", lets it fail just like it does now, and then does "git commit", 
if you really want the behaviour.  Again, you can then use "git co dev" and you 
do not have to worry about breaking people's scripts that depends on "git 
checkout" to fail without going interactive.

Reply via email to