Basic Git Commands

Your quick guide to getting started with Git

Initialization & Setup

git init

Initialize a new local repository in the project folder.

Viewing Changes & History

git log

Show the commit history of the repository. Example output:

commit a1b2c3d4e5f67890123456789abcdef12345678

Author: Your Name <youremail@example.com>

Date: Sat Dec 14 08:30:00 2025 +0300

Initial commit

commit a1b2c3d4... → This is the commit's unique identifier (hash).

Author → The name and email of the person who made the commit.

Date → When the commit was created.

Message → The commit message describing the changes.

git log --oneline

Show a summarized commit history (IDs and messages only).

git log -n 5

Show a specific number (e.g., the last 5) of the most recent commits.

git checkout <id>

Switch to a specific commit or branch (used to view old versions of the code).

git diff

Show changes between commits, commit and working tree, etc.

Branch Management

git branch

List all local branches in your repository. The current branch is marked with an asterisk (*).

git branch <branch-name>

Create a new branch with the specified name, but don't switch to it.

git checkout <branch>

Switch to an existing branch, updating the working directory to match the branch.

git checkout -b <branch>

Create and switch to a new branch in one command.

git branch -m <new-name>

Rename the current branch to a new name.

git branch -d <branch-name>

Delete a local branch (only if it has been merged).

git branch -D <branch-name>

Force delete a local branch (even if it contains unmerged changes).

git merge <branch>

Merge the specified branch into the current branch.

Undoing Changes

git rm <file>

Remove a file from the repository and your local system.

git rm --cached <file>

Remove a file from the staging area only (keep it on your local system).

Git Configuration

git config --global user.name "Your Name"

Set your Git username for all repositories on your computer.

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

Set your Git email for all repositories on your computer.

git config user.name

Display your current Git username configuration.

git config user.email

Display your current Git email configuration.

GitHub Repository Setup

git branch -M main

Rename the current branch to 'main' (if you're using a different branch name)

git remote add origin https://github.com/username/repo.git

Connect your local repository to a GitHub repository (replace with your GitHub repository URL)

git push -u origin main

Push your local commits to the remote repository and set up tracking

Modifying and Uploading an Existing Project on GitHub

git add .

Adds all changes to the staging area.

Prepares all modified or new files for the next commit.

git commit -m "message"

Creates a new commit with the added changes (replace "message" with your description).

A commit is a historical reference snapshot of your project's changes.

git push

Uploads local commits to the remote repository (GitHub).

Makes your changes available online.

Terminal Commands

clear

Clear the terminal screen.