2

I just discovered I did a big mistake. I made changes in the master branch for several days when I had intended to do it in the feature-x branch.

How do I switch all changes in the current branch (master) to feature-x branch?
I'd also like to have master branch in a clean state. (When I do git status on master, it should show no changes, but when I do git status on feature-x it should show all changes I had so far).

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
bodacydo
  • 75,521
  • 93
  • 229
  • 319

1 Answers1

3

You can

That assumes that all your last commits on master were for feature-x.
If those commits were mixed with some for master, as hoppjerka suggests in the comments, you would use an interactive rebase:

git checkout master
git rebase -i {master@"3 days ago"}'

You would drop the ones you don't want on master.


I have to say I haven't commited yet. All the changes currently in the master are showing in git status. So I only need to move the changes

Then

git stash
git checkout feature-x
git stash pop
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • If you only want to remove a few commits from master you can do 'git rebase -i {master@"3 days ago"}' and remove the commits you have moved to feature-x. – such Nov 23 '14 at 09:11
  • @hoppjerka true. I have included your suggestion in the answer for more visibility. – VonC Nov 23 '14 at 09:15
  • I have to say I haven't commited yet. All the changes currently in the master are showing in `git status`. So I only need to move the changes. I dont think I need to cherry pick. .. I think I only need to `git stash`, `git branch` and `git stash pop`. – bodacydo Nov 23 '14 at 09:42
  • @bodacydo I agree. I have edited the answer accordingly – VonC Nov 23 '14 at 09:43
  • Thanks you sir. This was easy. :) Upvoted & accepted. I'll refer to your answer in the future as well when I've commited and then realized branches weren't switched. Happens often. – bodacydo Nov 23 '14 at 09:46
  • Oh goodness, I think I just lost 2 days of work..... I did `git stash`, `git checkout -b feature-x`, `git stash pop`. Then I did `git status` - verified that changes are there. Then I `git checkout master`. Then I did `git status` and saw the changes in the master branch, but since I thought they were already saved in feature-x branch, I did `git checkout -f` to throw away local changes in master branch. Then I `git checkout feature-x` and all the changes are gone in `feature-x` branch as well........... whooooops. what did i do wrong? – bodacydo Nov 23 '14 at 09:59
  • 1
    @bodacydo you never added or committed those changes. Try git reflog to see if the stash might still be accessible there. – VonC Nov 23 '14 at 10:04
  • 1
    @bodacydo git stash apply the SHA1 previously displayed by git stash pop – VonC Nov 23 '14 at 10:06
  • trying now!! thanks (although i'm seeing only `dcff78b... HEAD@{0}: checkout: moving from master to feature-x` and `dcff78b... HEAD@{1}: checkout: moving from feature-x to master` messages) – bodacydo Nov 23 '14 at 10:07
  • 1
    @bodacydo here you go! I would still advise you to make a commit (even an intermediate one), or a backup of some sort ;) – VonC Nov 23 '14 at 10:10
  • Thanks a lot! Doing a commit now. I thought changes were per-branch. So when I see changes in `git status` in master, they're unrelated to changes in feature-x. Could you comment why `git commit -f` in master cleaned up all the changes in feature-x, too? I think I don't understand how git works fully yet – bodacydo Nov 23 '14 at 10:11
  • 1
    @bodacydo I think the checkout -f was the one which removed your local changes. But a local change is associated to your working tree, not to a branch. They are candidate to be added to the index, to then prepare the next commit. I would recommend following the quick tutorial https://try.github.io/levels/1/challenges/1 to better understand git. – VonC Nov 23 '14 at 10:16
  • sorry it was `git checkout -f` I mistyped when i wrote `commit -f`. you're right. i get my mistake now. thanks for tutorial! – bodacydo Nov 23 '14 at 10:17