Version control is an essential part of modern software development, and Git is the most widely used version control system. Here’s a list of basic Git commands that every developer should know when starting with Git.
1. Git Configuration
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
2. Initialize a Repository
git init
3. Clone a Repository
git clone https://github.com/username/repository.git
4. Check Status
git status
5. Stage Changes
git add filename # Stage a specific file
git add . # Stage all changes in the current directory
git add -A # Stage all changes (including deletions)
git add --all # Stage all changes in the repository
git add * # Stage all files in the current directory (excluding deleted files)
6. Commit Changes
git commit -m "Your commit message"
7.Recover erased data of a file or Recover deleted file
git checkout filename # Discard changes in a file
git checkout -f # Restore deleted files after commit
8. View Commit History
git log # Shows the full commit history of the current branch.
git log -p # Show changes in each commit. Shows the diffs introduced by each commit.
git log --oneline # Show a simplified commit history with one line per commit.
git show 9a0f1fc # Show details of a specific commit.
9. View Differences
git diff # Show unstaged changes in the working directory.
git diff --staged # Show staged changes that are ready to be committed.
git diff HEAD # Show all changes since the last commit.
git diff branch1..branch2 # Show differences between two branches.
git diff commit1..commit2 # Show differences between two commits.
10. .gitignore
A .gitignore file tells Git which files or folders to ignore so they don’t get tracked, committed, or pushed to a repository.
Syntax & Examples:
Place a .gitignore file in your project root. Example:
# Ignore compiled files
*.class
# Ignore log files
*.log
# Ignore system files
.DS_Store
Thumbs.db
# Ignore specific folders
node_modules/
target/
build/
11. Create and Switch Branch
git checkout -b new-branch
12. Merge Branch
git checkout main
git merge new-branch
13. Push to Remote
git push origin main
14. Pull from Remote
git pull origin main
15. Discard Changes
git checkout -- filename
16. Unstage a File
git reset filename
17. Delete a Branch
git branch -d branch-name
Conclusion: These commands cover the most essential
Git operations you'll use frequently. Master
these first, then move on to advanced topics like rebase
,
stash
, and
cherry-pick
.
✅ Stay consistent with commits and always write meaningful messages!