Git Commands Workflow

Learn via video courses
Topics Covered

Overview

Git is a version control system used to track changes in computer files. The git commands workflow is to initialize a repository/ clone the central repository, make changes and commit and then push new commits to the central repository.

Introduction

In software development, Git is generally used for managing source code. It provides a platform for several developers to work together on a project.

It allows multiple developers to work together. Because it has thousands of parallel branches, it supports non-linear development.

The Centralized Workflow provides the basis for further Git workflows. It's common for developers to push and pull code from some centralized repository in Git workflows. One of the other popular Git workflows will be discussed below. As a result of these extended workflows, you can manage features, hot fixes, and eventual releases using more specialized patterns.

Feature Branching is a logical extension of Centralized Workflow. The Feature Branch Workflow emphasizes the importance of feature development occurring in a dedicated branch rather than the main branch. Due to this ' encapsulation ', multiple developers can work on one feature without disturbing the main codebase.

Git workflow

Git workflow

The Git workflow is divided into three states:

  • Working directory - In this state, you can modify your files in your current working directory.
  • Staging area (Index) - In this state, you can stage your files and put snapshots in your staging area.
  • Git directory (Repository) - In this state, you can make a commit that stores the files permanently in your Git directory.

Git workflow 2

How Git Workflow Works?

A blob, or binary large object, is what it uses to store file contents. There is a difference between blobs and files in that blobs are simply binary streams while files also contain meta-data.

In Git, a directory is equivalent to a tree. A tree is a directory listing, including blobs and other trees.

In Git, a commit is a snapshot. Commit objects to contain information about the committer, commit message, and commit time as well as a pointer to the main tree (the root directory). Rather than just diffs from previous commits, every commit contains the entire snapshot. If an object does not change, we do not store its data.

A branch is simply a name for a commit. So when creating a new branch using git branch "name", you're creating another pointer. The pointer created by git branch name points to the same commit we are on now.

Git Init

An empty Git repository is created by running the command git init.

A .git folder and some subdirectories are created after running git init in the directory. Following the initialization of the repository, other files are created.

Syntax

Example

Git workflow 3

We created a new git instance for our project in the above example.

Add

The git add command tells Git to start tracking the files you've made changes into. Git now tracks the changes after adding the files to its "staging area" through git add.

Syntax

If you want to add all your files, then use the following:

Example

Git workflow 4 We added all the files to the staging area in the above example.

Commit

When you change a file, you add it to the staging area. Next, you need to take a snapshot to store your changes forever. Here, you commit!

The commits are recorded as a snapshot of the full repository, along with the contributor's name, timestamp, and message. The -m flag adds a message on the command line. A message is mandatory to provide so that you can remember the reason for your commitment in the future.

Syntax

Example

Git workflow 5

In the above example, we have changed two files and are committing those changes with the message "initial commit" so you know the reason for your commit.

Status

The git status command allows you to check the current status of your project, which means that it tells about the current changes made to the project.

Syntax

Example

Git workflow 6 In the above example, we can see the git status command gives us the current state of our project.

Branch

When you use the git branch command, you can determine on what branch the local repository is located. The git branch command can be used to add and delete a branch.

Syntax

Create a new branch using

List all remote or local branches using

Delete a branch using

Example

Git workflow 7

In the above example, we first created a new branch called to test and then tried listing all remote or local branches using git branch -a.

Merge

To integrate the branches, the git merge command is used. Using this command, you can combine the changes between the two branches.

Syntax

Example

Git workflow 8

Rebase

In rebasing, a sequence of commits is merged or moved into a new base commit. Visualizing rebasing as part of a feature branching workflow is most useful and easiest.

Syntax

Push

The git push command sends the changes to the repository. These changes are currently in your local machine, and you must add them to your GitHub repository.

Syntax

Example

Git workflow 9

Pull

When you use the git pull command, you are fetching and merging changes from a remote repository to a local one. When you run the command "git pull origin master", all the files in the remote repository's master branch are copied to the local repository.

Syntax

Example

Git workflow 10

Git Remote Add Origin

You can connect to other repositories using the git remote command. This is not a direct link to another repository but a bookmark with convenient names that can be used to refer to it.

Syntax

Example

Set up a connection between your local repository and your remote repository.

Git workflow 11

Conclusion

  • In the beginning, we discussed that Git workflows and Git can be used in various ways.
  • In addition, Git requires much understanding, but you will learn more about it over time.
  • You'll learn several topics, including version control systems, what Git is, how Git works, and branches and commands in Git.