Understanding The Git Workflow: Working Directory, Staging, Commit and Push
Imagine working on a project for several weeks. You make hundreds of changes across dozens of files. Then your computer crashes. Which version of your project was working yesterday? What did you change? Which changes did your colleague make? And how do you combine everyone's work without overwriting each other's changes? This is where Git comes in. What is Git? Git is an open-source system that…
Git is a widely-used open-source system that helps users manage projects, from a single file to large-scale ones. It is essentially a version control system capable of tracking files, identifying changes, assigning owners, and timestamps to those modifications. Moreover, Git provides an added layer of security by enabling users to duplicate their work in a remote repository, known as a "repository" on platforms like GitHub.
The Git workflow consists of several key stages, including the working directory, staging area, commit, and push. In this article, we will break down these steps to provide a clear understanding of how to navigate Git.
To begin, create a folder on your computer to store your project. For instance, on Windows, right-click and choose "New Folder," or in terminal, use the command `mkdir new_project`. Once the folder is created, open it in your terminal or text editor.
Next, initialize Git within the project folder using the command `git init`. This creates a local Git repository within your project folder. You can confirm the initialization by checking for a `.git` folder inside your main folder. Upon initialization, you will be on a default branch called "main" or "master". To verify this, run the command `git branch`.
Now let's create a file in your main folder. Using the terminal, create a file named `main.py` by entering the command `touch main.py`. To confirm your actions, run the command `git status`, which displays information about files not staged for commit, modified files, staged files, and more. At this point, you can write a line of text in `main.py` using the command `echo "print('This is a new file') > main.py`.
Now, it's time to stage your files. Staging a file tells Git that you want to commit your modifications. You can stage all files using `git add .`, or individual files using `git add filename`. After staging, proceed to commit your changes. This creates a snapshot of your current work, enabling you to revert to a particular point in your branch if needed. To commit, use the command `git commit -m "Add main Python file"`. Make sure to include a descriptive commit message within double quotes.
Finally, link your local repository to your remote repository on GitHub. First, create a repository on GitHub by clicking the "New Repository" button on your account page. Provide a name, description, and decide whether your repository should be public or private. Once created, copy the repository link.
Return to your terminal and enter the command `git remote add origin repository_link`. This links your local repository to the remote repository on GitHub. To push your changes to GitHub, execute the command `git push -u origin main`. This sends all your commits to the remote repository and establishes a connection between your local and remote repositories.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.
This story
This is one outlet's version. Read the fullest account.