Move develop changes to a new feature branch

For my project I use git-flow. I have master and develop branch synchronized with remote-repo
Usually for all changes I create a feature branch, edit the files, commit the changes on the feature branch and close the branch with a merge in develop.

But sometimes I forget to create a feature branch and I edit files directly in develop. How can I move the canges in a new feature branch?

  • Can git undo a checkout of unstaged files
  • How to get Vim Ctrl-P plugin to index files inside a git submodule
  • How to use cocoapods locally?
  • Automated deployment of Grails/Java web application to Tomcat7 from Git
  • When I clone project from Github to Visual Studio, all DLL file from nuget missing
  • Delete and Redeploy Rails app to heroku
  • Could not push to github using IntellijIDEA
  • Remove an Openshift server from RHC
  • Why does re-merging result in a repeated merge conflict in git?
  • Search code inside a Github project
  • what should be in the git description file?
  • Can't Push Rails Project to Heroku
  • 4 Solutions collect form web for “Move develop changes to a new feature branch”

    If you’ve already made the commit to develop, and your history looks like this:

    A---B---C---D develop
    

    Here, C is the last correct commit on develop, and D should have been committed to a feature branch. Do the following (while on develop):

    git branch feature      # creates feature branch pointing at D
    git reset --hard C      # or HEAD^, HEAD~1, etc - resets develop back to C
    

    This will result in:

    A---B---C develop
             \
              D feature
    

    This will also work well if you’ve made more than one commit, as you can substitute whichever commit reference you need to reset develop back to in the second command.

    Assuming you already committed these changes locally and haven’t pushed yet, you can use a soft reset:

    git reset --soft HEAD~1
    git checkout feature
    git commit
    

    Just replace the 1 in git reset --soft HEAD~1 with the number of commits you want to soft reset.

    you can use git stash for putting the changes to a new branch, see git book

    So in master, after you made the changes (but didn’t add or commit them) you could do:

    git stash //stashes the changes
    git stash branch featurebranchname //creates a new branch and applies the stash to it
    

    You can cherry pick the changes to new branch.

    Git Baby is a git and github fan, let's start git clone.