How to push to new branch
To push your local branch to a new branch on a remote repository, you can use the following Git commands:
-
Create a new branch:
git branch <new-branch-name>
Replace
<new-branch-name>
with the name you want to give to your new branch. -
Switch to the new branch:
git checkout <new-branch-name>
This will switch your local repository to the new branch.
-
Add and commit your changes:
git add. git commit -m "Initial commit on new branch"
This will stage all changes in your local repository and commit them with a meaningful commit message.
-
Push the new branch to the remote repository:
git push -u origin <new-branch-name>
Replace
origin
with the name of your remote repository (usuallyorigin
orupstream
).
The -u
flag sets the upstream tracking information for the branch, which allows you to use git pull
and git push
without specifying the remote branch.
For example, if you want to create a new branch called feature/new-login-system
, you would run the following commands:
git branch feature/new-login-system
git checkout feature/new-login-system
git add.
git commit -m "Initial commit on new branch"
git push -u origin feature/new-login-system
This will create a new branch called feature/new-login-system
on your remote repository, and set the upstream tracking information for that branch.
Note: If you want to push an existing branch to a new branch on the remote repository, you can use the following command:
git push origin <local-branch-name>:<new-branch-name>
For example:
git push origin master:feature/new-login-system
This will push the changes from your local master
branch to a new branch called feature/new-login-system
on the remote repository.