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

Re: Commited to wrong branch

From
Björn Steinbrink <b.steinbrink@gmx.de>
Date
Sep 15, 2009, 11:19 UTC
Message-ID
<20090915111922.GB31846@atjola.homenet>
In-Reply-To
<46a038f90909150355h20b39c71w4af7e2be2920fdbb@mail.gmail.com>
On 2009.09.15 12:55:58 +0200, Martin Langhoff wrote:
Show 21 quoted lines
> On Tue, Sep 15, 2009 at 12:31 PM, Howard Miller
> <howard@e-learndesign.co.uk> wrote:
> > I am resurrecting a discussion from a week or two back (been on
> > holiday).  As follows...
> >
> > I had made some changes to some files and then done a commit. Only
> > then did I realise that I had the wrong branch checked out. To make
> > matters worse I then did a 'git reset HEAD^' which means that I can
> > now no longer switch branches. I am stuck. I had some advice (thanks!)
> > but it was not complete. I'd appreciate some more help.
> 
> Hi Howard,
> 
> just to make sure I understand your issue
> 
>   1 - you were on branch X, thinking your were on branch Y
>   2 - edit, diff, commit, realised the mistake
>   3 - git reset HEAD^
> 
> so if you now run `git status` and `git diff` it will show your
> changes as if they were uncommitted and unstaged.
Not "as if", they are.
Show 11 quoted lines
> (Before you start with various attempts to recover below, a great
> trick is to make an instant-backup in case things go wrong: cd .. / ;
> cp -pr moodle.git moodle-backup.git ; cd moodle.git )
> 
> You can now try do do
> 
>   4 - git checkout Y
> 
> and if the changes are on files that don't change between X and Y,
> then git will change the branches and keep your changes there. If the
> files are different between X and Y, it won't work.

Well, then you could use "git checkout -m Y", to have git try a three-way merge (which might of course leave conflicts).

Show 11 quoted lines
> What I can recommend is to save your patch, as follows
> 
>   5 - git diff > tempchanges.patch
>   6 - git reset --hard # this will discard your changes, careful
>   7 - git checkout Y
>   8 - patch -p1 < tempchanges.patch
> 
> The patch may not apply cleanly :-) -- note that patch is more
> tolerant of iffy merges than git's internal implementation ("git
> apply") -- so it will succeed more often... but the results need
> review.

But a lot worse than the usual 3-way merge stuff, like "checkout -m" or "stash apply". The advantage of "stash" + "stash apply" is that, in case of conflicts, you can easily retry to fix them over and over again, while with "checkout -m", you can't easily start over AFAIK.

> There is a more git-style approach that is to use git-stash -- it uses
> git-apply and may not do what you want.

Only "stash apply --index" uses "git apply", and only to re-apply the staged changes. The changes for the working tree are applied using a 3way merge.

Björn
Previous: Martin Langhoff
Message 20 of 20 in “Commited to wrong branch”
  1. Howard MillerSep 15, 2009
  2. Martin LanghoffSep 15, 2009
  3. Howard MillerSep 15, 2009
  4. Martin LanghoffSep 15, 2009
  5. Howard MillerSep 15, 2009
  6. Martin LanghoffSep 15, 2009
  7. Howard MillerSep 15, 2009
  8. Björn SteinbrinkSep 15, 2009
  9. Howard MillerSep 15, 2009
  10. Martin LanghoffSep 15, 2009
  11. Howard MillerSep 15, 2009
  12. Björn SteinbrinkSep 15, 2009
  13. Howard MillerSep 15, 2009
  14. Martin LanghoffSep 15, 2009
  15. Björn SteinbrinkSep 15, 2009
  16. Howard MillerSep 15, 2009
  17. Howard MillerSep 15, 2009
  18. Johannes SixtSep 15, 2009
  19. Martin LanghoffSep 15, 2009
  20. Björn SteinbrinkSep 15, 2009

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.