As a software developer, mastering a version control system like Git is essential to easily manage your codebase, track changes, collaborate with team members, and deploy code to production environments.
To learn Git, you need to start with the fundamentals. That's why we've compiled this comprehensive guide covering basic git commands, from initializing a git repository to mastering power-user tools like reflog and blame.
We’ve covered everything from creating a new branch to pushing tags to a remote server. We also provide explanations of each command alongside examples to help you understand real-world use cases.
Part 1: Setup & Initialization
Before writing code, you need to set up your environment. If you are new, make sure you have Git installed.
1. Git config
The git config command sets or retrieves git configuration variables. In 2026, signing your commits is standard practice, but let's start with your identity.
Git Command Example:
git config --global user.email example@example.com
git config --global user.name "Your Name"
2. Git init
The git init command initializes a new git repository in the working directory it is invoked. It creates a hidden .git folder that tracks your project.
Git Command Syntax:
git init
3. Git clone & Git fork
There are two ways to get existing code:
- Git Clone: Creates a copy of a remote git repository on your local machine. Use this for your own projects or team repos you have access to.
- Git Fork (Concept): While not a command line tool (it's done via GitHub/GitLab UI), "Forking" creates a server-side copy of someone else's repo under your account. You then clone your fork.
Git Command Syntax:
git clone <repo_url>
Part 2: The Daily Workflow (Stage, Commit, Check)
4. Git status
The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven't, and which files aren't being tracked by Git.
Git Command Syntax:
git status
5. Git add & Git rm
You need to tell Git which files to track or untrack.
To add files to the staging area:
git add . (Adds all files)
git add <filename> (Adds specific file)To stop tracking a file (remove it from the repo), use git rm. This removes files or directories that are tracked by Git.
git rm <file_path>
6. Git commit
Think of commits as snapshots of your code. The -m flag allows you to write a descriptive message. In 2026, meaningful commit messages are critical for AI coding tools to understand your history.
Git Command Syntax:
git commit -m "feat: added login functionality"
7. Git diff
Before you commit, it's wise to check exactly what you changed. The git diff command compares changes made to files in the working directory with those in the staging area.
Git Command Syntax:
git diff
Part 3: Modern Branching & Navigation
Note: In recent versions of Git (2.23+), the "checkout" command was split into two separate, clearer commands: switch and restore.
8. Git branch
The git branch command is used to create and manage branches. When you want to work on a feature, you create a new branch to isolate your code.
Git Command Syntax:
git branch <branch-name> (Create)
git branch (List all)
9. Git switch (The Modern "Checkout")
For years, developers used git checkout to switch branches. While that still works, git switch is the modern standard designed specifically for changing to a specific branch-name.
Git Command Syntax:
git switch <branch-name>
git switch -c <new-branch-name> (Create and switch instantly)10. Git checkout (Legacy/Multi-purpose)
The git checkout command allows users to switch branches OR restore files. It is the "Swiss Army Knife" of Git. While effective, it can be confusing, which is why switch and restore were introduced. You will still see this often in older tutorials.
Git Command Example:
git checkout main
11. Git merge
The git merge command combines changes from one branch (like a feature branch) into another (like the main branch).
Git Command Syntax:
git merge <branch-name>Part 4: Syncing with Remote
12. Git remote
The git remote command manages the connection between your local computer and the server (GitHub/GitLab). Origin is the standard name given to the primary remote server.
Git Command Example:
git remote add origin https://github.com/user/repo.git
13. Git fetch
The git fetch command downloads commits, files, and refs from a remote repository into your local repo, but does not merge them. It is the safe way to see what your team has been up to.

14. Git pull
git pull is actually a combination of two commands: git fetch followed immediately by git merge. It updates your current branch with the latest changes from the remote server.

15. Git push (with Safety!)
The git push command uploads your local branch commits to the remote repository.
Git Command Syntax:
git push origin mainWarning: In the past, developers used --force to overwrite history. In 2026, this is dangerous. Instead, use --force-with-lease. This ensures you don't overwrite someone else's work if they pushed code you haven't seen yet.
git push --force-with-lease origin main16. Git tag
Tags are often used to mark specific releases (like v1.0). Use git tag to point to a specific point in your commit history that is important.
Git Command Syntax:
git tag <tag-name>
Part 5: Inspection & Detective Work
17. Git log
The git log command shows the commit history of the repository, listing every commit, author, and timestamp. This is essential for understanding how the project evolved.

18. Git blame
This is a powerful debugging tool. git blame shows you the author and commit hash for every single line in a file. If code is breaking, use this to find out who wrote it and (more importantly) why they wrote it (by checking the commit message).
Git Command Syntax:
git blame <filename>19. Git show
The git show command allows you to view the details of a specific object, often the most recent commit.
Git Command Syntax:
git show <commit_hash>
git show HEAD (Shows the most recent commit)Part 6: The "Oh No" Toolkit (Fixing Mistakes)
20. Git restore (Modern Undo)
If you have modified a file but haven't committed it yet, and you want to throw away your changes, use git restore.
Git Command Syntax:
git restore <filename>21. Git reset
The git reset command is used to undo commits.
- Soft Reset: Undoes the commit but keeps your changes in the staging area.
- Hard Reset: Undoes the commit and deletes all changes. Use with caution.
Git Command Syntax:
git reset --hard HEAD~1 (Go back 1 commit and delete changes)
22. Git revert
The git revert command is the "safe" undo. Instead of deleting history (like reset), it creates a new commit that does the exact opposite of the previous commit. This is perfect for fixing bugs in public branches.
Git Command Syntax:
git revert <commit_hash>23. Git reflog (The Time Machine)
This is the ultimate safety net. git reflog records updates to the tip of branches, even if you deleted them or reset them. If you accidentally did a "Hard Reset" and lost code, reflog can find the lost commit hash so you can restore it.
Git Command Syntax:
git reflog24. Git stash
The git stash command temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later.
Git Command Syntax:
git stash (Save changes)
git stash pop (Bring changes back)
25. Git clean
The git clean command removes untracked files from your working directory. It's useful for cleaning up build artifacts or temp files.
Git Command Syntax:
git clean -n (Dry run - check what will be deleted)
git clean -fd (Force delete files and directories)Masterclass: Efficiency with Aliases
In 2026, efficiency is key. Why type git checkout when you can type git co? You can configure aliases to speed up your workflow.
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st statusNow, you can simply type git st to check your status!
Conclusion
Git is an indispensable tool for software development, and its ecosystem has evolved. By moving beyond just commit and push to mastering switch, blame, and reflog, you position yourself as a capable, modern developer.
This article covered essential modern Git commands, updated for the 2026 workflow. Whether you are fixing a mistake with restore, creating a new branch, or collaborating via fork, these tools will streamline your daily coding life.
Want to keep these commands handy? Check out our