DEV Community

Collins Muatha
Collins Muatha

Posted on

Understanding the Git Workflow: Working Directory, Staging, Commit and Push

Introduction

When I first started learning Git, commands such as' git add',
'git commit', and 'git push' seemed confusing. I later understood
that each command represents a different stage in saving and sharing
project changes.

In this article, I explain the Git workflow using a small practical
project.

What is Git?

Git is a version control system that records changes made to files.
It allows developers to track project history, return to earlier
versions, and work safely on projects.

The Git workflow

The basic workflow is:

Working directory → Staging area → Commit → Remote repository

1. Working directory

The working directory is the project folder on my computer.
When I create or edit README.md, the changes first exist here.

Example:

'''bash
mkdir git-workflow-demo
cd git-workflow-demo
'''

2. Initialize a Git repository

'''bash
git init
'''

This command tells Git to start tracking the project.

To check the status:

'''bash
git status
'''

3. Staging area

The staging area allows me to choose which changes should be included
in the next commit.

'''bash
git add README.md
'''

To stage all files:

'''bash
git add .
''''

The staging area is useful because I may have several changes in my
working directory but only want to save some of them in one commit.

4. Commit

A commit is a saved snapshot of the staged changes.

'''bash
git commit -m "Add first Git workflow demo"
'''

A good commit message should be short and meaningful.

5. Connect the project to GitHub

After creating a repository on GitHub, I connect it to my local project:

'''bash
git remote add origin git@github.com:YOUR-USERNAME/git-workflow-demo.git
'''

I can confirm the connection with:

'''bash
git remote -v
'''

6. Push changes to GitHub

'''bash
git branch -M main
git push -u origin main
'''

The 'git push' command uploads local commits to the GitHub repository.

Making another change

After editing the file again, I can repeat the workflow:

'''bash
git status
git add README.md
git commit -m "Update project explanation"
git push
'''

Common mistakes

Forgetting to stage changes

If I commit without running git add, Git may report that there is
nothing to commit.

Using an unclear commit message

A message such as changes does not explain what was changed.
A message such as Update README instructions is clearer.

Pushing before configuring the remote

If Git does not know the GitHub repository address, I must first run:

'''bash
git remote add origin REPOSITORY-URL
'''

Sharing a private SSH key

The private key must remain on my computer. Only the public key should
be added to GitHub.

Conclusion

The Git workflow is easier to understand when viewed as a sequence:

  1. Make changes in the working directory.
  2. Select changes with 'git add'.
  3. Save a snapshot with 'git commit'.
  4. Upload the commit with 'git push'.

This small project helped me understand how local work is tracked and
shared on GitHub.

Useful commands

''''bash
git status
git add .
git commit -m "Meaningful message"
git log --oneline
git remote -v
git push
''''

Top comments (0)