DEV Community

Cover image for Understanding the Git Workflow: From Local Folder to Remote Repository
Clarence Gatama Chege
Clarence Gatama Chege

Posted on

Understanding the Git Workflow: From Local Folder to Remote Repository

For quite a while I have used Git without knowing what happens behind the scenes. I mastered using all commands from initializing, adding, committing to pushing code to Github but I couldn't explain what was happening to the files after each command. In this article we are going to cover what Git is, the git commands used when working in a Git repository and the four areas Git uses to track and manage your files.

Git and Github

Git and Github are not the same thing.
Git is a free, open source tool that helps you save and manage different versions of your files and code. Git sits silently in your project folder, keeping track of every change that happens.
Github on the other hand is a platform that hosts your project folders and repositories online. This way other people can access your code, collaborate with you or review your work.

Git Repository

A Git repository is a folder that Git lives in and tracks for changes, this includes, every addition, deletion, and edit made in that folder. To initialize a repository (to make Git track your folder), you use the command git init.

This is how it works. You have a folder article that has a file README.md and a folder first-article that you will be working on.Right now, this is just a standard folder on your computer. Git has no idea it exists.

Folder article

Once you run git init, Git creates a hidden .git folder inside it. From this point on, your article folder serves as your Working Directory.

Initializing git

Working directory

The Git Workflow

In Git, files move through four distinct workflow areas or stages. Lets look into each stage:

1. The Working Directory
This is the folder where Git has been initialized and where you make changes to your files, in our case the article folder. New or updated files cannot be tracked by Git until you stage and commit them. Using git status we can check the current state of your working directory. For example in our current working directory, git status shows we have a file README.md that is untracked meaning Git is aware of the file but it is not currently keeping a record of its changes.

git-status

2. Staging Area
To make Git track the different versions of our files we use the command git add. Git add takes a copy of your current files from your working directory to the staging area.
In our example, lets make changes to README.md file and also add a new text file in folder first-article. These changes will still be untracked until we use the git add command as in the image below.

git-article-5

To make git track this changes we use the git add command. In this case we can run either:
git add README.md first-article/ to add the two or we run git add . to add all the untracked files.

Now when we run git status we'll see that git can now track our files and can confirm that README.md & first-article/ have changes to be committed.

git add . image

3. Local Repository
The git commit command takes the snapshot from your Staging Area and moves it into the hidden .git folder. The git commit command includes a message that describes what the commit is about
git commit -m "Commit message"
Running git commit packages your changes and outputs exactly what was saved. Running git status and seeing “nothing to commit, working tree clean” means the staging area is empty, and all your current changes have been committed and safely backed up.

git commit

4. Remote Repository
After committing, your changes are saved only in your local Git repository. Use the git push command to upload those commits to a remote repository, such as GitHub or Bitbucket, making them accessible to others.

git push

Conclusion
In conclusion, understanding Git’s four tracking areas gives you a clearer picture of how your project files are managed. Instead of running commands without knowing what happens behind the scenes, you can now understand how your work moves from the Working Directory to the Staging Area, then to the Local Repository, and finally to the Remote Repository, giving you control over your project history and ensuring your work is safely backed up. One important advantage of Git is that you can access previous versions of your work. Overall, Git provides a reliable and organized way to manage, track, and protect your projects.

Top comments (0)