Create a new branch and switch to it

Description: Create a branch named feature-login, move to it, add a file login.html and commit it. Then show all branches.

Hint:

  • git switch -c creates a branch and moves to it.

Solution

Terminal output

$ git switch -c feature-login
Switched to a new branch 'feature-login'
$ echo "login" > login.html
$ git add login.html
$ git commit -m "Add login page"
[feature-login 001dfd4] Add login page
1 file changed, 1 insertion(+)
create mode 100644 login.html
$ git branch
* feature-login
main

How it works

  • The new branch starts from the commit where you were, so it has all the old files.
  • The new commit belongs only to feature-login, and main is not changed.

Read Git Branch and Git Checkout and Switch.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.