DEV Community

Peter Maina
Peter Maina

Posted on

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

Git is a version control system used in data and software related projects. It helps the analysts and engineers in tracking the changes of their projects, collaborating with other engineers and maintaining the history of their projects.
The git workflow can be categorized into four main stages:

  1. Working Directory
  2. Staging Area
  3. Commit
  4. Push

Knowing how to use the above stages makes it easier for you to manage projects using GitHub platform and git.

Working Directory

It is a folder in your local repository where the project files are located. If you have a folder called Weather_Project_Analysis. Inside the folder, you can have project files such as:

  • Weather_statistics.csv
  • README.md
  • Weather_analysis.sql

Git tracks the changes and manages the history of these files.
First command;

pwd
Enter fullscreen mode Exit fullscreen mode

which means Print Working Directory. It helps you locate your project folder in your local computer before running git commands.

Git status
Enter fullscreen mode Exit fullscreen mode

It checks the status of your project. Which projects are untracked and modified.

Git status on untracked files

The above screenshot shows the output after running a git status command. It shows that my files are untracked by git and the changes only exist in my working directory.

Staging Area

A staging area is a preparation area where you tell git which files to be included for tracking changes.

To add a README.MD file to the staging area use:

git add README.md
Enter fullscreen mode Exit fullscreen mode

To add several files in your staging area, you add by specifying their names;

Git add README.md weather_analysis.sql weather_statistics.csv
Enter fullscreen mode Exit fullscreen mode

To add all the files use the command;

Git add .
Enter fullscreen mode Exit fullscreen mode

Now we have staged the files, lets now check the status.

Adding all the files in the staging area

The screenshot above shows that our files are ready to be committed. The staging area is very important since it controls which files you want to be committed.

Commit

It is a saved snapshot of your project at a particular point so that git remembers how your project looked like at that particular time.
Now you can create a commit of your changes in the staging area using:

Git commit -m ”Add my weather project files”
Enter fullscreen mode Exit fullscreen mode

The text inside your double quotation marks is called a commit message. A commit message describes which changes were made. At this point, the changes are now saved in your local git repository.

Commiting your changes

The above screenshot shows that your changes are committed and saved in your local git repository.

Push

The last stage is publishing your commit to a remote repository such as GitHub, GitLab etc. A remote repository is an online storage that allows you to back up your project files. Allows you to access your work from any other computer and also collaborate with other engineers and developers.
To publish your changes to GitHub, use the command;

Git push origin main
Enter fullscreen mode Exit fullscreen mode
  • Git push; pushes your commits to an online repository.

  • Origin; It’s the remote repository.

  • Main; It’s the branch you are pushing your commits to.

After a successful push, your changes will be published on GitHub.

Pushing files to a remote repository
The git workflow can be simplified as follows:

Working Directory


git add


Staging Area


git commit


git commit


Local Repository


git push


Remote repository (GitHub)

The commands move your changes from one point to another.

Conclusion

Git workflow is a critical skill for any software developer/engineer, data analyst/engineer or data scientist. The git workflow follows a simple sequence.

Working Directory -> Staging Area -> Commit -> Push

A working Directory is where you make changes, staging area is where you want your changes to be saved, a commit records your changes in your local repository and a push publishes your commits to a remote repository such as GitHub.

Top comments (0)