How to create new local branch in Git?

Viewed 12036

I am working on a project which I cloned from a remote repo hosted on Gitlab. I made some changes to the project but didnt create any branch and want to now start work on some other new features, but without first pushing my existing work to the remote repo. I might discard the changes in the new feature or might need to push both the new feature as well as the earlier changes to the remote repo, at a later stage.

From what I know about Git, I think I need to create a new local branch, which I can do using git checkout -b NEW_BRANCH_NAME. Is this the correct way to accomplish what I am trying to do? When this command, creates a new branch, how do I switch back and forth between working on this new branch and the earlier one?

1 Answers

you switch back and forth between branches using git checkout <branch name>. and yes git checkout -b NEW_BRANCH_NAME is the correct way to create a new branch and switching to it at the same time the command you used is a shorthand to git branch <branch name> and git checkout <branch name>.

Related