Putting uncommitted changes at Master to a new branch by Git

How can you put uncommitted changes to a branch TEST when I am at the branch master?

  • Move Old Commit to New Branch
  • How do I list and fetch remote branches after SVN to Git migration?
  • Simple Git Feature Branch Issue
  • Git: Managing Branches
  • Difference between “upstream” branch and branch with owner's name
  • Keep files on working tree but make them available to any branch?
  • Alternate/multiple directory structure mappings within a git repository
  • Git pass gitignore file as command line argument
  • If you git rebase and push, vs if you git pull, merge, and push, does it help the project maintainer the work of merging?
  • Avoid repository creation on typo
  • Xcode loses git history when moving a file
  • Change the original branch for a feature branch
  • 4 Solutions collect form web for “Putting uncommitted changes at Master to a new branch by Git”

    You can just checkout to the test branch and then commit. You don’t lose your uncommited changes when moving to another branch.

    Supposing you are at the master branch:

    git checkout test
    git add .
    git add deletedFile1
    git add deletedFile2
    ...
    git commit -m "My Custom Message"
    

    I am not really sure about the deleted files, but I guess they aren’t included when you use git add .

    Also you can create a new branch and switch to it by doing:

    git checkout -b new_branch
    git add .
    

    I use this all the time because I always forget to start a new branch before I start editing code.

    Why not just use git stash. I think it’s more intuitive like a copy-and-paste.

    $ git branch
      develop
    * master
      feature1
      TEST
    $
    

    You have some files in your current branch that you want to move.

    $ git status
    # On branch master
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #      modified:   awesome.py
    #
    # Changed but not updated:
    #   (use "git add <file>..." to update what will be committed)
    #
    #      modified:   linez.py
    #
    $
    $ git stash
    Saved working directory and index state \
      "WIP on master: 934beef added the index file"
    HEAD is now at 934beef added the index file
    (To restore them type "git stash apply")
    $
    $ git status
    # On branch master
    nothing to commit (working directory clean)
    $
    $
    $ git stash list
    stash@{0}: WIP on master: 934beef ...great changes
    $
    

    Move to the other branch.

    $ git checkout TEST
    

    And apply

    $ git stash apply
    # On branch master
    # Changed but not updated:
    #   (use "git add <file>..." to update what will be committed)
    #
    #      modified:   awesome.py
    #      modified:   linez.py
    #
    

    I also like git stash because I use git flow, which complains when you want to finish a feature branch whilst having changes still in your working directory.

    Just like @Mike Bethany, this happens to me all the time because I work on a new problem while forgetting I am still on another branch. So you can stash your work, git flow feature finish..., and git stash apply to new git flow feature start ... branch.

    git checkout TEST
    git add file1 file2
    git commit
    
    Git Baby is a git and github fan, let's start git clone.