Tasnim Zotder

Git Essentials: A Comprehensive Guide to Setting Up, Using and Collaborating with Git

Author: Tasnim Zotder
GitCloud

Introduction

Git is a distributed version control system that allows developers to manage and track changes to their codebases. Git was created in 2005 by the creator of Linux, Linus Torvalds. It provides a simple yet powerful set of tools that enables developers to collaborate with others, track their codebases, and manage different versions of the codebases.

One of the key features of Git is its capability to handle branching and merging. This allows developers to work on different features or issues in parallel without affecting the primary codebase. Git also provides robust tools for collaborations. Multiple developers can work on the same codebase simultaneously and share code with each other. This can achieved through the processes called “Pushing” and “Pulling”.

Another important aspect of Git is that it's distributed which means each developer has the full copy of the entire codebase including the history. This allows developers work offline and make commit the changes to the codebase even without internet connection.

Setting up Git

Installing and Configuring Git on computer

Setting up Git on a computer is a straight-forward process. The first step is to download and install the Git software. The software can be downloaded from the Git's official website.

Once Git is installed, the next step is to configure the Git's settings. This process includes setting the the username and email address associated with the commit. This can be done by running the following commands in the terminal.

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

To verify the settings run the following command in the terminal.

git config --list

This will output the following results in the terminal.

user.name="Your Name"
user.email="[email protected]"

Create a Git repository

The last step is to create a new repository. A repository is simply a directory where the codebase will reside and all the changes will happen. To create a new Git repository navigate to the target directory and run the following command in the terminal.

git init

This will initialize an empty Git repository on that directory.

Basic Git commands

After setting up Git on the local machine, the operation commands can be performed. Some of the regularly used basic Git commands are:

  1. git init: This command is used to initialize a new Git repository. It creates a new directory called .git in the current directory, which keeps track of all the changes in the repository.

  2. git clone: This command is used to create a copy of an existing repository from a remote server to the local machine. The remote repository might be located on GitHub, Bitbucket, GitLab etc. The syntax for cloning a remote repository is: git clone <repository URL> <directory>. Here, <repository URL> is the URL of the remote repository and <directory> is the name of the local directory where the remote repository will be cloned.

  3. git add: This command is used to add files to the staging area. Staging area is where the changes to the files are prepared to be committed. The syntax is: git add <file>. Here, <file> is the file in the local system that needs to be added. The symbol . (dot) can be used for <file> to add all the files in that directory from where the command was run.

  4. git commit: This command is used to save changes to the repository. git commit works like a checkpoint to which can be rolled back if needed. The syntax is: git commit -m "<commit message>". <commit message> is a short description of the changes.

  5. git status: This command is used to check the current status of your repository. It shows the files that have been modified, added to the staging area, or committed.

  6. git log: This command is used to view the commit history of the repository. It shows the details of each commit, such as the author, date, and commit message.

These are the basic commands a developer uses on a regular basis.

Collaborating with Git

Collaborating with Git is an essential aspect of software development, and it's made easy with the Git’s powerful tools. The most commonly used Git commands for collaboration are as follows:

  1. git pull: This command is used to retrieve changes made by other team members from a remote repository to the local machine. It fetches the changes and merges them with the local repository. The syntax is: git pull <remote> <branch>, where "remote" is the name of the remote repository and "branch" is the name of the branch you want to pull changes from.

  2. git push: This command is used to share the changes with others. It sends the commits to the remote repository. The syntax is: git push <remote> <branch>, where "remote" is the name of the remote repository and "branch" is the name of the branch to push.

  3. git fetch: This command is used to retrieve changes from a remote repository to the local machine, but it doesn't merge them. It's useful for reviewing changes before merging them. The syntax is: git fetch <remote> <branch>.

  4. git merge: This command is used to merge changes from one branch to another. It's usually used to merge changes from a feature branch to the main branch. The syntax is: git merge <branch>, where "branch" is the name of the branch to merge to.

  5. git branch: This command is used to create, list, or delete branches. It's useful for managing different versions of your codebase. The syntax is: git branch <branch name> for creating a new branch, git branch for listing all branches, and git branch -d <branch name> for deleting a branch.

References