Skip to content
deepaksama edited this page Sep 7, 2022 · 2 revisions

Git

Introduction

What is Version Control?

Version control, also known as source control, is the practice of tracking and managing changes to software code.

What is Git?

Git is a free and open-source distributed version control system. It is designed to handle everything from small to very large projects with speed and efficiency.

Setup

Installation

Different software can be installed on different operating systems.

  • On Mac, Linux, and centOs operating systems we can use their package managers to install the software. We need to use sudo to execute these commands
    • Mac : sudo brew install git
    • Linux: sudo apt-get install git
    • centOS: sudo yum install git
  • Windows: On widows download git from https://git-scm.com/downloads and click on the installer and follow instructions to finish the installation.

Note: Follow Installations to get more details about installations in general for different operating systems

Verification

To verify whether the installation run the below command and check the output: git --version

Configuring

To track who made the change git identifies the users by their identity. A user is identified by his name and email. Run the below commands to configure the identity:

  • Syntax:

    git configure user.name "<Name>"

    git configure user.email "<Email>"

  • Example:

    git config user.name "Deepak Reddy Sama"

    git config user.email "deepak.sama26@gmail.com"

Note: To configure for all the projects, use --global in the above command. Ex. git configure --global user.name "Deepak"

Terminology

  • Commit :
  • Head

Architecture

Architecture

Commands Reference

Working on the local repository

Basic commands

git command Description
git init Turns the current working directory to the git repository.
git init <repository> Creates the repository with the given name <repository>
git status Shows the status of the current working directory or workspace or working tree
git add <file/files> Adds given file or files to staging area i.e it starts tracking file/files
git commit Opens an editor to provide comments for change and record changes to the local repository with given comments
git commit -m " comments " Moves all the staged files to the local repository with given comments
git rm <file/files> Untracks the given file/files and deletes file/files from file system
git rm cached <file/files> Untracks the given file/files and but do not delete file/files from file system
git log Show commit logs in a repository in sequential order with details
git log ---online Show commit logs in a repository in sequential order with details in a single line

Diff commands

git command Description
git diff <file/files> Show changes between working tree file/files and staged file/files
git diff HEAD <file/files> Show changes between working tree file/files and committed file/files
git diff --staged HEAD <file/files> Show changes between staged file/files and committed file/files
git diff <commitid> <file/files> Show changes between working tree file/files and file/files from specified commit id

Branch commands

git command Description
git branch Lists all the branches. Show * in front of the current or active branch
git branch <branch-name> creates a branch with the given branch name
git checkout <branch-name> Switch to branch branch-name
git checkout -b <branch-name> Creates and Switches to branch branch-name
git checkout <branch-name> Switch to branch branch-name
git branch -d <branch-name> deletes a branch with the given branch name

Working with the remote repository

git command Description
git clone <repository> Clone a remote repository into a new directory
git clone <repository> Clone a remote repository into a new directory
git pull Incorporates changes from a remote repository into the current branch
git push Updates remote repository with local repository changes
git push Updates remote repository with local repository changes

Note: To work with a remote repository you need to establish a secured shell authentication mechanism

Clone this wiki locally