Git commit to new branch
To commit changes to a new branch in Git, you can follow these steps:
-
Create a new branch:
git branch <branch-name>
Replace
<branch-name>
with the name you want to give your new branch. -
Checkout the new branch:
git checkout <branch-name>
This will switch your current branch to the new one.
-
Commit your changes:
git add. git commit -m "Initial commit"
The
git add.
command stages all changes in your repository, and thegit commit -m "Initial commit"
command commits those changes with a meaningful commit message. -
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.
-
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.