How to switch Git branch

How to switch Git branch
  • Related posts
How to first commit on Git
I used to GitLab before, and now I have set up Gitea on my Synology NAS server. 💡 tip: This post is a simple guide on how to use Git. First of all, I want to manage to branch: * main: Operation * develop: Development when creating a repository for the first time:

Last time, I explained how to first commit on Git.

Welcome to the Git World

This time, I will explain How to switch a local repository Git branch.

I initially committed my code to the remote main branch, but the main branch is used for production.

So this time, I will create a new branch from remote main branch and switch local repository to that branch.

💡 tip: This post is a simple guide on how to use Git.

Web

  1. Click to "Branches" menu
  2. Click to "Create new branch from main" button
  3. Write name on "Branch Name" label
  4. Click to "Confirm" button

Local repository

  1. Move to project directory
  2. Open terminal
  3. Check to Local branch
$ git branch

Result:

* main
  1. Fetch the lastest changes from remote branch
$ git fetch origin

Result:

From https://YourGitURL
* [new branch]      develop    -> origin/develop
  1. Create a new local branch from remote branch and switch to it
$ git checkout -b develop origin/develop

Result:

branch 'develop' set up to track 'origin/develop'.
Switched to a new branch 'develop'
  1. Check again
$ git branch

Result:

* develop
  main