Ultimate Guide to Advanced Git and GitHub: Overview of GitLab & Bitbucket
Last Updated :
01 Aug, 2024
In software development, mastering advanced tools and techniques is important for efficiency and collaboration. Git, GitHub, GitLab, and Bitbucket offer features beyond basic version control, like rebasing, cherry-picking, stashing, submodules, and Git hooks in Git, as well as GitHub Actions, GitLab CI/CD pipelines, and Bitbucket Pipelines.
This article will explore these advanced functionalities to help developers improve productivity and code quality.
Advanced Git Features
1. Rebasing
Rebasing is the process of moving or combining a sequence of commits to a new base commit. By default, a rebase will simply drop merge commits from the todo list, and put the rebased commits into a single, linear branch.
- Use cases: Clean up commit history, and integrate changes from one branch into another.
- Difference from merging: Rebasing rewrites commit history, while merging preserves it.
Syntax:
git checkout <feature-branch>
git rebase main
2. Cherry-Picking
Cherry-picking allows you to apply changes introduced by some existing commits.
- Scenarios: Apply a specific commit from one branch to another.
Syntax:
git cherry-pick <commit-hash>
3. Stashing
Stashing is useful when you want to save your work temporarily without committing it.
- Purpose: Save changes that you’re not ready to commit.
- Create a stash
git stash
- To list all stashed entries
git stash list
This will give an output that looks something like this
stash@{0}: WIP on my-branch: ca96af0 Commit message 3
stash@{1}: WIP on my-branch: 03af20c Commit message 2
stash@{2}: WIP on my-branch: 216b662 Commit message 1
- If we want to remove a stash entry from the list, we use pop. It will pop the last stash entry by default
git pop
- To pop an individual stash
git pop stash@{2}
4. Submodules
Submodules allow you to keep a Git repository as a subdirectory of another Git repository.
- Use cases: Manage dependencies.
Syntax:
git submodule add <repository-url>
.gitmodules: In this file we have to define:
- path to submodule folder
- url to submodule folder
- branch
[submodule “app/styles/core”]
path = app/styles/core
url = ../../repo/core.git
branch = master
5. Git Hooks
Git hooks are scripts that run automatically every time a particular event occurs in a Git repository. They let you customize Git’s internal behavior and trigger customizable actions at key points in the development life cycle.
Installing Hooks
Hooks reside in the .git/hooks directory of every Git repository. Git automatically populates this directory with example scripts when you initialize a repository. If you take a look inside .git/hooks, you’ll find the following files:
applypatch-msg.sample pre-push.sample
commit-msg.sample pre-rebase.sample
post-update.sample prepare-commit-msg.sample
pre-applypatch.sample update.sample
pre-commit.sample
To install a hook, all you have to do is remove the .sample extension.
As an example, try installing a simple prepare-commit-msg hook. Remove the .sample extension from this script, and add the following to the file:
#!/bin/sh
echo "# Please include a useful commit message!" > $1
Hooks need to be executable, so you may need to change the file permissions of the script if you’re creating it from scratch. For example, to make sure that prepare-commit-msg executable, you would run the following command:
chmod +x prepare-commit-msg
- Scope of hooks: Hooks are local to any given Git repository, and they are not copied over to the new repository when you run git clone. And, since hooks are local, they can be altered by anybody with access to the repository.
This has an important impact when configuring hooks for a team of developers. First, you need to find a way to make sure hooks stay up-to-date amongst your team members. Second, you can’t force developers to create commits that look a certain way—you can only encourage them to do so.
Advanced GitHub Features
1. GitHub Actions
GitHub Actions enable you to automate workflows directly in your GitHub repository by using CI/CD, automated testing etc.
Setting up workflows:
name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run a one-line script
run: echo Hello, world!
2. GitHub Packages
GitHub Packages provides hosting for software packages, making it easy to distribute them.
- How to use: Integrate with npm, Maven, Docker, etc.
- Benefits: Unified hosting with GitHub repositories.
3. GitHub Discussions
GitHub Discussions provides a space for conversations about your project.
- Use cases: Community engagement, Q&A.
- How to use: Enable discussion in your repository settings.
4. Project Management
GitHub offers robust project management tools.
- GitHub Projects: Kanban-style boards for tracking issues and pull requests.
- Milestones and Issues: Organise and prioritise tasks.
GitLab
GitLab is a complete DevOps platform, delivered as a single application.
- Key features: Built-in CI/CD, comprehensive security features.
- Comparison with GitHub: More features integrated into a single platform.
1. CI/CD Pipelines
GitLab CI/CD enables you to automatically build, test, and deploy your code.
- Setting up pipelines: Define in .gitlab-ci.yml.
stages:
- build
- test
- deploy
build:
stage: build
script:
- echo "Building..."
test:
stage: test
script:
- echo "Testing..."
deploy:
stage: deploy
script:
- echo "Deploying..."
2. GitLab Runner
GitLab Runner is an application that works with GitLab CI/CD to run jobs in a pipeline.
sudo gitlab-runner register
3. Security and Compliance
GitLab offers robust security features.
- Features: Static Application Security Testing (SAST), Dependency Scanning, Container Scanning.
- Compliance: Tools to manage compliance frameworks.
Bitbucket
Overview of Bitbucket
Bitbucket, part of the Atlassian suite, integrates seamlessly with Jira.
- Key features: Built-in CI/CD, excellent Jira integration.
- Comparison: Focus on team collaboration and project management.
1. Bitbucket Pipelines
Bitbucket Pipelines offers integrated CI/CD.
Setting up pipelines: Define in bitbucket-pipelines.yml.
pipelines:
default:
- step:
name: Build and Test
script:
- echo "Building and testing..."
2. Code Review and Collaboration
Bitbucket offers strong code review features.
- Pull requests: Inline comments, approvals.
- Jira integration: Link issues directly to pull requests.
Security Features
Bitbucket provides essential security features.
- Integration: Connect with various security tools.
Similar Reads
An Ultimate Guide to Git and Github
Highlights of the article: Introduction to GitGit Repository StructureGithubAccessing Github central repository via HTTPS or sshWorking with git - Important Git commandsIntroduction to Git For installation purposes on ubuntu, you can refer to this article: How to Install, Configure and Use GIT on Ub
8 min read
Take Advantages of Git and GitHub to Stay Motivated & Consistent While Coding
Coding can be a difficult and challenging task, but using tools like Git and GitHub can significantly enhance your motivation and consistency. These tools offer powerful features that help you manage your projects, collaborate with others, and maintain a steady workflow. Hereâs how you can use Git a
4 min read
The Ultimate Guide to Git Configurations.
Git is an important tool for developers, enabling efficient version control and collaboration on software projects. While most users are familiar with basic Git commands, mastering Git configurations can significantly enhance your workflow, and improve project management. In this article, we will ta
7 min read
Bitbucket vs GitHub vs GitLab
BitbucketBitbucket was launched in 2008 initially supporting Mercurial Projects. In 2010, it was acquired by Atlassian and from 2011 it also started to support Git hosting.It supports the Mercurial VCS(version control system) in addition to GitIt is not open source but by buying the self-hosted vers
4 min read
Mastering Git and GitHub: A Comprehensive Guide
Git and GitHub have become essential tools in the modern developerâs toolkit, facilitating version control, collaboration, and efficient project management. Whether you are a novice or an experienced developer, mastering these tools can significantly enhance your productivity and ability to work in
5 min read
First Open Source Contribution to GitHub - A Step By Step Guide
Contributing to open-source projects on GitHub is a rewarding experience that allows you to collaborate with developers worldwide, improve your skills, and make a positive impact on the tech community. This step-by-step guide will walk you through the process of making your first open source contrib
3 min read
How To Create A .gitlab-ci.yml File in GitLab?
GitLab CI/CD automates the process of building, testing, and deploying code, ensuring your software is always in a releasable state. At the heart of GitLab's CI/CD pipelines is the .gitlab-ci.yml file, which defines the stages, jobs, and commands that need to be executed in a CI/CD pipeline. In this
6 min read
GitHub App to Add or Remove Labels to Issues
Managing issues effectively is important for any project on GitHub. Labels are an important tool for categorizing and prioritizing issues, making it easier for teams to organize their workflows. To enhance this process, GitHub offers various apps that can help automate the addition and removal of la
3 min read
How to Build Portfolio Website And Host It on GitHub Pages?
Having an online portfolio is important for showcasing your skills and accomplishments. GitHub Pages provides a convenient and free platform for hosting your portfolio website. In this article, we will see how to build a Portfolio Website And Host It on GitHub Pages. Table of Content Creating a Port
15 min read
Creating New Branch on Github without using any IDE
GitHub is a powerful platform for version control and collaboration, widely used by developers around the world. Creating a new branch is a common task that allows you to work on different features or fixes separately without affecting the main codebase. In this guide, we will walk you through the p
2 min read