GitLab Git Commands

Git runs on your computer. GitLab hosts the shared copy of your project. Git commands move changes between the two. A small set of commands covers nearly everything a developer does each day.

Think of packing a parcel. You gather items on a table, place them in a box, seal and label the box, and hand it to a courier. Git follows the same path, and each step has its own command.

Four Places Your Code Lives

  Working Folder ──git add──► Staging Area ──git commit──► Local Repository ──git push──► GitLab
  (items on table)          (items in box)               (sealed, labeled box)          (delivered)

  GitLab ──git pull / git fetch──► Local Repository ──► Working Folder

One-Time Setup

Git stamps every commit with your name and email. Set both once per computer.

git config --global user.name "Riya Sharma"
git config --global user.email "riya@example.com"
git config --global init.defaultBranch main
git config --list

Get a Project onto Your Computer

The clone command downloads the full project and its history into a new folder.

git clone git@gitlab.com:my-group/my-project.git
cd my-project

Check What Changed

git status

This command lists changed files and shows which ones sit in the staging area.

On branch feature/login
Changes not staged for commit:
        modified:   login.html

Untracked files:
        notes.txt

git diff

The diff command prints the exact lines you added or removed. Lines with a plus sign are new. Lines with a minus sign are gone.

git diff

git log

The log command lists past commits. The short form fits on one screen.

git log --oneline
a1b2c3d Add login form
9f8e7d6 Create project layout

Save Your Work

Staging picks which changes join the next commit. A commit records those changes with a message.

git add login.html
git commit -m "Add email field to login form"

Use git add . to stage every changed file in the current folder.

Write Useful Commit Messages

  • Start with a verb such as Add, Fix, or Remove.
  • Keep the first line under 70 characters.
  • Describe what changed and why it matters.
  • Commit one idea at a time.

Share Your Work

The push command uploads local commits to GitLab. The first push of a new branch needs the -u flag, which links your local branch to the remote one.

git push -u origin feature/login

Later pushes need only a short command.

git push

Download Teammate Changes

CommandWhat It DoesChanges Your Files?
git fetchDownloads new commits and leaves your files aloneNo
git pullDownloads new commits and merges them into your branchYes

Run fetch when you want to look before you merge. Run pull when you trust the incoming changes.

Undo Mistakes Safely

GoalCommand
Discard edits in a filegit restore login.html
Remove a file from staginggit restore --staged login.html
Fix the last commit message before pushinggit commit --amend -m "New message"
Undo a pushed commit with a new commitgit revert a1b2c3d

Revert creates a fresh commit that cancels an older one. Teammates keep a clean history, and nobody loses work.

A Complete Daily Session

git pull                              # get the latest changes
git switch -c fix/button-color        # start a new branch
# edit files in your editor
git status                            # review what changed
git add style.css
git commit -m "Fix button color on login page"
git push -u origin fix/button-color   # upload the branch

After the push, GitLab prints a link that opens a merge request for the branch.

Quick Reference

CommandPurpose
git cloneCopy a project to your computer
git statusSee changed files
git addStage changes
git commitSave staged changes
git pushUpload commits
git pullDownload and merge commits
git switchMove between branches
git logView history