Useful Git Commands: A Comprehensive Guide

Here is a detailed list of useful Git commands, along with illustrative examples:

git init

Initialize a new Git repository in your project directory.


$ git init
Initialized empty Git repository in /path/to/your/project/.git/

git clone [url]

Clone a remote repository from the server to your local machine.


$ git clone
Cloning into 'your-repo'...

git add [file]

Add one or more files to the staging area to prepare for a commit.


$ git add index.html
$ git add *.css

git commit -m "message"

Create a new commit with the changes that have been added to the staging area and include your commit message.


$ git commit -m "Fix a bug in login process"
[main 83a9b47] Fix a bug in login process
1 file changed, 5 insertions(+), 2 deletions(-)

git status

View the current status of the repository, including modified files and the staging area.


$ git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   index.html

no changes added to commit (use "git add" and/or "git commit -a")

git log

Display the commit history of the repository.


$ git log
commit 83a9b4713f9b6252bfc0367c8b1ed3a8e9c75428 (HEAD -> main)
Author: Your Name <[email protected]>
Date:   Mon Jul 13 12:34:56 2023 +0200

    Fix a bug in login process

commit 47f1c32798b7e862c4c69718abf6498255f1a3d2
Author: Your Name <[email protected]>
Date:   Sun Jul 12 18:42:15 2023 +0200

    Add new homepage

git branch

List all branches in the repository and mark the current branch.


$ git branch
* main

git checkout [branch]

Switch to another branch in the repository.


$ git checkout feature/fix-bug
Switched to branch 'feature/fix-bug'

git merge [branch]

Merge another branch into the current branch.


$ git merge feature/add-new-feature
Updating 83a9b47..65c6017
 new-feature.html | 10 ++++++++++
 1 file changed, 10 insertions(+)
 create mode 100644 new-feature.html

git pull

Fetch and integrate changes from a remote repository into the current branch.


$ git pull origin main
* branch            main       -> FETCH_HEAD
Already up to date.

git push

Push changes from the current branch to a remote repository.


$ git push origin main

git remote add [name] [url]

Add a new remote server to your list of remote repositories.


$ git remote add upstream

git fetch

Download changes from remote repositories but don't integrate into the current branch.


$ git fetch origin

git diff

Compare changes between the staging area and the tracked files.


$ git diff

git reset [file]

Remove a file from the staging area and revert it to the previous state.


$ git reset index.html

git stash

emporarily save uncommitted changes to work on a different branch without committing them.


$ git stash
Saved working directory and index state WIP on feature/branch: abcd123 Commit message

git remote -v

List the remote servers and their url addresses.


$ git remote -v
origin (fetch)
origin (push)
upstream (fetch)
upstream (push)