From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ricardo Wurmus Newsgroups: gmane.emacs.devel Subject: Re: Git question: when using branches, how does git treat working files when changing branches? Date: Wed, 28 Oct 2015 21:04:14 +0100 Message-ID: <878u6miwu9.fsf@elephly.net> References: <20151028192017.GC2538@acm.fritz.box> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1446062697 32114 80.91.229.3 (28 Oct 2015 20:04:57 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 28 Oct 2015 20:04:57 +0000 (UTC) Cc: emacs-devel@gnu.org To: Alan Mackenzie Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Oct 28 21:04:48 2015 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZrWxg-0003Am-Ok for ged-emacs-devel@m.gmane.org; Wed, 28 Oct 2015 21:04:44 +0100 Original-Received: from localhost ([::1]:40506 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZrWxf-0001RS-Ff for ged-emacs-devel@m.gmane.org; Wed, 28 Oct 2015 16:04:43 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:38047) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZrWxR-0001R7-If for emacs-devel@gnu.org; Wed, 28 Oct 2015 16:04:30 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZrWxM-0002np-EU for emacs-devel@gnu.org; Wed, 28 Oct 2015 16:04:29 -0400 Original-Received: from sender-p126.zohomail.com ([74.201.154.126]:25931) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZrWxM-0002nV-5R for emacs-devel@gnu.org; Wed, 28 Oct 2015 16:04:24 -0400 Original-Received: from localhost (xd9ba63ad.dyn.telefonica.de [217.186.99.173]) by mx.zohomail.com with SMTPS id 1446062659324515.4844935351362; Wed, 28 Oct 2015 13:04:19 -0700 (PDT) User-agent: mu4e 0.9.13; emacs 24.5.1 In-reply-to: <20151028192017.GC2538@acm.fritz.box> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 74.201.154.126 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:192822 Archived-At: Alan Mackenzie writes: > So, what happens to to changes in the working directory when changing > branches? > 1. git refuses to change branches because there are uncommitted changes. > 2. git changes branches, discarding all uncommitted changes. > 3. git changes branches, leaving the changes from the previous branch in > the working directory. It’s a mix of 1 and 3. (A branch is just a pointer to a commit in the graph. When creating a branch you create a named pointer to the last commit (the one pointed to by “HEAD”). When you switch branches you really just tell git to travel to the commit in the graph by following the named pointer.) If there are new, uncommitted files in your working directory they will remain as you switch branches. Git refuses to switch to another branch when the uncommitted file is created by a commit in the target branch, so that you won’t accidentally lose the file. For tracked files git will refuse to change branches: ~~~~~~~~ error: Your local changes to the following files would be overwritten by checkout: /path/to/the/modified/file.el Please, commit your changes or stash them before you can switch branches. Aborting ~~~~~~~ > What I really want to happen is > 4. git maintains uncommitted changes separately in each branch. > > I suspect 4. is not the way git works. So, how do I best simulate 4.? > I would like to be able to move freely between git branches without > suffering stupid restrictions like having to worry about preserving > changes in the working directory. Is there some variant of "git stash" > which might do what I want? Git only cares about commits and pointers to commits. If you have uncommitted changes and you want to make sure they are not lost as you switch branches, the git way is to commit them, e.g. git add /path/to/the/modified/file.el git commit -m "WIP" git checkout the-other-branch and then reset the last commit upon your return: git checkout first-branch git reset HEAD^ # This means: reset to the parent commit of the commit that “HEAD” # points to. The changes introduced by the commit remain in your # directory, but the commit is gone. This is safe and you are very unlikely to ever lose your changes because you created a commit object. (It’s not easy to really lose commits by accident.) An alternative is to use “git stash” before switching and “git stash pop” after your return. You can have multiple stashes, but it’s easier to get lost than it is with commits. ~~ Ricardo