Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

Git Tutorial

Git HOME Git Intro Git Install Git Config Git Get Started Git New Files Git Staging Git Commit Git Remove and Move Git Tagging Git Stash Git History Git Diff Git Blame Git Help Git Branch Git Merge Git Workflow Git Best Practices

Git and GitHub

GitHub Get Started Git What is SSH? GitHub Add SSH GitHub Set Remote GitHub Edit Code Pull from GitHub Push to GitHub GitHub Branch Pull Branch from GitHub Push Branch to GitHub GitHub Flow GitHub Pages Git GUI Clients

Git Contribute

GitHub Fork Git Clone from GitHub GitHub Send Pull Request

Git Undo

Git Revert Git Reset Git Clean Git Amend Git Rebase Git Reflog Git Recovery

Git Advanced

Git .gitignore Git .gitattributes Git Large File Storage (LFS) Git Signing Commits/Tags Git Cherrypick & Patch Git Bisect Git Merge Conflicts Git CI/CD Git Hooks Git Submodules Git Worktree Git Remote Advanced

Git and Bitbucket

Bitbucket Get Started Bitbucket Add SSH Bitbucket Set Remote Bitbucket Edit Code Bitbucket Branch Bitbucket Pages Bitbucket Fork Git Clone from Bitbucket Bitbucket Send Pull Request

Git and GitLab

GitLab Get Started GitLab Add SSH GitLab Set Remote GitLab Edit Code GitLab Branch GitLab Pages GitLab Fork Git Clone from GitLab GitLab Send Pull Request

Git Cert

Git Certificate

Git Reference

Git Glossary

Git Exercises

Git Terminal Git Exercises Git Challenges Git Quiz Git Syllabus Git Study Plan

Git Best Practices


Share

Summary of Git Best Practices

  • Commit Often
  • Write Clear Commit Messages
  • Use Branches
  • Pull Before You Push
  • Review Changes Before Committing
  • Keep Repositories Small
  • Use .gitignore
  • Tag Releases

Commit Often

Make small, frequent commits to capture your progress.

This makes it easier to track changes and find bugs.

Example

git add .
git commit -m "Add user authentication logic"
[main 93091fb] Add user authentication logic
 1 file changed, 1 insertion(+)
 create mode 100644 auth.js
Try it Yourself »

Write Clear Commit Messages

Use descriptive messages that explain why a change was made, not just what changed.

Good commit messages help you and your team understand the history of the project.

  • Be specific: Say what and why, not just "Update" or "Fix".
  • Use the imperative mood: For example, "Add login validation" instead of "Added login validation".

Example

git commit -m "Fix bug in user login validation"
[main ed82f74] Fix bug in user login validation
 1 file changed, 1 insertion(+), 1 deletion(-)
Try it Yourself »


Use Branches

Create branches for features, fixes, and experiments to keep your main branch stable.

This way, you can work on new ideas without affecting the main codebase.

  • Why? Branches let you test and develop independently, and make collaboration safer.
  • Name branches clearly: For example, feature/login-form or bugfix/user-auth.

Example

git checkout -b feature/login-form
Switched to a new branch 'feature/login-form'
Try it Yourself »

Pull Before You Push

Always git pull before pushing.

This updates your local branch with changes from others, helps you avoid conflicts, and ensures your push will succeed.

Why? If someone else has pushed changes since your last pull, your push may be rejected or cause conflicts.

Pulling first lets you fix any issues locally.

Example

git pull origin main
git push origin main
Try it Yourself »

Review Changes Before Committing

Use git status and git diff to review your changes before you commit.

This helps you catch mistakes early.

Example

git status
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:   auth.js

no changes added to commit (use "git add" and/or "git commit -a")
git diff
diff --git a/auth.js b/auth.js
index a404cd3..be27e13 100644
--- a/auth.js
+++ b/auth.js
@@ -1 +1 @@
-function login(user) { return user.name !== ''; }
+function login(user) { return user.name.trim() !== ''; }
Try it Yourself »

Keep Repositories Small

Avoid adding large files or unnecessary dependencies.

This keeps your repository fast and easy to clone.

  • Tip: For large files (like videos or datasets), use Git LFS (Large File Storage) instead of adding them directly to your repo.

Use .gitignore

Exclude files that shouldn't be tracked (like build artifacts, log files, or secrets) by adding them to a .gitignore file.

Note: .gitignore only prevents new files from being tracked.

Files already tracked by Git will remain in the repository until you remove them with git rm --cached <file>.

Example: .gitignore

# .gitignore
node_modules/
*.log
.env
Try it Yourself »

Tag Releases

Use tags to mark release points (like v1.0) so you can easily find and reference important versions.

This helps you keep track of your project's history and make it easier to roll back to previous versions if needed.

Example

git tag v1.0
git push origin v1.0
Try it Yourself »

Note: Good Git habits make it easier for your team (and your future self) to understand and build on your work.




Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.