How Git Works: A Visual Guide with Code

--

image source:https://scontent.fsif1-1.fna.fbcdn.net/v/t39.30808-6/397397678_731719435667103_7943289361016472044_n.jpg?_nc_cat=106&ccb=1-7&_nc_sid=5f2048&_nc_ohc=GiWXr27Bwi0AX9jLohB&_nc_ht=scontent.fsif1-1.fna&oh=00_AfBzyaNyoPjlXcWnci0T_n7nFcxYu6Bv-FqBln2l93KwiQ&oe=655FFF31

Git is a distributed version control system that allows you to track changes to your code over time. It is used by millions of developers around the world to manage their projects.

This Medium post will explain the basics of how Git works, using a visual diagram and code.

Diagram Overview

The diagram above is a simplified version of the Git workflow. There are two main parts in the diagram: the remote repository and the local repository.
The remote repository is where the code is stored centrally. This could be on a server like GitHub, GitLab, or Bitbucket.

The local repository is a copy of the remote repository that is stored on your computer. This allows you to work on your code offline and commit changes when you are ready to push them to the remote repository.

Git Workflow

The Git workflow is a series of steps that you follow to track changes to your code. The basic workflow is as follows:

  1. Clone the remote repository to your computer. This will create a local copy of the repository.
  2. Make changes to the code in your local repository.
  3. Stage the changes that you want to commit.
  4. Commit the changes to your local repository.
  5. Push the changes to the remote repository.

Code in IDE

The following code shows how to perform the basic Git workflow in an IDE like Visual Studio Code:

This command creates a new Git repository in the current directory, establishing a local workspace for tracking changes.

Staging marks files or changes for inclusion in the next commit. You can stage individual files or use wildcards to stage multiple files.

Committing saves the staged changes to the Git repository along with a descriptive message. The message explains the modifications made in the commit.

Branches allow you to work on different versions of your code independently without affecting the main development line. This is useful for feature development or bug fixes.

Switching branches changes the current working directory to the specified branch. This allows you to work on different versions of your code seamlessly.

Merging combines changes from one branch into another. This is typically done when you want to integrate feature work back into the main development line.

Pushing sends your local commits to a remote repository, such as GitHub or GitLab. This makes your changes accessible to other collaborators.

Pulling fetches the latest changes from the remote repository and merges them into your local repository. This ensures you have the most up-to-date code from your collaborators.

Conclusion:

Git is a powerful tool that can help you to manage code changes more effectively. By understanding the basics of how Git works, you can streamline your development workflow and collaborate with others more easily.
Additionally, don’t forget to push and pull changes regularly to stay in sync with your collaborators and maintain a consistent codebase.

--

--

Sujan Adhikari | @FrontEnd Developer |
0 Followers

Hi👩‍💻 This is Sujan Adhikari. I share content about UI/UX, Cloud Services and Front End Development. Follow me here to new tips and tricks.