Git commit to new branch

To commit changes to a new branch in Git, you can follow these steps:

  1. Create a new branch:

    git branch <branch-name>

    Replace <branch-name> with the name you want to give your new branch.

  2. Checkout the new branch:

    git checkout <branch-name>

    This will switch your current branch to the new one.

  3. Commit your changes:

    git add.
    git commit -m "Initial commit"

    The git add. command stages all changes in your repository, and the git commit -m "Initial commit" command commits those changes with a meaningful commit message.

  4. Verify that you are on the new branch:

    git branch

    This will show you a list of all branches in your repository, including the new one.

  5. Push the new branch to a remote repository (optional):

    git push -u origin <branch-name>

    This will push the new branch to a remote repository (e.g. GitHub) and set the upstream tracking information.

Here's an example of the entire process:

$ git branch new-feature
$ git checkout new-feature
$ git add.
$ git commit -m "Initial commit"
$ git branch
  master
* new-feature
$ git push -u origin new-feature

Note: If you want to create a new branch from an existing branch (e.g. master), you can use the -b option with git checkout:

git checkout -b <branch-name> <existing-branch>

For example:

git checkout -b new-feature master

This will create a new branch new-feature based on the master branch.