GitLab Commands
1. Git Configuration
Before using Git, configure your username and email.
bash
git config --global user.name "Your Name"
git config --global user.email "
[email protected]"
Sets user information for Git commits.
2. Working with GitLab Repositories
Clone a Repository
bash
git clone <repo_url>
Copies the remote repository to your local machine.
Initialize a New Repository
Bash
git init
Creates a new local Git repository.
3. Branching and Merging
Create a New Branch
bash
git checkout -b <branch_name>
Creates and switches to a new branch.
Switch to Another Branch
bash
git checkout <branch_name>
Switches to an existing branch.
View All Branches
bash
git branch
Lists all branches in the repository.
Push a New Branch to GitLab
bash
git push -u origin <branch_name>
Uploads the branch to GitLab.
Merge a Branch into Main
bash
git checkout main
git merge <branch_name>
Merges the branch into the main branch.
4. Staging and Committing Changes
Check Status of Changes
bash
git status
Shows modified and untracked files.
Add Files to Staging Area
bash
git add <file_name>
git add .
Stages files for commit (. stages all files).
Commit Changes
bash
git commit -m "Your commit message"
Saves changes with a commit message.
View Commit History
bash
git log --oneline --graph --decorate --all
Shows commit history in a graphical format.
5. Pushing and Pulling Changes
Push Local Changes to GitLab
bash
git push origin <branch_name>
Uploads commits to GitLab.
Pull Latest Changes from GitLab
bash
git pull origin <branch_name>
Fetches and merges changes from GitLab.
Fetch Remote Changes Without Merging
bash
git fetch origin
Retrieves updates but does not apply them.
6. Handling Merge Conflicts
Check for Conflicts
bash
git status
Shows conflicting files.
Resolve Conflicts Manually
1. Open conflicting files, edit and fix conflicts.
2. Add the resolved files back:
bash
git add <resolved_file>
3. Commit the resolution:
Bash
4. git commit -m "Resolved merge conflict"
5. Push the changes:
bash
git push origin <branch_name>
7. Undoing Changes
Undo Last Commit (Keep Changes in Staging)
bash
git reset --soft HEAD~1
Reverts last commit but keeps changes.
Undo Last Commit (Remove Changes Completely)
bash
git reset --hard HEAD~1
Deletes last commit and all changes.
Discard Unstaged Changes
bash
git checkout -- <file_name>
Reverts file to the last committed version.
Remove a File from Staging
bash
git reset HEAD <file_name>
Unstages a file without deleting changes.
8. GitLab CI/CD Commands
Trigger a Pipeline Manually
bash
curl --request POST --form "token=<trigger_token>" --form ref=<branch_name>
https://gitlab.com/api/v4/projects/<project_id>/trigger/pipeline
Starts a GitLab CI/CD pipeline.
Check Pipeline Status
bash
curl --header "PRIVATE-TOKEN: <your_access_token>"
"https://gitlab.com/api/v4/projects/<project_id>/pipelines"
Lists all pipelines and their statuses.
9. GitLab Runner Commands
Register a GitLab Runner
bash
gitlab-runner register
Registers a runner to execute CI/CD jobs.
Start the GitLab Runner
bash
gitlab-runner start
Starts the GitLab runner.
Stop the GitLab Runner
bash
gitlab-runner stop
Stops the GitLab runner.
Check the Runner Status
bash
gitlab-runner verify
Verifies if the runner is connected and active.
10. GitLab Access & Authentication
Login to GitLab via SSH
bash
ssh -T [email protected]
Tests SSH authentication with GitLab.
Set Up SSH Key for GitLab
1. Generate SSH Key:
bash
2. Copy the key:
bash
cat ~/.ssh/id_rsa.pub
3. Add it to GitLab under Settings → SSH Keys.
11. Git Tags (Versioning)
Create a Tag
bash
git tag -a v1.0 -m "Release version 1.0"
Creates an annotated tag for versioning.
Push a Tag to GitLab
bash
git push origin v1.0
Uploads the tag to GitLab.
List All Tags
bash
git tag
Displays all tags in the repository.
Delete a Tag Locally and Remotely
bash
git tag -d v1.0
git push origin --delete v1.0
Removes a tag from the local and remote repository.
12. GitLab Webhooks & API Commands
Create a Webhook
1. Go to Project → Settings → Webhooks.
2. Enter the URL and select triggers (Push, Merge, etc.).
3. Click Add Webhook.
Check GitLab API Version
bash
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.com/api/v4/version"
Returns the GitLab API version.
List GitLab Project Details via API
bash
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.com/api/v4/projects"
Retrieves project information from GitLab.
13. GitLab Permissions Management
Add a User to a Project
1. Go to Project → Settings → Members.
2. Enter the user’s GitLab username/email.
3. Assign a role:
o Guest (view-only)
o Reporter (view + reports)
o Developer (can push code)
o Maintainer (can merge)
o Owner (full control)