Linux

Essential Git Commands for Linux: A Beginner's Cheatsheet

This guide introduces you to essential Git commands for version control in Linux. You'll learn to initialize repositories, make commits, work with branches, and sync changes with remote servers.

Updated at February 16, 2026
15-30 min
Easy
FixPedia Team
Применимо к:Git 2.30+Ubuntu 20.04+Debian 11+Fedora 35+Any Linux with bash

Introduction / Why This Is Needed

Git is a distributed version control system that has become the standard in software development. It allows you to track changes in code, revert to previous versions, work in parallel on different tasks in separate branches, and collaborate effectively within a team. Understanding basic Git commands is an essential skill for any developer, DevOps engineer, or technical professional working with code. This cheat sheet will give you a practical foundation for everyday work with Git in the Linux terminal.

Prerequisites / Preparation

Before you begin, ensure you have:

  • Git installed. Minimum version 2.30.
  • Terminal access (bash, zsh).
  • An account on a platform with a remote repository (GitHub, GitLab, Bitbucket), if you plan to work with remote repositories.
  • Basic skills in Linux command line work (directory navigation cd, file creation/deletion touch, rm).

Step 1: Installing Git

If Git is not already installed, run the command for your distribution.

For Ubuntu/Debian:

sudo apt update
sudo apt install git

For Fedora/RHEL/CentOS:

sudo dnf install git

For Arch Linux:

sudo pacman -S git

After installation, check the version:

git --version

The output should look something like: git version 2.43.0.

Step 2: Configuring the User

Git needs to know who is making commits. Configure your name and email once for the entire system.

git config --global user.name "Your Full Name"
git config --global user.email "your.email@example.com"

This data will be used by default in all your repositories. To verify the settings, run git config --list.

Step 3: Cloning an Existing Repository

To start working with a project that already exists on a remote server, clone it. This will create a complete local copy.

git clone <repository_URL>

For example:

git clone https://github.com/username/project-name.git

The command will create a project-name directory and copy all files and commit history there. Navigate into it: cd project-name.

Step 4: Adding Files and Creating a Commit

Local changes go through two stages: staging (indexing) and commit.

  1. Check the repository status to see modified and new files:
    git status
    
  2. Add specific files to the staging area to prepare them for a commit:
    git add filename.txt
    

    Or add all modified and new files (except ignored ones):
    git add .
    
  3. Commit (finalize) the changes in the local history. The message should be concise and meaningful:
    git commit -m "Description of changes made"
    

    The commit is created only locally. It is not visible on the remote server.

Step 5: Pushing Changes to the Remote Server

After creating a commit, you need to push it to the central repository so others can see it.

git push origin <branch_name>

By default, when cloning, the remote server is called origin, and the branch is main or master. So often this is sufficient:

git push origin main

On the first push for a new branch, you may need to specify -u to set tracking: git push -u origin feature-branch.

Step 6: Working with Branches

Branches allow you to isolate development of new features or fixes. The main branch is usually called main.

  • Create a new branch and switch to it:
    git checkout -b feature-login
    

    Or use the modern syntax:
    git switch -c feature-login
    
  • Switch between existing branches:
    git checkout main
    

    or
    git switch main
    
  • List all branches (local and remote):
    git branch -a
    
  • Delete a local branch (after it has been merged into the main branch):
    git branch -d feature-login
    

Verifying the Result

Ensure the operations were performed correctly:

  1. After git commit, check the commit history: git log --oneline. The new commit should be at the top of the list.
  2. After git push, open your repository page on GitHub/GitLab. The new commit should appear in the commit list.
  3. After creating/switching branches, run git branch. The asterisk * will show which branch you are currently on.

Common Issues

  • Error fatal: not a git repository.
    • Cause: You are not in a Git repository directory.
    • Solution: Navigate to the root of the cloned repository (cd project-name) or initialize a new one: git init.
  • Error remote: Repository not found during git push.
    • Cause: Incorrect URL for the remote repository (origin) or lack of write permissions.
    • Solution: Check the URL: git remote -v. Correct it if necessary: git remote set-url origin <correct_URL>. Ensure you are authenticated on the platform (use an SSH key or token).
  • Merge conflict during git pull or git merge.
    • Cause: The same lines in a file were modified in different branches.
    • Solution: Git will mark the conflicting files. Open them and find the blocks between <<<<<<< HEAD, =======, and >>>>>>> branch-name. Manually edit the file, keeping the desired version. Remove the conflict markers. Then run git add <file> and git commit to complete the merge.

F.A.Q.

How to install Git on Ubuntu/Debian?
What's the difference between `git commit` and `git push`?
How to undo the last commit but keep changes in the working directory?
What to do during a merge conflict?

Hints

Install Git
Configure global settings
Clone an existing repository
Add files and create a commit
Push changes to a remote server
Create and switch to a new branch
FixPedia

Free encyclopedia for fixing errors. Step-by-step guides for Windows, Linux, macOS and more.

© 2026 FixPedia. All materials are available for free.

Made with for the community