Search code examples
gitgit-branchgit-commit

Local uncommited changes in master. Need to switch to another branch


I am WebDev intern and I have done some work in the master branch. And I have done a week's work without realising that I am in the master branch. I need to switch branches so that I can commit my local changes from a branch that is not the master branch. What I need to do:

  • Take all the local changes from my master branch to some other branch.
  • Commit changes from that branch.
  • Give you a correct answer verification. :P

I went through some answers but I don't think that they are catering to my exact situation. Thanks in advance. :)


Solution

  • The easiest thing for you to do would be to stash your working directory (and possibly stage, if you have staged any files), create a new feature branch, and then apply your stash there.

    # from the master branch
    git stash                      # stash your work
    git checkout -b your_feature   # create and switch to a feature branch
    git stash apply                # then apply your work to that branch
    

    Then, you may commit your work to the your_feature branch:

    git add .
    git commit -m 'Finished my work'
    

    The git stash command works by making two (or sometimes three) commits containing the contents of your working directory and stage. It then would rollback master to the point where you first began.