{"thread":{"id":"20954","subject":"R: Commited to wrong branch","startedAt":"2009-09-15T13:45:04Z","lastAt":"2009-09-15T13:45:04Z","messageCount":1,"participants":["Daniele Segato"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"123254","messageId":"9accb4400909150645g72e3d78bhd1621e48d3857d99@mail.gmail.com","threadId":"20954","inReplyTo":null,"subject":"R: Commited to wrong branch","fromName":"Daniele Segato","fromEmail":"daniele.bilug@gmail.com","sentAt":"2009-09-15T13:45:04Z","receivedAt":"2009-09-15T13:45:04Z","isPatch":false,"sender":{"key":"daniele.bilug@gmail.com","avatar":null},"body":"I replied to Howard directly without CCing the Git ML (as a mistake)\n\nanyway he solved the problem on his own now.\n\nthis was my first reply that didn't reached the list\n\n\nOn Tue, Sep 15, 2009 at 12:31 PM, Howard Miller\n<howard@e-learndesign.co.uk> wrote:\n> I had made some changes to some files and then done a commit. Only\n> then did I realise that I had the wrong branch checked out. To make\n> matters worse I then did a 'git reset HEAD^' which means that I can\n> now no longer switch branches. I am stuck. I had some advice (thanks!)\n> but it was not complete. I'd appreciate some more help.\n\nI think that explaining what you did and what happened along your\nmodification will help you to understand.\nIt's not that hard, really :)\n\nso this are the steps you followed (confirmed by your reply to Martin)\n\n 1 - you were on branch X, thinking your were on branch Y\n\nBranch X point to a commit which is the last commit of branch X:\n\nyou can visualize the situation like this\n\n               BRANCH Y\n                    |\nc1 - c2 - c3 - c5\n        \\\n         c4 - c6\n                 |\n              BRANCH X (and your current HEAD)\n\nthe cN stuffs are commits\n\nBranchX, BranchY and HEAD are only pointers to a commit\n\n 2 - you worked on on branch X\n\nmodified some files/added new one/whatever\n\nnow you may have some untracked files (new files)\nand some untracked modification (modified files)\n\nuntracked means that those files/modification aren't in the git\nrepository, they are not indexed\n\n 3 - git add .\n\nYou ask git to index the modification to prepare a commit: we say that\nfiles are \"staged\"\n\nAt this time HEAD and BRANCH X are still on c6 commit (see graphic above)\n\n 4 - git commit\n\nOk what happen here?\n\n               BRANCH Y\n                    |\nc1 - c2 - c3 - c5\n        \\\n         c4 - c6 - c7\n                       |\n                  BRANCH X (and your current HEAD)\n\nyou created a new commit and moved the 2 pointers.\n\n\n6 - you realized the mistake\n\nnow what you want here is to make your Branch X point again the c6\ncommit and, probably,\nsave your modification for c7\n\nbut you actually panicked :)\n\nand you did:\n\n 5 - git reset HEAD^\n\nLet's see what happen to the index:\n\n               BRANCH Y\n                    |\nc1 - c2 - c3 - c5\n        \\\n         c4 - c6 -c7\n                 |\n              BRANCH X (and your current HEAD)\n\nseems right uh?\n\nno you have a lot of untracked files in your directory\n\nthat's because git reset doesn't change your directory content\nit only work on indexes.\n\nSo what you now have in your project directory is what you did on the\ntop of your initial Branch X\n\ntry a\ngitk --all\n\nit should show you that\n\n\nNow you have 2 options:\n1) discard what you did for the wrong commit c7 (and restart again on branch Y)\n2) try to backup your work and migrate it to branch Y\n\nthe 1) is the easiest one: just execute\ngit reset HEAD --hard\nthe option --hard ask git to give you a \"clean\" reset modifying your\nworking directory to be exactly\nlike the indexed c6 commit.\n\nthen checkout branch Y:\ngit checkout Y\n\nand restart to work on that branch\n\n\nif you want to recover what you did to avoid re-doing it then the\nthings became a little harder but not\nthat much :)\n\nthere are many way to do it because git gave you a lot of tools\n\nwe can help if you calm down :)\n\nfor now: question about this?\n\nregards,\nDaniele\n"}]}