Skip to content

Basic Git Commands

Overview

Git is a distributed version control system designed to handle everything from small to very large projects with speed and efficiency. This document covers the essential Git commands you'll need to manage your projects.

Initializing a Repository

git init

Initializes a new Git repository.

git init

Example

git init my-new-repo
cd my-new-repo

This creates a new directory my-new-repo with a Git repository initialized.

Cloning a Repository

git clone

Clones an existing repository.

git clone <repository-url>

Example

git clone https://github.com/user/repo.git

This command clones the repository from the specified URL to your local machine.

Checking the Status

git status

Displays the state of the working directory and the staging area.

git status

Example

git status

This command shows which changes have been staged, which haven't, and which files aren't being tracked by Git.

Adding Changes

git add

Adds changes to the staging area.

git add <file>

To add all changes:

git add .

Example

git add README.md

This command stages the changes made to README.md.

Committing Changes

git commit

Records changes to the repository.

git commit -m "Commit message"

Example

git commit -m "Add initial project files"

This command commits the staged changes with the message "Add initial project files".

Viewing Commit History

git log

Displays the commit history.

git log

Example

git log

This command shows a list of commits in reverse chronological order.

Creating a Branch

git branch

Lists, creates, or deletes branches.

To list branches:

git branch

To create a new branch:

git branch <branch-name>

Example

git branch new-feature

This command creates a new branch named new-feature.

Switching Branches

git checkout

Switches branches or restores working tree files.

To switch branches:

git checkout <branch-name>

Example

git checkout new-feature

This command switches to the new-feature branch.

Merging Branches

git merge

Merges one or more branches into the current branch.

git merge <branch-name>

Example

git merge new-feature

This command merges the new-feature branch into the current branch.

Pulling Changes

git pull

Fetches and integrates changes from a remote repository to the current branch.

git pull

Example

git pull origin main

This command fetches and merges changes from the main branch of the remote repository.

Pushing Changes

git push

Updates the remote repository with local commits.

git push

Example

git push origin main

This command pushes the local main branch to the remote repository.

Summary

These basic Git commands will help you get started with version control for your projects. For more detailed information on each command, refer to the official Git documentation.