Basic Git Workflow: Add, Commit, Push โ
Introduction โ
Now that you have created your first Git repository and understand the basic concepts, it's time to learn the fundamental Git workflow. This workflow forms the backbone of daily Git usage and consists of three main steps: Add, Commit, and Push.
This tutorial will guide you through these essential operations, helping you understand how to track changes, save snapshots of your work, and share your code with others.
Prerequisites โ
Before starting this tutorial, make sure you have:
- A Git repository created (Creating Your First Git Repository)
- Basic understanding of Git concepts (Understanding Git Basics and Terminology)
- Some files in your repository to work with
The Basic Git Workflow โ
The standard Git workflow follows these steps:
1. Modify files in working directory
2. Stage changes (git add)
3. Commit changes (git commit)
4. Push to remote repository (git push)Let's explore each step in detail.
Step 1: Understanding Current Status โ
Before making any changes, let's check the current status of our repository:
git statusYou should see something like:
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
README.md
config.json
hello.py
sample.txt
nothing added to commit but untracked files present (use "git add" to track)This shows:
- Current branch:
main - No commits yet
- Several untracked files
Step 2: Adding Files to Staging Area (git add) โ
The git add command moves files from the working directory to the staging area. This is where you prepare your next commit.
Adding Individual Files โ
Add files one by one:
# Add the README file
git add README.md
# Check status
git statusYou should see:
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
config.json
hello.py
sample.txtNotice the difference:
README.mdis now under "Changes to be committed" (staged)- Other files remain "Untracked"
Adding Multiple Files โ
Add multiple files at once:
# Add multiple specific files
git add hello.py config.json
# Or add all files in current directory
git add .
# Check status
git statusAfter adding all files:
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .gitignore
new file: README.md
new file: config.json
new file: hello.py
new file: sample.txtCommon git add Patterns โ
# Add all files
git add .
# Add all files in current directory and subdirectories
git add -A
# Add only modified files (not new files)
git add -u
# Add files interactively
git add -i
# Add specific file types
git add *.py
git add *.mdUnderstanding the Staging Area โ
The staging area allows you to:
- Craft precise commits - Choose exactly what goes into each commit
- Review changes - See what will be committed before committing
- Split changes - Commit related changes separately
Step 3: Making Your First Commit (git commit) โ
A commit creates a snapshot of your staged changes. Each commit should represent a logical unit of work.
Basic Commit Command โ
git commit -m "Initial commit: Add project files"You should see output like:
[main (root-commit) a1b2c3d] Initial commit: Add project files
5 files changed, 23 insertions(+)
create mode 100644 .gitignore
create mode 100644 README.md
create mode 100644 config.json
create mode 100755 hello.py
create mode 100644 sample.txtUnderstanding the output:
main- Current branchroot-commit- This is the first commita1b2c3d- Short commit hash5 files changed, 23 insertions(+)- Summary of changes
Commit Message Best Practices โ
Good commit messages are crucial for project maintenance:
Structure โ
Short summary (50 characters or less)
More detailed explanation if needed. Wrap at 72 characters.
Explain what and why, not how.
- Use bullet points for multiple changes
- Reference issue numbers if applicableExamples of Good Commit Messages โ
# Good - Clear and concise
git commit -m "Add user authentication system"
# Good - Explains the why
git commit -m "Fix login bug that prevented password reset"
# Good - Multiple line commit
git commit -m "Implement user profile editing
- Add form validation
- Update user model
- Add profile image upload
- Fix styling issues on mobile"Examples of Bad Commit Messages โ
# Bad - Too vague
git commit -m "fix stuff"
# Bad - Not descriptive
git commit -m "update"
# Bad - Too long for summary
git commit -m "This commit adds the new user authentication system that allows users to log in and register accounts with email validation and password reset functionality"Alternative Commit Methods โ
Using Default Editor โ
# Opens your default editor for commit message
git commitCommitting All Changes โ
# Stages and commits all tracked files
git commit -a -m "Update all tracked files"Step 4: Viewing Commit History โ
After making commits, you can view your repository's history:
# View commit history
git logOutput:
commit a1b2c3d4e5f6789... (HEAD -> main)
Author: Your Name <your.email@example.com>
Date: Wed Jul 9 10:30:00 2025 +0000
Initial commit: Add project filesUseful git log Options โ
# Compact one-line format
git log --oneline
# Show last 3 commits
git log -3
# Show commits with file changes
git log --stat
# Show commits with actual changes
git log -p
# Graphical representation
git log --graph --onelineMaking Additional Changes โ
Let's practice the workflow with some changes:
Step 1: Modify a File โ
Edit the README.md file:
echo "
## Recent Updates
- Added basic project structure
- Created initial configuration
- Set up Git repository" >> README.mdStep 2: Check Status โ
git statusYou should see:
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and commit)Step 3: View Changes โ
See what changed:
git diffThis shows the differences between your working directory and the last commit.
Step 4: Stage and Commit โ
# Stage the changes
git add README.md
# Commit the changes
git commit -m "Update README with project status"Understanding File States โ
Files in Git go through different states:
Untracked โ Staged โ Committed
โ โ โ
git add โ git commit โ git pushFile Status Examples โ
# Check detailed status
git status
# See short status
git status -sShort status symbols:
??- Untracked fileA- Added (staged)M- ModifiedD- DeletedR- Renamed
Step 5: Setting Up a Remote Repository โ
To push your changes, you need a remote repository. Let's set up a remote:
Using GitHub (Example) โ
- Create a new repository on GitHub
- Copy the repository URL
- Add it as a remote:
# Add remote repository
git remote add origin https://github.com/yourusername/my-first-git-project.git
# Verify remote
git remote -vUsing GitLab or Other Services โ
The process is similar:
# GitLab example
git remote add origin https://gitlab.com/yourusername/my-first-git-project.git
# Self-hosted Git server
git remote add origin user@server:/path/to/repo.gitStep 6: Pushing to Remote Repository (git push) โ
Push your commits to the remote repository:
# Push to remote repository
git push -u origin mainThe -u flag sets up tracking between your local main branch and the remote main branch.
Understanding Push Output โ
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 4 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (7/7), 1.23 KiB | 1.23 MiB/s, done.
Total 7 (delta 0), reused 0 (delta 0)
To https://github.com/yourusername/my-first-git-project.git
* [new branch] main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.Future Pushes โ
After the initial push with -u, you can simply use:
git pushComplete Workflow Example โ
Here's a complete example of the Git workflow:
# 1. Make changes
echo "print('Hello, Git!')" > new_script.py
# 2. Check status
git status
# 3. Stage changes
git add new_script.py
# 4. Commit changes
git commit -m "Add new Python script"
# 5. Push to remote
git pushCommon Git Workflow Patterns โ
Feature Development Workflow โ
# Start working on a feature
git status
# ... make changes ...
git add .
git commit -m "Implement feature X"
git pushBug Fix Workflow โ
# Fix a bug
git status
# ... fix the bug ...
git add -u # Add only modified files
git commit -m "Fix bug in user authentication"
git pushRegular Development Workflow โ
# Daily development cycle
git status
# ... work on code ...
git add .
git commit -m "Add user profile validation"
# ... more work ...
git add .
git commit -m "Update error handling"
git pushBest Practices โ
1. Commit Often โ
- Make small, focused commits
- Commit related changes together
- Don't wait too long between commits
2. Write Good Commit Messages โ
- Use present tense ("Add feature" not "Added feature")
- Keep first line under 50 characters
- Explain why, not just what
3. Review Before Committing โ
# Always check what you're committing
git status
git diff --staged4. Use Staging Area Effectively โ
- Stage only related changes
- Use
git add -pfor partial file staging - Review staged changes before committing
Troubleshooting Common Issues โ
Issue: "Nothing to commit" โ
Cause: No changes staged for commit. Solution: Use git add to stage changes first.
Issue: "Repository not found" โ
Cause: Remote repository URL is incorrect. Solution: Check remote URL with git remote -v.
Issue: "Authentication failed" โ
Cause: Incorrect credentials or permissions. Solution: Verify your username/password or SSH keys.
Issue: "Uncommitted changes" โ
Cause: Trying to push with uncommitted changes. Solution: Commit or stash changes first.
Useful Commands Summary โ
Status and Information โ
git status # Check repository status
git log # View commit history
git diff # Show changes
git remote -v # Show remote repositoriesStaging and Committing โ
git add <file> # Stage specific file
git add . # Stage all files
git commit -m "msg" # Commit with message
git commit -a -m "msg" # Stage and commit tracked filesRemote Operations โ
git remote add origin <url> # Add remote repository
git push -u origin main # Push and set upstream
git push # Push to configured remoteSummary โ
You've successfully learned the basic Git workflow! Here's what you accomplished:
- Understanding the workflow: Add โ Commit โ Push
- Staging changes: Using
git addto prepare commits - Making commits: Creating snapshots with
git commit - Setting up remotes: Connecting to external repositories
- Pushing changes: Sharing your work with
git push - Best practices: Writing good commit messages and organizing work
Key Commands Mastered: โ
git add- Stage changes for commitgit commit- Create a snapshot of staged changesgit push- Upload commits to remote repositorygit status- Check current repository stategit log- View commit historygit diff- See changes between versions
Workflow Pattern: โ
Edit files โ git add โ git commit โ git pushThis basic workflow forms the foundation of all Git usage. Whether you're working alone or with a team, these commands will be your daily tools for version control.
Next Steps โ
Now that you understand the basic Git workflow, you're ready to explore more advanced topics:
