Skip to content

GitLab - Git Commands

While GitLab provides a web interface for many operations, Git itself is a command-line tool. Understanding basic Git commands is crucial for effective version control and collaboration using GitLab. These commands are run in your terminal or command prompt in your local project repository.

Before you start using Git, configure your user name and email address. This information will be associated with your commits.

$ git config --global user.name "Your Name"
$ git config --global user.email "youremail@example.com"

To check your configuration:

$ git config --global --list

Check Git version:

$ git --version

Clone an existing repository from GitLab:

$ git clone <repository_url>

Initialize a new Git repository in an existing directory:

$ git init

List all local branches (current branch is marked with an asterisk):

$ git branch

Create a new branch:

$ git branch <new-branch-name>

Switch to a branch (modern Git recommends git switch):

$ git switch <branch-name>
# Older command:
# $ git checkout <branch-name>

Create a new branch and switch to it immediately:

$ git switch -c <new-branch-name>
# Older command:
# $ git checkout -b <new-branch-name>

Merge changes from another branch into your current branch (e.g., merging feature-branch into main):

$ git switch main
$ git merge feature-branch

Delete a local branch (safely, only if merged):

$ git branch -d <branch-name>

Force delete a local branch (use with caution):

$ git branch -D <branch-name>

Check the status of your working directory and staging area:

$ git status

Add file changes to the staging area:

$ git add <file-name> # Add a specific file
$ git add . # Add all changes in the current directory and subdirectories

Commit staged changes to your local repository:

$ git commit -m "Your descriptive commit message"

View commit history:

$ git log
$ git log --oneline --graph --decorate # A more concise view

Synchronizing with GitLab (Remote Repository)

Section titled “Synchronizing with GitLab (Remote Repository)”

Note: origin is the default name for your remote repository. The primary branch is often named main (modern) or master (older).

Fetch changes from the remote repository (doesn’t merge):

$ git fetch origin

Pull changes from the remote repository (fetches and merges/rebases):

$ git pull origin <branch-name>
# Example: git pull origin main

If your local branch is tracking a remote branch, you can often just use:

$ git pull

Push your local commits to the remote repository:

$ git push origin <branch-name>
# Example: git push origin feature-branch

To set the upstream branch for the first push (so future git push and git pull can be run without arguments):

$ git push -u origin <branch-name>

Discard changes in a specific file in your working directory (modern Git recommends git restore):

$ git restore <file-name>
# Older command:
# $ git checkout -- <file-name>

Unstage a file (remove from staging area but keep changes):

$ git restore --staged <file-name>
# Older command:
# $ git reset HEAD <file-name>

Amend the last commit (change message or add more changes):

$ git commit --amend

Caution: Avoid amending commits that have already been pushed to a shared remote branch.

Revert a commit (creates a new commit that undoes the changes of a previous commit):

$ git revert <commit-hash>

Reset your current branch to a previous commit (can lose data, use with care):

$ git reset --hard <commit-hash> # Discards all changes since that commit
$ git reset --soft <commit-hash> # Keeps changes staged

Remove untracked files from your working directory (DANGEROUS - cannot be undone):

$ git clean -f # Remove untracked files
$ git clean -fd # Remove untracked files and directories

This list covers many common Git commands, but Git is a powerful tool with many more features. For further learning, refer to the official Git documentation (git-scm.com) or interactive tutorials available online.