Whether you're new to Git or need a quick refresher, this post will walk you through the most commonly used Git commands with practical examples. Each command is paired with input/output and real-world context so you can learn by doing. Let’s dive in!
1. git init
– Start a New Git Repository
Use Case:
You're creating a new project and want to start tracking it with Git.
Input:
mkdir my-app && cd my-app
git init
Output:
Initialized empty Git repository in /home/user/my-app/.git/
Explanation:
This creates a .git
directory that tracks your project. It's now a local Git repo.
2. git clone
– Copy an Existing Repository
Use Case:
You want to contribute to an existing project or start working locally on one from GitHub.
Input:
git clone https://github.com/octocat/Hello-World.git
Output:
Cloning into 'Hello-World'...
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
Explanation:
Creates a new directory (Hello-World/
) and downloads all files and history from the repo.
3. git status
– Check Your Work
Use Case:
You want to see what’s changed, what’s staged, and what’s untracked.
Input:
git status
Output:
On branch main
Untracked files:
(use "git add ..." to include in what will be committed)
index.html
Explanation:
Shows what files are untracked (not staged) or modified. Helpful before committing.
4. git add
– Stage Changes
Use Case:
You're ready to tell Git which files to include in the next commit.
Input:
git add index.html
Output:
(no output on success)
Explanation:
Stages the file so it will be included in the next commit. Use .
to stage all files:
git add .
5. git commit
– Save a Snapshot
Use Case:
You're done with a chunk of work and want to checkpoint your progress.
Input:
git commit -m "Add landing page"
Output:
[main 3e1f51a] Add landing page
1 file changed, 12 insertions(+)
Explanation:
Commits all staged files to your local repo with a message.
6. git push
– Upload Changes to Remote
Use Case:
You want to push your local commits to GitHub (or another remote).
Input:
git push origin main
Output:
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
To https://github.com/user/my-app.git
abc1234..def5678 main -> main
Explanation:
Sends your local commits to the remote repository. Often used after commit
.
7. git pull
– Get Latest Changes
Use Case:
Other team members made changes and you want to fetch + merge them into your local copy.
Input:
git pull origin main
Output:
Updating abc1234..def5678
Fast-forward
style.css | 4 ++++
Explanation:
Downloads and merges new changes from the remote branch.
8. git branch
– Manage Branches
View all branches:
git branch
Create a new branch:
git branch feature/login
Switch to a branch:
git checkout feature/login
Output:
Switched to branch 'feature/login'
Explanation:
Branches help isolate features or fixes. Always create a branch for new work!
9. git merge
– Combine Branches
Use Case:
You're done working in a feature branch and want to merge it into main
.
Input:
git checkout main
git merge feature/login
Output:
Updating abc1234..def5678
Fast-forward
login.js | 20 ++++++++++++++++++++
Explanation:
Combines the changes from the feature branch into your main branch.
10. git log
– View Commit History
Input:
git log --oneline
Output:
def5678 Add login functionality
abc1234 Add landing page
3e1f51a Initial commit
Explanation:
Shows a compact history of commits. Super helpful for understanding progress or debugging.
Bonus: Undoing Mistakes
Unstage a file:
git restore --staged file.txt
Discard changes:
git restore file.txt
Reset last commit (keep changes):
git reset --soft HEAD~1
Summary Table
Command | Description |
---|---|
git init |
Start a new repo |
git clone |
Copy an existing repo |
git status |
Check file changes |
git add |
Stage files for commit |
git commit |
Record staged changes |
git push |
Upload commits to remote |
git pull |
Get latest changes from remote |
git branch |
Create/list branches |
git checkout |
Switch branches |
git merge |
Combine branches |
git log |
View commit history |
Final Thoughts
These commands form the backbone of your daily Git workflow. Whether you're building a side project, collaborating on a team, or contributing to open source, mastering these basics will keep your code safe, trackable, and team-friendly.