Skip to main content
Chadura
Django 18 min read •

Mastering GitLab: Key Commands for Efficient Workflow

Part II Git-Lab - Commands

Sridhar S
Sridhar S
Cloud Admin
Mastering GitLab: Key Commands for Efficient Workflow
01

A Simple Guide to Essential Git Commands with Examples

Part II | Part I

git init

Initializes a new Git repository in your project folder. This creates a hidden .git directory where Git stores all its tracking data.

Bash / Terminal
git init

This command turns the current directory into a Git repository, allowing you to start tracking changes.

git clone

Creates a copy of a remote repository on your local machine. This is useful when you want to work on an existing project.

Bash / Terminal
 git clone https://github.com/username/repo.git

This command downloads the repository from the provided URL and creates a local copy.

git add

Stages changes in your working directory for the next commit. You can stage specific files or all changes at once.

Bash / Terminal
git add file.txt          # Stages a single file

git add .                # Stages all changes in the directory

This prepares your changes to be saved in the next commit.

git commit

Saves the staged changes to the repository with a message describing what was changed.

Bash / Terminal
 git commit -m "Added new feature"

This command creates a new commit with the message "Added new feature".

git status

Shows the current state of your working directory, including staged, unstaged, and untracked files.

Bash / Terminal
git status

This command helps you see which files are ready to be committed and which are not.

git push

Uploads your local commits to a remote repository. This is how you share your changes with others.

Bash / Terminal
git push origin main

This command pushes your local main branch to the main branch on the remote repository named

git pull

Fetches changes from a remote repository and merges them into your local branch.

Bash / Terminal
git pull origin main

This command updates your local main branch with the latest changes from the remote repository.

git branch

Lists, creates, or deletes branches. Branches allow you to work on different features or fixes in isolation.

Bash / Terminal
git branch feature-branch  # Creates a new branch
Bash / Terminal
git branch -d feature-branch # Deletes a branch

The first command creates a new branch, and the second deletes it.

git checkout

Switches between branches or restores files from a specific commit.

Bash / Terminal
git checkout feature-branch  # Switches to the feature-branch
Bash / Terminal
git checkout -b new-branch   # Creates and switches to a new branch

This command helps you navigate between branches or create new ones.

git merge

Combines changes from one branch into another. This is often used to integrate feature branches into the main branch.

Bash / Terminal
git checkout main
Bash / Terminal
git merge feature-branch

These commands switch to the main branch and merge the changes from Feature-branch

git log

Displays the commit history of the repository, including commit messages, authors, and dates.

Bash / Terminal
git log

This command shows a detailed history of all commits in the current branch.

git reset

Undoes changes by moving the HEAD pointer or upstaging files.

Bash / Terminal
 git reset HEAD file.txt  # Unstages a file
Bash / Terminal
git reset --hard HEAD~1 # Resets the working directory to the previous commit

The first command upstages a file, and the second discards all changes since the last commit.

git stash

Temporarily saves changes that you don’t want to commit yet. This is useful when switching branches.

Bash / Terminal
git stash        # Saves changes temporarily
Bash / Terminal
git stash apply  # Reapplies the stashed changes

This allows you to save your work without committing it.

git remote

Manages connections to remote repositories. You can add, rename, or remove remotes.

Bash / Terminal
git remote add origin https://github.com/username/repo.git
Bash / Terminal
git remote -v

The first command adds a remote repository, and the second lists all remotes.

Bash / Terminal
git fetch

Downloads changes from a remote repository without merging them into your local branch.

Bash / Terminal
git fetch origin

This command retrieves updates from the remote repository but leaves your working directory unchanged.

02

Conclusion

These Git commands are the foundation of version control and collaboration in software development. By mastering these basics, you'll be able to track changes, collaborate with others, and manage your code effectively. Practice these commands in your projects, and you'll soon become a Git pro!

Sridhar S

Sridhar S

Cloud Admin

Cloud Admin - Chadura Tech Pvt Ltd, Bengaluru

Related Technical Articles

Explore additional architectural breakdowns in Django.

M

Maya Solutions Architect

Ask about products & scoping →

WhatsApp Direct Help