Essential Git Commands for Developers

Either you're a beginner or just need a reference, this guide will walk you through the most essential Git commands you will use mostly. Git is a distributed version control system that tracks changes in project over time. It lets you collaborate with others, revert changes, and maintain a clean history of the project.

Git was created created by the Linus Torvald, also the founder of Linux.

At first, Git tells who you are. This information is attached to every commit you make.

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Starting a Repository

Initialize a new repo
git init

Creates a new Git repository in your current folder.

Clone an existing repo
git clone https://github.com/user/repository.git

Downloads a remote repository to your local machine.


Tracking Changes

Check the status of your files
git status

Shows which files are modified, staged, or untracked.

Add files to the staging area
git add filename.txt       # Add a specific file
git add .                  # Add all changed files

Staging is like putting items in a box before shipping — you decide what goes in before committing.

Commit your changes
git commit -m "Your commit message here"

Saves a snapshot of your staged changes with a descriptive message. Good commit messages matter — be clear and concise.


Viewing History

See the commit log
git log
git log --oneline          # Compact view

Shows the history of commits in the current branch.

See what changed
git diff                   # Changes not yet staged
git diff --staged          # Changes staged but not committed

Working with Branches

Branches let you work on features or fixes without touching the main codebase.

List branches
git branch
Create a new branch
git branch <branch-name>

# convention
git branch category/short-desc

# examples of branches, all are valid
git branch update-login # short-desc
git branch feature/123-add-user-login # category/ticket-short-desc
git branch bugfix/fix-header-overflow # category/short-desc
Switch to a branch
git checkout <branch-name>
# OR (modern syntax)
git switch <branch-name>
Create and switch in one step
git checkout -b <branch-name>
# OR
git switch -c <branch-name>
Merge a branch into your current branch
git merge <branch-name>
Delete a branch
git branch -d <branch-name>

Working with Remotes

A remote is a name pointing to a URL. origin is the conventional default, but it can be anything (e.g. upstream, backup, deploy).

View remote connections
git remote -v
Add a remote
git remote add <remote-name> <url>

# Example:
git remote add origin https://github.com/user/repo.git
Push changes to a remote
git push <remote-name> <branch-name>

# Example:
git push origin main

Uploads your local commits to the remote repository.

Pull changes from a remote
git pull <remote-name> <branch-name>

# Example:
git pull origin main

Fetches and merges the latest changes from the remote into local.

Fetch without merging
git fetch <remote-name>

# Example:
git fetch origin

Downloads updates from the remote but doesn't apply them yet. Can be useful for reviewing before merging.


Undoing Things

Unstage a file (keep changes in working directory)
git restore --staged <file_name>
Discard changes in a file
git restore <file_name>

?? This permanently discards local changes to that file.

Undo the last commit (keep changes staged)
git reset --soft HEAD~1
Update/Amend/Rewrite the last commit message
git commit --amend -m "Updated commit message"

Stashing Work

To stash your work means to temporarily save your uncommitted changes to a clean, hidden shelf so that you can switch branches or pull updates without losing or committing your uncomplete(unfinished) code.

git stash            # Save current changes temporarily

git stash pop        # Restore the stashed changes at the top of stash stack

git stash list       # View all stashes

git stash drop       # drop the stash at the top without restoring

Cheat Sheet

Command Effect
git init Initialize a new repo
git clone <url> Clone a remote repo
git status Check file states
git add . Stage all changes
git commit -m "" Commit with a message
git push Push to remote
git pull Pull from remote
git branch List branches
git checkout -b Create & switch branch
git merge Merge a branch
git log --oneline View commit history
git stash Stash current changes

Extra Tips

  • Commit often: small, focused commits are easier to understand and to revert.
  • Write meaningful commit messages: so that you will understand it in future.
  • Use .gitignore: to keep secrets, build files, and local config out of your remote repository.