Useful Git Commands You Should Know
Git

Written By: Samuel Mkamanga

Last Update: Fri Feb 27 2026


Useful Git Commands You Should Know

Git is a very essential skill for developers, you can improve your workflow, manage versions of your project and share your project with other developers from this skill set.

This Discussion will cover most important git commands every developer needs to know.


1. Getting Started

git init

To get a started you can create a repository in current folder/ working directory using this command:

bash
1git init

git clone

Or create local copy of a remote repository.

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

2. Staging & Committing

git status

This command shows the state of your working directory — what's staged, unstaged, or untracked.

bash
1git status

git add

Stages changes for the next commit.

bash
1git add file.txt       # Stage a specific filegit add .             
2                       # Stage all changesgit add -p            
3                       # Interactively stage chunks of changes

git commit

Records staged changes to the repository.

bash
1git commit -m "Your commit message"
2git commit --amend    # Modify the most recent commit

3. Branching & Merging

git branch

Lists, creates, or deletes branches.

bash
1git branch             # List all local branchesgit branch feature-x   
2                       # Create a new branchgit branch -d feature-x  
3                       # Delete a branchgit branch -a          
4                       # List all branches (local + remote)

git checkout / git switch

Switches between branches or restores files.

bash
1git checkout main
2git switch main            # Modern alternativegit switch -c new-branch   
3                           # Create and switch to a new branch

git merge

Merges a branch into the current branch.

bash
1git merge feature-x
2git merge --no-ff feature-x   # Preserve merge commit history

git rebase

Reapplies commits on top of another branch — great for keeping a clean history.

bash
1git rebase main
2git rebase -i HEAD~3   # Interactive rebase for the last 3 commits

4. Remote Repositories

git remote

Manages connections to remote repositories.

bash
1git remote -v # List remotes git remote add origin <url> 
2              # Add a remote git remote set-url origin <new-url>    
3              # Update remote URL

git fetch

Downloads changes from a remote without merging them.

bash
1git fetch origin
2git fetch --all    # Fetch from all remotes

git pull

Fetches and merges changes from a remote branch.

bash
1git pull origin main
2git pull --rebase origin main   # Pull with rebase instead of merge

git push

Uploads local commits to a remote repository.

bash
1git push origin main
2git push -u origin feature-x    # Set upstream and pushgit push --force-with-lease     
3                                # Safer alternative to force push

5. Undoing & Fixing Mistakes

git restore

Discards changes in the working directory.

bash
1git restore file.txt        # Discard unstaged changesgit restore --staged file.txt  
2                            # Unstage a file

git reset

Moves the HEAD pointer and optionally modifies the index/working directory.

bash
1git reset --soft HEAD~1    # Undo last commit, keep changes stagedgit reset --mixed HEAD~1   
2                           # Undo last commit, keep changes unstagedgit reset --hard HEAD~1    
3                           # Undo last commit, discard all changes

git revert

Creates a new commit that undoes a previous commit — safe for shared branches.

bash
1git revert <commit-hash>

git stash

Temporarily shelves changes so you can work on something else.

bash
1git stash              # Stash current changesgit stash pop          
2                       # Re-apply the latest stashgit stash list         
3                       # View all stashesgit stash drop         
4`                      # Delete a stashgit stash apply stash@{2}  
5                       # Apply a specific stash

6. Viewing History & Differences

git log

Shows the commit history.

bash
1git loggit log --oneline --graph --all   # Visual branch graphgit log --author="John"           
2                                         # Filter by authorgit log -- path/to/file  
3                                         # History for a specific file
4                                     

git diff

Shows differences between commits, branches, or files.

bash
1git diff # Unstaged changesgit diff --staged      
2         # Staged changesgit diff main feature-x  
3         # Differences between branches

git show

Displays information about a specific commit.

bash
1git show <commit-hash>

7. Tagging

git tag

Marks specific points in history — typically used for releases.

bash
1git tag v1.0. # Lightweight taggit tag -a v1.0.0 -m "Release v1.0.0"  
2              # Annotated taggit push origin --tags                  
3              # Push all tags to remote

8. Advanced & Power Commands

git cherry-pick

Applies a specific commit from one branch to another.

bash
1git cherry-pick <commit-hash>

git bisect

Uses binary search to find the commit that introduced a bug.

bash
1git bisect start
2git bisect bad   # Current commit is badgit bisect good v1.0     
3                                 # Last known good commit

git blame

Shows who last modified each line of a file.

bash
1git blame file.txt

git reflog

Records every movement of HEAD — your safety net for recovering lost commits.

bash
1git refloggit checkout HEAD@{3}   # Jump to a previous HEAD position

git shortlog

Summarizes commit history by author — great for changelogs.

bash
1git shortlog -sn   # Show commit counts per author

git archive

Creates a zip/tar of a repository at a given commit.

bash
1git archive --format=zip HEAD > output.zip

9. Configuration & Aliases

git config

Configures Git settings at the user or repository level.

bash
1git config --global user.name "Your Name"
2git config --global user.email "you@example.com"
3git config --global core.editor "code --wait"
4git config --list   # View all config settings

Creating Aliases

Speed up your workflow with custom shortcuts.

bash

bash
1git config --global alias.st status
2git config --global alias.co checkout
3git config --global alias.lg "log --oneline --graph --all"

Now git lg gives you a beautiful, visual commit graph in one command.


Quick Reference Cheat Sheet

Command

What it Does

git init

Initialize a repository

git clone <url>

Clone a remote repo

git add -p

Interactively stage changes

git commit --amend

Modify the last commit

git switch -c <branch>

Create and switch branch

git rebase -i HEAD~n

Interactive rebase last n commits

git stash pop

Re-apply stashed changes

git log --oneline --graph

Visual commit history

git cherry-pick <hash>

Apply a specific commit

git reflog

Recover lost commits

git bisect

Find the bug-introducing commit

git push --force-with-lease

Safe force push


If you are interested in learning more about Programming and Building project’s feel free to contact me below I give one on one lecture’s online on Google meet.

Happy committing! 🚀


Written By: Samuel Mkamanga

Last Update: Fri Feb 27 2026

Useful Git Commands You Should Know