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:
1git initgit clone
Or create local copy of a remote repository.
1git clone https://github.com/user/repo.git2. Staging & Committing
git status
This command shows the state of your working directory — what's staged, unstaged, or untracked.
1git statusgit add
Stages changes for the next commit.
1git add file.txt # Stage a specific filegit add .
2 # Stage all changesgit add -p
3 # Interactively stage chunks of changesgit commit
Records staged changes to the repository.
1git commit -m "Your commit message"
2git commit --amend # Modify the most recent commit3. Branching & Merging
git branch
Lists, creates, or deletes branches.
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.
1git checkout main
2git switch main # Modern alternativegit switch -c new-branch
3 # Create and switch to a new branchgit merge
Merges a branch into the current branch.
1git merge feature-x
2git merge --no-ff feature-x # Preserve merge commit historygit rebase
Reapplies commits on top of another branch — great for keeping a clean history.
1git rebase main
2git rebase -i HEAD~3 # Interactive rebase for the last 3 commits4. Remote Repositories
git remote
Manages connections to remote repositories.
1git remote -v # List remotes git remote add origin <url>
2 # Add a remote git remote set-url origin <new-url>
3 # Update remote URLgit fetch
Downloads changes from a remote without merging them.
1git fetch origin
2git fetch --all # Fetch from all remotesgit pull
Fetches and merges changes from a remote branch.
1git pull origin main
2git pull --rebase origin main # Pull with rebase instead of mergegit push
Uploads local commits to a remote repository.
1git push origin main
2git push -u origin feature-x # Set upstream and pushgit push --force-with-lease
3 # Safer alternative to force push5. Undoing & Fixing Mistakes
git restore
Discards changes in the working directory.
1git restore file.txt # Discard unstaged changesgit restore --staged file.txt
2 # Unstage a filegit reset
Moves the HEAD pointer and optionally modifies the index/working directory.
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 changesgit revert
Creates a new commit that undoes a previous commit — safe for shared branches.
1git revert <commit-hash>git stash
Temporarily shelves changes so you can work on something else.
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 stash6. Viewing History & Differences
git log
Shows the commit history.
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.
1git diff # Unstaged changesgit diff --staged
2 # Staged changesgit diff main feature-x
3 # Differences between branchesgit show
Displays information about a specific commit.
1git show <commit-hash>7. Tagging
git tag
Marks specific points in history — typically used for releases.
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 remote8. Advanced & Power Commands
git cherry-pick
Applies a specific commit from one branch to another.
1git cherry-pick <commit-hash>git bisect
Uses binary search to find the commit that introduced a bug.
1git bisect start
2git bisect bad # Current commit is badgit bisect good v1.0
3 # Last known good commitgit blame
Shows who last modified each line of a file.
1git blame file.txtgit reflog
Records every movement of HEAD — your safety net for recovering lost commits.
1git refloggit checkout HEAD@{3} # Jump to a previous HEAD positiongit shortlog
Summarizes commit history by author — great for changelogs.
1git shortlog -sn # Show commit counts per authorgit archive
Creates a zip/tar of a repository at a given commit.
1git archive --format=zip HEAD > output.zip9. Configuration & Aliases
git config
Configures Git settings at the user or repository level.
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 settingsCreating Aliases
Speed up your workflow with custom shortcuts.
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 |
|---|---|
| Initialize a repository |
| Clone a remote repo |
| Interactively stage changes |
| Modify the last commit |
| Create and switch branch |
| Interactive rebase last n commits |
| Re-apply stashed changes |
| Visual commit history |
| Apply a specific commit |
| Recover lost commits |
| Find the bug-introducing commit |
| 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