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/deletiontouch,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.
- Check the repository status to see modified and new files:
git status - 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 . - 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
orgit 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:
- After
git commit, check the commit history:git log --oneline. The new commit should be at the top of the list. - After
git push, open your repository page on GitHub/GitLab. The new commit should appear in the commit list. - 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 foundduringgit 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).
- Cause: Incorrect URL for the remote repository (
- Merge conflict during
git pullorgit 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 rungit add <file>andgit committo complete the merge.