To commit local changes to a new branch with git, we can use the following three steps:
Create a new branch
git switch -c new-branch
This will leave our current branch unedited, create a new branch called new-branch, and we still have our uncommitted changes. Itβs the parameter -c that tells git to create a new branch with a selected name.
We can use git branch -a
command to verify that the new Git branch to be pushed to the remote GitHub repo was indeed created locally.
Set upstream repos
git push -βset-upstream origin new-branch
With a new branch created, the -βset-upstream
switch must be run the first time a push is performed. This step tells the new branch which remote repository to use every time it synchronizes its commit history.
Add, Commit, and Push
git add .
git commit -m "First commit on new-branch"
git push origin
Once the remote GitHub repository is set as the upstream target for the new Git branch, push and pull operations can be performed normally with a simple git push origin
command.
Top comments (2)
Thanks for the brief explanation about git and commiting to a new branch leaving the original branch unedited, I was have troubles with that thank you π―
Great article! It is very clear and to the point, and it is a great reference for those who are starting to use Git.
Keep sharing!