Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • jQuery
  • AngularJS
  • ReactJS
  • Next.js
  • React Native
  • NodeJS
  • Express.js
  • MongoDB
  • MERN Stack
  • PHP
  • WordPress
  • Bootstrap
  • Tailwind
  • CSS Frameworks
  • JS Frameworks
  • Web Development
Open In App
Next Article:
How To Create And Commit A Branch in GitLab?
Next article icon

How To Create And Commit A Branch in GitLab?

Last Updated : 03 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

GitLab is a popular Git repository hosting service with built-in tools for Continuous Integration/Continuous Deployment (CI/CD), issue tracking, and more. Branching is a fundamental aspect of Git workflow, allowing developers to isolate features, bugs, or experiments from the main codebase. This guide will walk you through creating and committing changes to a branch in GitLab.

Steps To Create And Commit A Branch in GitLab

Step 1: Clone the Repository

To work with a GitLab repository locally, you first need to clone it onto your machine.

git clone <repository_url>

For example:

git clone https://gitlab.com/your-username/your-repository.git
dfgg
Clone git repo

This will create a local copy of the repository in your working directory.

Step 2: Create a New Branch

Once you have the repository cloned, navigate to the repository’s folder and create a new branch using the git checkout command:

git checkout -b <new-branch-name>

For example, to create a branch called feature/add-login:

git checkout -b feature/add-login

This command creates a new branch named feature/add-login and switches you to that branch.

Step 3: Make Changes in the New Branch

Now that you're on the new branch, make the necessary changes to your files. You can edit, add, or delete files in the repository.

For example, let’s say you edited a file called index.js to add some new functionality.

Step 4: Stage and Commit Changes

Once you've made changes, you need to stage the changes before committing them. Staging tells Git which files you want to include in your next commit.

To stage changes:

git add <file-name>

For example, to stage the index.js file:

git add index.js

If you want to stage all changed files, use:

git add .

After staging the changes, commit them with a descriptive message:

git commit -m "Added login feature"

Step 5: Push the Branch to GitLab

After committing your changes locally, you need to push the new branch to the remote GitLab repository. You can do this using the git push command:

git push origin <new-branch-name>

For example:

git push origin feature/add-login

This command pushes your local branch feature/add-login to the GitLab repository.

Step 6: Create a Merge Request (Optional)

In GitLab, after pushing your branch, it’s common to create a Merge Request (MR) to merge the branch back into the main branch (usually main or master). This allows for code review and CI/CD checks.

  • Go to your GitLab repository in your web browser.
  • GitLab should automatically detect that you’ve pushed a new branch and will suggest opening a Merge Request. Click the button to create the MR.
  • Fill in the details for your Merge Request, including a title and description of the changes.
  • Submit the Merge Request for review.

Best Practices for GitLab Branching

  • Use Descriptive Branch Names: Branch names should clearly describe the work being done. Examples: feature/add-login, bugfix/fix-user-auth, hotfix/update-logo.
  • Keep Branches Focused: A branch should ideally focus on a single task, feature, or bug to simplify the review and merging process.
  • Regular Commits: Make small, regular commits with clear messages. This makes it easier to track changes and troubleshoot any issues.

Next Article
How To Create And Commit A Branch in GitLab?

S

swati4934
Improve
Article Tags :
  • Web Technologies
  • Git

Similar Reads

    How to Create a New Branch in Git and Push the Code?
    Branching in Git is a helpful feature for software developers working on a big team project. It allows the team members to work on different aspects of the software by creating a branch from the main branch. The main branch is not affected by the changes in the created branch until it is merged into
    8 min read
    How To Create Branch In Git?
    Creating branches in Git is a fundamental skill for any developer. Branches allow you to work on different features, bug fixes, or experiments in isolation from the main codebase. This way, you can keep your main branch stable while making changes on separate branches. In this article, we’ll guide y
    2 min read
    How to Create a New Branch in Git?
    Git is a powerful and widely used version control system that helps developers manage code changes across projects efficiently. One of the fundamental features of Git is branching, which allows developers to diverge from the main line of development and work on different tasks or features independen
    4 min read
    How to Delete Branch in Gitlab?
    When working with Git, it's common to create branches to isolate changes and work on new features or fixes. However, once a branch has served its purpose, it's often necessary to delete it to keep your repository clean and organized. In this article, we'll see the process of deleting a Git branch us
    2 min read
    How to Create a Branch In Git from Another Branch?
    Branching is a fundamental aspect of version control systems like Git, which helps developers work on multiple features or bug fixes simultaneously without interfering with the main codebase. Creating a branch from another branch is a common practice, especially when you want to build upon existing
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences