GitLab - Create a Branch
GitLab - Creating and Managing Branches
Section titled “GitLab - Creating and Managing Branches”What is a Branch?
Section titled “What is a Branch?”In Git, a branch is an independent line of development. Think of it as a separate workspace where you can work on a new feature, a bug fix, or an experiment without affecting the main codebase (often called ‘main’ or ‘master’). Once your work on a branch is complete and tested, it can be merged back into the main branch.
Using branches is fundamental to modern software development because it allows for:
- Parallel Development: Multiple developers can work on different features simultaneously.
- Isolation: Changes in one branch do not affect others until explicitly merged.
- Experimentation: Safely try out new ideas without risking the stability of the main codebase.
- Organized Workflow: Feature branches, bugfix branches, and release branches help organize the development process.
Creating a Branch using the Command Line (Recommended)
Section titled “Creating a Branch using the Command Line (Recommended)”The most common way to create branches is through the Git command-line interface (CLI).
Step 1: Navigate to your local project directory.
$ cd /path/to/your/projectStep 2: Ensure your current branch (e.g., main) is up-to-date with the remote repository.
$ git checkout main$ git pull origin mainStep 3: Create and switch to a new branch. The git checkout -b <branch-name> command creates a new branch from your current branch and immediately switches to it.
$ git checkout -b feature/new-login-pageSwitched to a new branch 'feature/new-login-page'Alternatively, using the git switch -c <branch-name> command (available in newer Git versions):
$ git switch -c feature/new-login-pageSwitched to a new branch 'feature/new-login-page'Best Practice for Naming Branches: Use descriptive names. Common conventions include prefixes like feature/, bugfix/, hotfix/, release/, followed by a short, descriptive name (e.g., feature/user-authentication, bugfix/issue-123).
Step 4: Push the new branch to GitLab. To make your branch visible to others and available on GitLab, you need to push it to the remote repository (usually named ‘origin’).
$ git push -u origin feature/new-login-pageThe -u flag (or --set-upstream) sets up a tracking relationship between your local branch and the remote branch. This allows you to use git pull and git push without specifying the remote and branch name in the future for this branch.
Creating a Branch via the GitLab UI
Section titled “Creating a Branch via the GitLab UI”You can also create branches directly from the GitLab web interface.
Step 1: Log in to your GitLab account and navigate to your project.
Step 2: In the left sidebar, go to ‘Repository’ and then select ‘Branches’.
A textual representation of this navigation might be: Your Project > Repository > Branches.
Step 3: On the ‘Branches’ page, click the ‘New branch’ button. This button is usually located in the top-right corner of the page.
Step 4: In the ‘New branch’ form:
- Enter a ‘Branch name’ (e.g.,
feature/documentation-update). - Choose the ‘Create from’ source. This is the existing branch, tag, or commit from which your new branch will be created. Typically, you’ll create it from
mainor another feature branch.
Step 5: Click the ‘Create branch’ button.
After creation, you’ll be taken to the page for your new branch within GitLab. To work on this branch locally, you would typically run:
$ git fetch origin$ git checkout feature/documentation-updateListing Branches
Section titled “Listing Branches”To see all local branches:
$ git branchTo see all remote branches:
$ git branch -rTo see all local and remote branches:
$ git branch -aSwitching Branches
Section titled “Switching Branches”To switch to an existing local branch:
$ git checkout <branch-name># Or with newer Git versions:$ git switch <branch-name>Deleting Branches
Section titled “Deleting Branches”To delete a local branch (after it’s been merged):
$ git branch -d <branch-name>To delete a remote branch on GitLab:
$ git push origin --delete <branch-name>Branches are a core part of the Git workflow, and GitLab provides excellent tools for managing them both via its UI and in conjunction with Git CLI commands.