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 Workflow


Share

Git Workflow Commands Overview

See Also: GitHub Flow is a popular collaborative workflow for teams using GitHub.

If you work with GitLab or Bitbucket, those platforms have their own workflows too. Learn more about GitHub Flow »


Understanding the Git Workflow

Git uses a distributed workflow that allows you to work on your code, stage changes, and commit them to your local repository before sharing with others.

Understanding this workflow is essential for effective version control.

The Three Areas of Git

  • Working Directory: Where you make changes to your files.
  • Staging Area (Index): Where you prepare changes before committing.
  • Repository: Where your committed history is stored.

Workflow Diagram

[Working Directory] --git add--> [Staging Area] --git commit--> [Repository]
  

Best Practices for Git Workflow

  • Commit frequently with clear, meaningful messages.
  • Check your status often with git status to avoid surprises.
  • Stage only what you intend to commit. Use git add <file> for precision.
  • Push regularly to back up your work and share with others.
  • Review your changes with git diff before committing.


Working Directory

This is where you make changes to your files.

Think of it as your workspace or desk.

Files here can be new, modified, or deleted, but Git won't save these changes until you stage and commit them.


Staging Changes (git add)

When you are happy with your changes, you "stage" them with git add.

This puts your changes in the Staging Area, like putting your finished letter in an envelope.

Example

git add index.html
Try it Yourself »

To stage all changes (new, modified, and deleted files):

git add .
Try it Yourself »

Committing Changes (git commit)

Committing saves your staged changes to your local repository.

It's like mailing your letter: you can't change it after it's sent!

Example

git commit -m "Describe your changes"
[main 291a117] Describe your changes
 1 file changed, 1 insertion(+)
Try it Yourself »

You can also use git commit -a -m "message" to stage and commit all modified and deleted files in one step (but not new files).


Pushing Changes (git push)

After you commit, your changes are only in your local repository.

Use git push to send your commits to a remote repository (like GitHub or Bitbucket) so others can see them.

Example

git push
Try it Yourself »

Checking Status (git status)

Use git status to see which files are staged, unstaged, or untracked.

This helps you keep track of what you still need to add or commit.

Example

git status
On branch main
Changes to be committed:
  (use "git restore --staged &lt;file&gt;..." to unstage)
        modified:   index.html

Changes not staged for commit:
  (use "git add &lt;file&gt;..." to update what will be committed)
  (use "git restore &lt;file&gt;..." to discard changes in working directory)
        modified:   style.css

Untracked files:
  (use "git add &lt;file&gt;..." to include in what will be committed)
        app.js
Try it Yourself »

Undoing and Amending Changes

Made a mistake? Git lets you fix things before you push!

  • git restore <file> - Undo changes in your working directory (before staging).
  • git restore --staged <file> - Unstage a file (move it out of the Staging Area).
  • git reset HEAD~ - Undo your last commit (keeps changes in your working directory).
  • git commit --amend - Change the last commit message or add files to your last commit.

Example: Unstage a file

git restore --staged index.html
Try it Yourself »

Tips & Troubleshooting

  • Use git status often to see what's going on.
  • If you commit the wrong thing, use git reset or git commit --amend before pushing.
  • Stage only what you want to commit. Use git add <filename> for specific files.
  • Don't forget to push after committing, or your changes won't show up for others.
  • If you're not sure, ask for help or look up the error message. Everyone makes mistakes!



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.