DEV Community

Chen Zhang
Chen Zhang

Posted on • Updated on

Commit Changes to a New Branch with Git

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
_abdurrahmaan9 profile image
Abdur-rahmaan chimalo

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 πŸ’―

Collapse
 
mpfdev profile image
Matheus πŸ‡§πŸ‡·

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!