DEV Community

Cover image for Working Directory, Staging, Commit and Push
Manu Kipkoech
Manu Kipkoech

Posted on

Working Directory, Staging, Commit and Push

Git is a version control system that tracks changes to files within the project. Rather than manually saving multiple versions of a project, Git tracks changes to allow developers to roll back to previous versions, know what has changed, and share their work. The GitHub website is a website that allows you to store online and share your Git repositories with others.
It is essential for new Git users to be familiar with the Git workflow, because many Git operations consist of making changes, staging them, committing them, and pushing them. The purpose of each step is different.

  1. The Working Directory The working directory is the actual folder on a computer where the project files are stored. Any changes to a file that a developer makes (creating, editing or deletion) are initially made in the working directory. Let us say that I have a project that includes a file named app.py. Make changes to the python code and save the file. Now Git is aware that the file has changed, but this change is not ready to commit. I can check the current state of my project using: git status That app.py has been changed may be displayed in Git. This is helpful since it enables me to view the changes in the files prior to taking on the subsequent step.
  2. The Staging Area The staging area is where I choose the changes that I want to commit. It serves as a workspace between the working directory and the repository. To stage a specific file, I can use: git add app.py To stage all changed files, I can use: git add . Staging is useful because I may not want to commit every change at once. For example, if I modified three files but only want to save changes from app.py, I can stage only that file. I can confirm what has been staged by running: git status
  3. Committing Changes A commit is a saved point of the staged changes. It stores the changes made and a message describing the reason for the changes. For example: git commit -m "Add user login functionality" Commit messages should be informative and concise. A message like "Fix database connection" is much more self-explanatory than the vague "changes" or "update". Commits are crucial because they provide a track record of the project. In case of an error at a later stage, developers can trace back to previous commits and see how the project evolved.
  4. Pushing to GitHub Once a commit is created, the changes are in the local Git repository. These are not necessarily on GitHub by default. The git push command is used to push commits to your remote repository like GitHub. For example: git push origin master In this case, the origin is usually the distant repository on GitHub, and the main is the branch that is being updated. Once you have successfully pushed, you'll be able to see the new commit and those changes on GitHub.
  5. A Real-world Git Workflow The following simple project will be used to illustrate a typical workflow. First, make my changes on my files and verify the status: git status I then set up the changes: git add . I then commit: git commit -m “Update project files”” Last, but not least, I push my commit to GitHub: git push origin master The entire process can therefore be recalled as: Working Directory → Staging Area → Commit → Remote Repository Each stage has a specific purpose. Changes I am making are in the working directory, changes in the staging directory are changes I wish to save, and pushing copies the commits to GitHub. This is an important workflow because of the following reasons: The Git workflow assists developers to work organized and controlled. Developers are not required to push each change straight to GitHub, but can check their work, choose certain changes and establish meaningful checkpoints. In this case, if I'm writing a data engineering project, I might write a python script, test it, stage it, and commit it with the message "Add data cleaning script". I may make another commit later if there's a chance to make the script better. This leaves a distinct record of the development of the project. Conclusion Focusing on the Git workflow as a set of steps helps make it easier to understand. Changes start in your working directory, selected changes are moved to the staging area, changes in staging area are saved in a commit and a commit is pushed to GitHub. The basic process is: git status git add . git commit -m "Write a description of the changes made" git push origin master The more you do this, the easier Git is to use. Additionally, it has the ability to keep track of project advancement, interact with various other programmers, and track changes.

Top comments (0)