DEV Community

Janet Kangogo
Janet Kangogo

Posted on

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

Git is used to track changes made to files in a project. The basic Git workflow has four main parts:

Working Directory → Staging Area → Commit → Push

A simple project can contain different types of files, for example:

  • hello.py
  • practice.sql
  • README.md

The same Git workflow can be used for all these files.

Working Directory

The working directory is where the project files are created and edited.

For example, hello.py can contain:

A SQL file can contain:

And README.md can contain:

When a file is changed, Git can detect the change.

The current state of the project can be checked using:

git status
Enter fullscreen mode Exit fullscreen mode

It can show modified and untracked files, staged and unstaged changes, as well as the current branch.

The output can show something like:

Which means the file has been changed, but the change has not been staged yet.

Staging Area

The staging area is used to select the changes that should go into the next commit.

To stage hello.py:

git add hello.py
Enter fullscreen mode Exit fullscreen mode

On checking git status, the output can show something like:

More than one file can also be staged:

git add hello.py practice.sql
Enter fullscreen mode Exit fullscreen mode

To stage all the changes:

git add .
Enter fullscreen mode Exit fullscreen mode

Note that: Not every change has to be included in the same commit.

Commit

After staging the required changes, a commit can be created:

git commit -m "Your Commit Message"
Enter fullscreen mode Exit fullscreen mode

A commit records the staged changes in the local Git repository.

The message after -m describes the change.

The commit history can be checked using:

git log
Enter fullscreen mode Exit fullscreen mode

This shows the commits that have already been made.

Create a repository on Github

A repository should be created on GitHub where the project would be pushed.
The GitHub repository acts as the remote repository for the project.

On GitHub, New repository or the “+” icon was selected, then a name and description were added before creating the repository.

Push

A commit is stored in the local repository. It does not automatically go to GitHub.

Push: Sends committed changes from the local repository to the remote repository, such as GitHub, using git push or git push origin main to specify the remote (origin) and branch (main).

End Result

When you refresh git hub, the repository is updated

The Full Workflow

The Git workflow can be shown as:

Working Directory > git add > Staging Area > git commit> Local Repository > git push > Remote Repository (GitHub)
Enter fullscreen mode Exit fullscreen mode

Conclusion

The Git workflow shows how changes move from the working directory to GitHub. The changes are checked using git status, added using git add, committed using git commit, and then pushed using git push.

Top comments (0)