Last Updated : 01 Aug, 2024
Comments
Improve
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.
Table of Content
- Advanced Git Features
- 1. Rebasing
- 2. Cherry-Picking
- 3. Stashing
- 4. Submodules
- 5. Git Hooks
- Advanced GitHub Features
- 1. GitHub Actions
- 2. GitHub Packages
- 3. GitHub Discussions
- 4. Project Management
- GitLab
- Bitbucket
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 3stash@{1}: WIP on my-branch: 03af20c Commit message 2stash@{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/coreurl = ../../repo/core.gitbranch = 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.samplecommit-msg.sample pre-rebase.samplepost-update.sample prepare-commit-msg.samplepre-applypatch.sample update.samplepre-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/shecho "# 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.
- How to set up:
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.
Previous Article
How to Install, Configure and Use GIT on Ubuntu?
Next Article
How to Get Started with ml5.js?