Git and GitHub

Most Used Git Commands

Mastering Git commands is essential for efficiently managing your codebase and collaborating with other developers. This section covers the most commonly used Git commands that you will frequently use in your day-to-day development workflow.

git init

Initializes a new Git repository in your project directory. This command creates a .git directory in the root of your project.

# Initialize a new Git repository
git init

git clone

This command creates a copy of an existing repository and is used to obtain a working copy of a remote repository.

# Clone a repository
git clone <https://github.com/user/repo.git>

git add

Stages changes to be included in the next commit. This command can add individual files or all changes in the working directory.

# Stage a specific file
git add path/to/file

# Stage all changes
git add .

git commit

Records the staged changes in the repository with a descriptive message.

# Commit staged changes with a message
git commit -m "Your commit message here"

# Commit with a detailed multi-line message
git commit

git status

Displays the state of the working directory and the staging area. It shows which changes have been staged, which haven't, and which files aren't being tracked by Git.

# Check the status of the repository
git status

git log

Shows the commit history for the repository. It includes information like commit hash, author, date and commit message.

# View the commit history
git log

# View a brief history with one line per commit
git log --oneline

# View a graph of your commits
git log --graph --oneline

git diff

Shows the differences between commits, commit and working tree, etc. It helps in reviewing the changes made to the codebase.

# Show changes between working directory and staging area
git diff

# Show changes between staging area and last commit
git diff --staged

# Show changes between two commits
git diff commit1 commit2

git branch

Lists all branches in the repository or creates/deletes branches.

# List all branches
git branch

# Create a new branch
git branch new-branch

# Delete a branch
git branch -d branch-name

git checkout

Switches branches or restores working tree files. This command is used to navigate between different branches.

# Switch to a branch
git checkout branch-name

# Create and switch to a new branch
git checkout -b new-branch

git merge

Combines changes from different branches into the current branch. This is typically used to integrate feature branches into the main branch.

# Merge a branch into the current branch
git merge branch-name

git pull

This command fetches changes from a remote repository and merges them into the current branch. It is used to update your local repository with changes from the remote repository.

# Pull changes from the remote repository
git pull origin branch-name

git push

Uploads local repository content to a remote repository. This command is used to share your changes with others.

# Push changes to the remote repository
git push origin branch-name

git remote

Manages the set of repositories whose branches you track. This command adds, removes, and manages connections to other repositories.

# Add a remote repository
git remote add origin <https://github.com/user/repo.git>

# List remote repositories
git remote -v

# Remove a remote repository
git remote remove origin

git fetch

Downloads objects and refs from another repository. This command retrieves updates from the remote repository without merging them.

# Fetch changes from the remote repository
git fetch origin

git reset

Resets the current branch to a specific state. This command can be used to undo changes.

# Unstage a file
git reset HEAD path/to/file

# Reset to a specific commit
git reset --hard commit-hash

git stash

This command temporarily saves changes that are not ready to be committed. It is useful for cleaning the working directory without committing changes.

# Stash changes
git stash

# Apply stashed changes
git stash apply

# List stashed changes
git stash list

# Apply and remove stashed changes
git stash pop

By familiarizing yourself with these commands, you'll be well-equipped to handle most tasks in Git, from basic operations to more advanced workflow management. These commands form the backbone of version control with Git, ensuring smooth and efficient project development.

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board