Git Commands Reference
Introduction
Git is a version control system that helps you track changes in your code, collaborate with others, and revert to previous versions when needed.
Why Git Knowledge is Essential
- Work in teams without code conflicts.
- Track and document changes over time.
- Quickly roll back if something breaks.
1️⃣ Git Basics
Check Git Version
git --version
Git Configurations
List Configurations
git config --list
Set Global User Info
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
Automatically Prune Deleted Remote Branches
git config --global fetch.prune true
For a specific repository:
git config --local fetch.prune true
Verify Configuration
git config --global --get fetch.prune
git config --local --get fetch.prune
2️⃣ Working with Repositories
Initialize a Repository
git init
Clone a Repository
git clone https://github.com/company/project.git
Check Repository Status
git status
It shows:
- Modified files
- New files
- Untracked files
3️⃣ Committing Changes
Add & Commit Files
git add .
git commit -m "Fixed bug in functions.php"
4️⃣ Working with Branches
List Branches
git branch # Local branches
git branch -r # Remote branches
git branch -a # All branches
git branch -v # Branches with last commit
git ls-remote --heads origin # More remote details
Create & Switch to a New Branch
git checkout -b feature-woocommerce-optimization
Make changes and commit:
git add .
git commit -m "Optimized WooCommerce checkout speed"
Push branch to remote:
git push origin feature-woocommerce-optimization
Merge into main
git checkout main
git merge feature-woocommerce-optimization
Delete a Branch
git branch -d <branch-name> # Delete merged branch
git branch -D <branch-name> # Force delete unmerged branch
Delete a Remote Branch
git push <remote-name> --delete <branch-name>
Clean Up Deleted Remote Branches Locally
git remote prune origin
git fetch --prune # or git fetch -p
5️⃣ Undoing Mistakes
Undo the Last Commit
git reset --soft HEAD~1 # Keep changes
git reset --hard HEAD~1 # Discard changes
Restore a Specific File
git checkout -- wp-content/themes/mytheme/functions.php
6️⃣ Stashing (Temporary Changes)
Save & Restore Stashed Changes
git stash
git checkout main
git stash pop
7️⃣ Working with Remote Repositories
List Remote Repositories
git remote -v
Add a Remote Repository
git remote add origin https://github.com/user/repo.git
Push & Pull Changes
git push origin main
git pull origin main
Remove or Rename a Remote Repository
git remote remove <name>
git remote rename origin upstream
Fetch Updates from Remote
git remote update
git fetch <remote-name>
Change Remote URL
git remote set-url origin https://github.com/user/new-repo.git
Prune Deleted Remote Branches
git remote prune origin
8️⃣ Git Log
The git log command displays the commit history of the repository.
Useful options:
git log: Shows the basic commit history.git log --oneline: Displays each commit as a single line, showing the commit hash and message.git log --graph: Visualizes the commit history in a graphical format.git log --author="Name": Filters commits by author.git log --since="date": Shows commits after a specified date.git log --until="date": Shows commits before a specified date.
Example:
git log --oneline --graph
This will show a concise and graphical view of your commit history.
9️⃣ Git Merge
The git merge command is used to combine changes from different branches.
Typical usage scenarios:
git merge origin/main: Merges changes from the remote main branch into your local branch.git checkout mainfollowed bygit merge feature-branch: Merges a feature branch into the main branch.
Handling merge conflicts:
If there are conflicting changes, Git will mark the conflict. You need to manually resolve it, then:
git add <resolved-files>
git commit
Example:
git checkout main
git merge feature-branch
1️⃣1️⃣ Difference Between git clone, git pull, and git fetch
These three commands are used to interact with remote repositories but serve different purposes:
1️⃣ git clone
- Copies an entire remote repository to your local machine.
- Downloads all commits, branches, and files.
- Used when working with a repository for the first time.
git clone https://github.com/user/repository.git
2️⃣ git pull
- Fetches latest changes from the remote repository.
- Automatically merges them into your local branch.
- Equivalent to running
git fetchfollowed bygit merge.
git pull origin main
3️⃣ git fetch
- Retrieves changes from the remote but does not merge them.
- Updates references to remote branches without modifying working files.
- Useful when you want to check for updates before merging.
git fetch origin
🔹 Key Differences
| Command | Action | Merges Changes? | When to Use? |
|---|---|---|---|
git clone | Copies entire repo | ✅ Yes (initially) | First time downloading a repo |
git pull | Fetch + merge | ✅ Yes | Keeping local branch updated |
git fetch | Fetches updates only | ❌ No | Checking updates before merging |