Ultimate Guide to Advanced Git and GitHub: Overview of GitLab & Bitbucket

Debojit SahaDebojit Saha
7 min read

Introduction

In software development, mastering advanced tools and techniques is crucial 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 blog explores these advanced functionalities to help developers improve productivity and code quality.

1. Advanced Git Features

Git Push - Export commits to remote repository - Git - dyclassroom | Have  fun learning :-)

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, integrate changes from one branch into another.

  • Difference from merging: Rebasing rewrites commit history, while merging preserves it.

A diagram showing that before rebase, branch commits stick out from the main line of development.

  • How to perform a rebase:

      git checkout <feature-branch>
      git rebase main
    

A diagram showing that after rebase, commits over multiple branches get flattened into a single line.

Cherry-Picking

Cherry-picking allows you to apply changes introduced by some existing commits.

  • Scenarios: Apply a specific commit from one branch to another.

  • How to cherry-pick a commit:

      git cherry-pick <commit-hash>
    

Stashing

Stashing is useful when you want to save your work temporarily without committing it.

A stash entry added to the stash list

  • Purpose: Save changes that you’re not ready to commit.

  • Create a stash

git stash
  • To list all the stash 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}

Submodules

Submodules allow you to keep a Git repository as a subdirectory of another Git repository.

  • Use cases: Manage dependencies.

  • How to add and manage submodules:

      git submodule add <repository-url>
    

    .gitmodules: In this file we have to define:

    • path to submodule folder

    • url to submodule repository

    • branch

[submodule “app/styles/core”]
path = app/styles/core
url = ../../repo/core.git
branch = master

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.

Hooks executing during the commit creation process

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.

Maintaining hooks for a team of developers can be a little tricky because the .git/hooks directory isn’t cloned with the rest of your project, nor is it under version control. A simple solution to both of these problems is to store your hooks in the actual project directory (above the .git directory). This lets you edit them like any other version-controlled file. To install the hook, you can either create a symlink to it in .git/hooks, or you can simply copy and paste it into the .git/hooks directory whenever the hook is updated.

Hooks executing during the commit creation process

2. Advanced GitHub Features

765900ba9-article-200807-github-gitguardbody-text.jpg

GitHub Actions

GitHub Actions enable you to automate workflows directly in your GitHub repository.

  • Benefits: CI/CD, automated testing.

  • 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!
    

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.

Security Features

  • Dependabot: Automates dependency updates.

  • Code scanning: Identifies vulnerabilities in your code.

  • Secret scanning: Detects secrets in your codebase.

GitHub Discussions

GitHub Discussions provides a space for conversations about your project.

  • Use cases: Community engagement, Q&A.

  • How to use: Enable discussions in your repository settings.

discussions2

Project Management

GitHub offers robust project management tools.

  • GitHub Projects: Kanban-style boards for tracking issues and pull requests.

  • Milestones and Issues: Organize and prioritize tasks.

Github Project Management Guide | Jobsity

3. GitLab

Exploring GitLab CI/CD: A Journey of Learning and Practical Insights |  Medium

Overview of 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.

CI/CD Pipelines

Dependencies between jobs

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..."
    

    GitLab Runner

    GitLab Runner is an application that works with GitLab CI/CD to run jobs in a pipeline.

    • How to set up:

        sudo gitlab-runner register
      

      Gitlab runner execution flow

      Security and Compliance

      GitLab offers robust security features.

      • Features: Static Application Security Testing (SAST), Dependency Scanning, Container Scanning.

      • Compliance: Tools to manage compliance frameworks.

4. Bitbucket

Repository Management in 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.

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..."
    

    Bitbucket Pipelines - Just a Series of Tubes

    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.

Conclusion

Mastering advanced Git and GitHub features can significantly enhance your development workflow, making it more efficient and streamlined. From rebasing and cherry-picking to leveraging GitHub Actions and GitLab CI/CD pipelines, these tools offer powerful capabilities for version control, automation, and collaboration. Understanding and utilizing these features will not only improve your code management but also foster better team collaboration and project management. Whether you are working on a solo project or part of a large team, these advanced techniques and tools are essential for modern software development.

13
Subscribe to my newsletter

Read articles from Debojit Saha directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Debojit Saha
Debojit Saha