Git Commands to Push Local Code to GitHub — Step by Step
Example:
-
Local Project:
D:\Syed\GitTest\ConsoleApp3 -
GitHub Repository:
https://github.com/syedmohamed2008/repo_test3.git -
Branch:
main
Step 1: Navigate to the project folder
cd D:\Syed\GitTest\ConsoleApp3
Moves to your local project directory.
Step 2: Initialize Git (first time only)
git init
Creates a local Git repository (.git folder). Skip this if Git is already initialized.
Step 3: Connect to GitHub (first time only)
git remote add origin https://github.com/syedmohamed2008/repo_test3.git
Connects your local repository to the existing GitHub repository.
Verify:
git remote -v
Skip adding the remote if origin is already configured correctly.
Step 4: Check the current branch
git branch
Displays your local branches. The * indicates the current branch.
Example:
* main
Step 5: Check modified files
git status
Displays new, modified, deleted, and staged files.
Step 6: Stage your changes
git add .
Stages all changes under the current directory.
Alternatively, stage a specific file:
git add Program.cs
Step 7: Commit your changes
git commit -m "Updated ConsoleApp3 application"
Creates a commit in your local Git repository.
Step 8: Pull the latest code
git pull --rebase origin main
Downloads the latest remote changes and replays your local commits on top of them.
Note: Use this when your local and remote branches share Git history. If Git reports unrelated histories or merge conflicts, resolve those before pushing.
Step 9: Push code to GitHub
git push -u origin main
Uploads your local commits to GitHub.
-
origin— Remote repository. -
main— Target branch. -
-u— Sets upstream tracking for future pushes.
After setting upstream, you can simply use:
git push
Step 10: Verify the result
git status
Expected output:
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
Daily Git Workflow — Most Important
Once the repository is configured, these are the commands you will commonly use:
# 1. Navigate to project
cd D:\Syed\GitTest\ConsoleApp3
# 2. Check modified files
git status
# 3. Stage changes
git add .
# 4. Commit changes
git commit -m "Updated application code"
# 5. Get latest remote changes
git pull --rebase origin main
# 6. Push changes to GitHub
git push origin main
# 7. Verify status
git status
Git Commands — Quick Reference
|
Command |
Purpose |
|---|---|
|
|
Initialize a local repository |
|
|
Connect to a remote repository |
|
|
View remote repositories |
|
|
Check current branch |
|
|
Check changed files |
|
|
Stage all changes |
|
|
Commit changes locally |
|
|
Integrate latest remote changes |
|
|
First push with upstream tracking |
|
|
Push subsequent commits |
|
|
View the latest five commits |
Remember the Git workflow:
Working Directory
Staging Area
Local Repository
git add
→
git commit
→
git push
GitHub Remote Repository
Key point for interviews: git commit saves changes locally, while git push uploads committed changes to the remote repository.