Git Workflow Commands Overview
Working Directory – Where you edit and create files.
- git add – Stage your changes to be committed.
- git commit – Save your staged changes to the repository history.
- git push – Upload your commits to a remote repository to share with others.
- git status – See the current state of your working directory and staging area.
Undo / Amend – Correct mistakes:
- git restore – Undo changes in your working directory.
- git reset – Unstage changes or move commits.
- git commit --amend – Edit the most recent commit.
Understanding the Git Workflow
Git follows a distributed workflow, allowing you to make changes locally, stage them, and commit them to your own repository before sharing with others.
Mastering this workflow helps you manage your code effectively.
The Three Key Areas in Git
- Working Directory: Where you actively edit and update your project files.
- Staging Area (Index): Where you review and prepare selected changes that you plan to commit.
- Repository: Where your committed snapshots of the project history are safely stored.
Workflow Overview
css
[Working Directory] -- git add --> [Staging Area] -- git commit --> [Repository]
Working Directory
This is your workspace—where you create, edit, or delete files.
At this stage, Git is only aware of the changes, but they aren’t saved to your project history yet.
Staging Changes (git add)
When your changes are ready, you stage them using git add.
This moves them to the Staging Area, like putting your draft into an envelope before sending.
# Stage a specific file
git add index.html
# Stage all changes (new, modified, and deleted files)
git add .
Committing Changes (git commit)
Committing saves your staged changes to your local repository.
It’s like mailing your letter—you’ve made it official, and it becomes part of your project history.
# Commit staged changes with a message describing what you did
git commit -m "Describe your changes"
# Shortcut: stage and commit modified & deleted files (but not new files)
git commit -a -m "Describe your changes"
Pushing Changes (git push)
After you commit, your changes only exist in your local repository.
Use git push to upload these commits to a remote repository (like GitHub, GitLab, or Bitbucket) so others can see and collaborate on your work.
Example:
git push
Checking Status (git status)
Use git status to check which files are:
- Staged for commit
- Modified but not staged
- Untracked (new files Git isn’t yet tracking)
This keeps you aware of what’s going on before you commit.
Example:
git status
Undoing and Amending Changes
Git makes it easy to fix mistakes before you push your commits.
Undo changes in the working directory (before staging):
git restore <file>
- Unstage a file (keep changes, but remove from Staging Area):
git restore --staged <file>
- Undo the last commit (keeps changes in your working directory):
git reset HEAD~
- Amend the last commit (edit message or add more changes):
git commit --amend
Example: Unstage a file
git restore --staged index.html
Tips & Troubleshooting
- Use git status frequently to check the current state of your repository.
- Committed something by mistake? Before pushing, fix it with git reset or git commit --amend.
- Be selective when staging: use git add <filename> to stage only the files you actually want to commit.
- Remember to run git push after committing, so your changes appear on the remote repository for others to see.
- When in doubt, don’t hesitate to ask for help or search for the error message—mistakes happen to everyone!