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?

  • Why is 'Updating the Git index failed' displayed
  • View commits that make changes to subfolder
  • IntelliJ error: branch 'origin/HEAD' does not point at a commit, some refs could not be read
  • How to get rid of some changeset in Hg?
  • Working with zend framework and git in large projects
  • unable to click always allow on git-credential-osxkeychain popup
  • Do I commit the package-lock.json file created by npm 5?
  • GitHub: can I offer access to compiled files without versioning them?
  • is there a way to find out where a branch came from?
  • ANSI color in git is not displayed correctly
  • GCommit doesn't work with fugitive.vim under windows xp?
  • Microsoft Git Provider and Visual Studio 2012 failed to start the configured compare tool
  • 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.