From master, commit to another branch

I usually create branches for the different segments of code but once in a while I forget to create a new branch and everything is happening on the master branch.

How can I say to git that although I am on the master branch, this commit is for the xyz branch?

  • Custom git diff to ignore some conflicts
  • Git: Storing but not pulling LESS files
  • Add an xcode project in a repository tfs
  • PhpStorm -> BitBucket Server integration
  • Can add Gemfile and Gemfile.lock to git
  • unset export-ignore for selected files in git
  • How can I preview many git -repos easily?
  • So which is better, using git-p4 or just sliding a .git directory into the working directory and having perforce ignore it?
  • Change GIT repository to shared
  • git - removing rails development log
  • GIT merge / rebase upstream without fetching the history (Laravel 4 )
  • git added by them renaming conflict - git recognises the same files in the renamed directory as new files
  • 2 Solutions collect form web for “From master, commit to another branch”

    Just checkout that branch first and commit to it

    git checkout -b mynewbranch
    # `git add` what you need
    git commit -m "my commit message"
    

    If you already committed on master one which should go on another branch, you can;

    # create a branch on that commit
    git branch mynewbranch
    # reset master to the previous commit
    # git reset --hard HEAD~
    

    (Make sure you don’t have any private file not added yet to the index, or the reset --hard would erase them: you can use git stash to save them temporarily)

    Then you can switch on mynewbranch is you have other commits to do on that new branch:

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