GIT, Deployment

Learn the Basics of Git in Under 10 Minutes

Learn the Basics of Git in Under 10 Minutes is possible ? Git is a distributed version control system that tracks changes in any set of computer files, usually used for coordinating work among programmers who are collaboratively developing source code during software development. Its goals include speed, data integrity, and support for distributed, non-linear

If you want to get started on learning about Git technology, you’ve come to the right place. This is a comprehensive beginner’s guide to Git. There are many clients for Git. The technology is all the same no matter the client. But in this guide we’ll be using GitHub to understand Git.

Why Git ?

Git is a widely used distributed version control system (DVCS) that plays a crucial role in modern software development. Here are some reasons why Git is so popular:

  1. Distributed Version Control:
    • Git is distributed, meaning that every developer has a complete copy of the project’s history on their local machine. This allows for greater flexibility and independence among team members.
  2. Branching and Merging:
    • Git makes branching and merging straightforward. Developers can create branches to work on features or bug fixes independently, and later merge their changes back into the main branch.
  3. Flexibility:
    • Git is not tied to a specific workflow. Teams can adopt various branching strategies (e.g., Git Flow, GitHub Flow) based on their needs. This flexibility makes it adaptable to different project structures and team sizes.
  4. Speed and Performance:
    • Git is designed to be fast. Most operations in Git are performed locally, and it’s optimized for performance, making it efficient even with large projects.
  5. Security:
    • Git uses a cryptographic hash function (SHA-1) to ensure the integrity of the data. Each commit is uniquely identified by its hash, making it difficult to alter historical data without detection.
  6. Open Source and Community Support:
    • Git is open source, and it has a large and active community. This means constant improvement, updates, and a wealth of resources and third-party tools available for Git users.

Git commands:

There are numerous Git commands available for various purposes. Here is a list of some commonly used Git commands categorized by functionality:

Repository Initialization and Configuration:

  • git init: Initialize a new Git repository.
  • git clone <repository_url>: Clone a repository into a new directory.
  • git config: Configure Git settings (user name, email, etc.).

Basic Snapshotting:

  • git add <file>: Add file changes to the staging area.
  • git commit -m "message": Commit staged changes with a message.
  • git status: Show the working tree status.
  • git diff: Show changes between commits, commit and working tree, etc.

Branching and Merging:

  • git branch: List, create, or delete branches.
  • git checkout <branch>: Switch branches or restore working tree files.
  • git merge <branch>: Join two or more development histories together.
  • git log: Show the commit logs.
  • git blame <file>: Show what revision and author last modified each line of a file.

Remote Repositories:

  • git remote: Manage set of tracked repositories.
  • git fetch: Download objects and refs from another repository.
  • git pull: Fetch from and integrate with another repository or a local branch.
  • git push: Update remote refs along with associated objects.

Undoing Changes:

  • git reset: Reset current HEAD to specified state.
  • git revert <commit>: Create a new commit that undoes changes of a previous commit.
  • git checkout -- <file>: Discard changes in the working directory.

Git Information:

  • git show <commit>: Show various types of objects.
  • git tag: Create, list, delete, or verify a tag object signed with GPG.
  • git describe: Show the most recent tag that is reachable from a commit.

Collaboration:

  • git remote add <name> <url>: Add a remote repository.
  • git fetch <remote>: Download objects and refs from another repository.
  • git pull <remote> <branch>: Fetch from and integrate with another repository or a local branch.
  • git push <remote> <branch>: Update remote refs along with associated objects.
  • git merge <remote>/<branch>: Merge changes from a remote branch into the current branch.

Advanced Tools:

  • git stash: Stash changes in a dirty working directory.
  • git submodule: Initialize, update, or inspect submodules.
  • git bisect: Use binary search to find the commit that introduced a bug.

Git Help:

  • git help <command>: Display help information for a specific command.
  • git --version: Display the Git version.


Deploying a basic project with Git involves pushing your code to a remote repository and then pulling it on the server where your application will run. Here’s a simplified example assuming you have a web application:

1. Set Up a Remote Repository:

If you haven’t already, create a remote repository on a platform like GitHub, GitLab, or Bitbucket.

2. Initial Project Setup:

Initialize your local project as a Git repository, add your files, and make an initial commit.

# Initialize a new Git repository
git init

# Add all files to the staging area
git add .

# Commit the changes
git commit -m "Initial commit"

3. Connect Local Repository to Remote Repository:

Add the URL of your remote repository as the origin.

git remote add origin <repository_url>

4. Push Code to Remote Repository:

Push your code to the remote repository.

git push -u origin master

5. Set Up Deployment Server:

On your deployment server, clone the repository.

git clone <repository_url>

6. Pull Changes on Deployment Server:

When you make updates, commit and push them to the remote repository. Then, on the deployment server, pull the changes.

# Pull changes from the remote repository
git pull origin master

7. Configure Your Application:

Make sure your application is configured correctly for the production environment. This might involve setting environment variables, configuring database connections, etc.

8. Install Dependencies:

If your project has dependencies, make sure they are installed on the deployment server.

Commit vs Rebase

commit and rebase are both Git commands used for managing changes in a repository, but they serve different purposes and have distinct use cases.

Commit:

  1. Purpose:
    • The commit command is used to record changes to the repository. It creates a new commit, storing the current state of the working directory and staging area.
  2. Committing Workflow:
    • Developers typically commit changes to their local branch before pushing to a remote repository.
    • Commits create a linear history, where each commit has a unique identifier (SHA-1 hash) and a reference to its parent commit.
  3. Usage:
git add <file>
git commit -m "Commit message"

Rebase:

  1. Purpose:
    • The rebase command is used to combine a sequence of commits into a new base commit. It is often used to integrate changes from one branch into another, providing a cleaner and more linear history.
  2. Rebasing Workflow:
    • Developers use rebase to bring the changes from a feature branch into the main branch without creating a merge commit.
    • It helps in avoiding unnecessary merge commits and maintaining a cleaner commit history.

Key Differences:
History Visualization:

  • commit maintains a straightforward history with distinct commits.
  • rebase creates a linear history by integrating changes into a new base commit.

Commit IDs:

  • Each commit with commit has a unique identifier (SHA-1 hash).
  • Commits are rewritten during rebase, resulting in new commit IDs.

Use Cases:

  • Use commit for regular local commits and pushing changes to a remote repository.
  • Use rebase for integrating changes from one branch into another, creating a cleaner and more linear history.