Open In App

How to Manage Branches in GitLab?

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

GitLab is a web-based platform (like GitHub) that provides services related to the version control system of a project. Branching is one such concept of the version control system.

Note: Make sure you have created a GitLab account and a repository.

These are the following approaches to Manage branches in GitLab:

Using the GUI method (GitLab website)

  • Open your repository and click on the branches tab as shown in the image below. Here we have created a sample repository using the default NodeJS template provided by GitLab. Create a new branch from the master branch of our repository.
step1gui
Creating a new branch
  • The test branch will be identical to our master branch. Add some changes to server.js file of test branch. Here we have changed the message from "Hello, World" to "Hello from GeeksForGeeks!". Commit the changes before exiting.
step2gui
Added changes to server.js of test branch
  • Now we can compare the differences between branches by click on compare button. Select the source branch as test and target branch as master. Note that you can only find the compare button in any branch you have created expect the master branch. We can also delete the branch we have created through the branches tab. However, we will try to merge this test branch with our master branch first. We can also see the compare button here in the drop-down menu along with the delete button.
step4gui
Option to delete branch
  • In order to merge the branch, go to the compare tab (as shown in 3rd step). There you can find the option to create merge requests. There is also a tick-box to delete the test branch if the merge request is accepted.
  • You can see the merge requests in the merge requests tab. Check the changes before accepting it. You can also select whether the branch which is to be merged should be deleted or kept if the request is accepted.
  • After merging the branches, the changes made to test branch will be followed to the master branch automatically. If we want to roll back the changes made due to merging the branches, we can create a new merge request to revert back the changes made earlier by the particular merge action.

Using Git (command line method)

  • In order to open the repository in your local machine, you must clone it first. Make sure to change the directories accordingly where you want to store the project. Find the URL of your repository in the homepage. Clone the repository using the command:
git clone <url>
  • In order to create a branch, enter into your project folder and type the following command in your terminal and press enter:
git branch <branch-name>
  • For making changes into the server.js file of test branch, we must first switch to the test branch, change the branch by using the command:
git checkout <branch-name>
  • Make the changes to server.js file using the command of your IDE or text editor and the file name. Here we are using simple notepad which comes pre-installed in windows 10.
notepad server.js
  • Push the changes (creating a new branch and making changes to server.js of test branch) using the commands:
git add .
git commit -m "commit-message"
git push -u origin <branch-name>
step5cli
Pushing changes to remote repository
  • The remote repository has been updated with new branch named test having a slightly different server.js file. In order to merge master and test branches run the commands which will navigate to target branch (master), merge the branches and push the changes:
  • If you want to delete the new branch after it is merged, then you can use the following command while pushing the changes to remote repository:
git push origin --delete <branch-name>



Next Article
Article Tags :

Similar Reads