logo

A Comprehensive Guide to Basic Git Usage

O

Ohidur Rahman Bappy

MAR 22, 2025

Introduction

Git is a powerful, open-source version control system that helps developers manage changes in their source code over time. Whether you're collaborating with a team or working solo, mastering Git is essential for efficient and organized project management.

Install Git

To begin using Git, you'll need to install it on your system.

sudo apt update
sudo apt install git
git --version

Initialize a New Repository

Creating a new Git repository is the first step in tracking your project's changes.

mkdir my-git-repo
cd my-git-repo
git init

Configure Git

Set up your user name and email to have them associated with your Git commits.

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

Basic Git Operations

Here are some fundamental Git operations that you'll frequently use:

echo "This is my first repository." > README
git add README 
git commit -m "This is my first commit!"
echo "A repository is where all project files are stored." > README
git diff README 
git add README 
git status 
git commit -m "This is my second commit."
git log

Git for All Platforms

Download Git for your platform at git-scm.com.

Configure Tooling

Enhance your Git experience with these configurations:

git config --global color.ui auto

Creating and Cloning Repositories

There are two main ways to create a repository:

  • Initialize locally:

    git init
    git remote add origin [url]
    
  • Clone an existing repository:

    git clone [url]
    

Working with Branches

Branches allow you to work on different versions of your project simultaneously.

git branch [branch-name]           # Create a new branch
git checkout [branch-name]         # Switch to the branch
git merge [branch]                 # Merge history from another branch
git branch -d [branch-name]       # Delete a branch

The .gitignore File

Excluding files from Git tracking can be managed with .gitignore. Visit GitHub's .gitignore templates for help.

Change Management

Track and browse your project's history effectively with these commands:

git log                             # View history
git log --follow [file]            # Track changes to a file
git diff [first-branch]...[second-branch] # Compare branches
git show [commit]                  # Show commit details

Synchronize Changes

Keep your local and remote repositories in sync:

git fetch                          # Fetch history
git merge                          # Merge branches
git push                           # Push changes
git pull                           # Fetch then merge changes

Redo Commits

Sometimes you may need to alter your project's history:

git reset [commit]                 # Undo commits but keep changes
git reset --hard [commit]          # Discard changes to a commit

Caution: Altering history can lead to complications. Seek help from GitHub Community.

Deleting History

Start fresh by removing Git history:

cat .git/config  # Note the remote origin
rm -rf .git

git init
git add .
git commit -m "Initial commit"

git remote add origin <remote url>
git push -u --force origin master

Pushing to Remote Repositories

Choose your method of pushing code to a remote repository:

Using SSH

git push --set-upstream git@gitlab.example.com:namespace/project.git master

Using HTTPS

git push --set-upstream https://gitlab.example.com/namespace/project.git master

Glossary

  • Git: Distributed version-control system
  • GitHub: Platform for repository hosting
  • Commit: Snapshot of your project
  • Branch: Pointer to a commit
  • Clone: Copy of a repository
  • Remote: Common repository on GitHub
  • Fork: User-owned copy of a repository
  • Pull Request: Branch comparison and discussion on GitHub
  • HEAD: Current working directory indicator

For further information, visit the GitHub Git Cheat Sheet.