DEV Community

Waweru
Waweru

Posted on

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

Introduction

Git is a version control system that tracks changes in code. When writing code we need to save every significant change or feature that does a certain function as a commit that can be reverted to if we ever need that instance of the code. This helps programmers to traverse code since nothing is ever lost in the product life cycle of the project. It also helps in debugging since we have a copy of the previous working state. So how do we start out with git you ask.

Install git

  1. Install git for your specific operating system. After installing git you will note that the file comes with git bash which is a command line terminal that is used to run commands or instruct the version control git.
  2. To ensure that git has been installed open git bash and run the command git --version which will return the version of git installed

Download Visual Studio Code

  1. Install VS code a text editor for writing code.

Create a github account

GitHub is a cloud-based platform used by developers to store, track, and collaborate on software code. It operates as a hosting service for git, an open-source version control system that tracks changes made to files.

  • Head over to github and create and account

Creating a directory tracked by git

  1. On git bash run mkdir <filename>. This will create a folder ie the project that will contain your files of code.
  2. Enter the folder through cd <filename> and create a file README.md file which explains what this project does touch README.md. Run the command ls to list files in the directory
  3. Open the vs code from terminal by running code . and edit the README.md file. Now the file is ready to be tracked by git on github

Staging, committing and pushing

We now need to initialize the directory making sure that git is now tracking the file. Running the commands ls-la will confirm that git has been initialized in the repository. There are two ways of doing this either via the text editor VS code or the terminal. Let us go through both.

1. VS code

Make sure your VS code is connected to your github account.

  1. Click the source control icon (third from top) and Initialize Repository. This will introduce git to track the file
  2. Hover over the file and click the + button to add the file. This changes the file from U (untracked) to A (added)
  3. Write the commit message in the input box above describing what you are committing eg add README.md file.
  4. On the commit button press the drop down arrow **for more options and **press commit and sync. This will now stage your repository to be pushed to github.
  5. Click on publish branch and set the repository to either public or private.
  6. Go to your github and refresh to see your newly pushed repository that is now tracked by git.

2. Using the terminal

  1. Open your terminal ie. gitbash and go to your folder.
  2. Initialize Git using git init.
  3. Add your files using git add . to track them.
  4. Commit the files using git commit -m "first commit". This should describe what we are committing and will to save the change locally.
  5. Link your remote repository using git remote add origin <your-repo-url>.Preferably use the ssh link option
  6. Push the files using git push -u origin main to send your code to GitHub and publish the repository as the main branch.

With that now you know how to set up a project and keep track of your changes using github.

Top comments (0)