Putting uncommitted changes at Master to a new branch by Git

后端 未结 4 738
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 02:03

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

4条回答
  •  天命终不由人
    2021-01-30 02:26

    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 ..." to unstage)
    #
    #      modified:   awesome.py
    #
    # Changed but not updated:
    #   (use "git add ..." 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 ..." 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.

提交回复
热议问题