ACES

Git Basics: Comprehensive Guide for Installation, Configuration, and Workflow

Learn the essentials of Git in this comprehensive guide. Explore installation steps, basic commands, repository management, and workflow practices to effectively handle version control in your projects.

user
Sandip Sapkota

Fri, Jul 5, 2024

3 min read

thumbnail

What is Git?

Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. A version control system tracks changes to files or sets of files over time, allowing you to review specific versions later. Unlike central VCS, a distributed VCS has both remote and local repositories, eliminating the need for constant backups.


Installation

  1. Download Git
    Get Git from git-scm.com.

  2. Enter Configuration Values
    Set your name and email:

    git config --global user.name "Your Name"
    git config --global user.email "your.email@example.com"
    
  3. Check Your Settings
    Verify your configuration:

    git config --list
    

Help

To open Git’s manual and display information:

git help -a

or

git help --all

– Lists all available commands.

git help config

– Explains how to use the config command.

git help -g

or

git help --guide

– Shows a list of Git guides.


Creating a New Repository

Initialize a New Repository:

git init

Clone a Remote Repository:

git clone /url/

Staging Area

Add or Propose Changes:

git add /file/

Add All Changes:

git add .

Remove Changes:

git reset /file/

Commit

Show Changes:

git diff

– Displays changes made.

Commit Changes:

git commit -m "Detailed message explaining the nature of changes"

Push Changes to Remote Repository:

git pull origin master
git push origin master

Workflow

A Git project consists of three main sections:

  1. Working Directory
  2. Staging Area / Index
  3. HEAD

Files have three main states:

  1. Committed
  2. Modified
  3. Staged

Branches

A Git branch is a movable pointer to commits. The default branch is called master.

Create a New Branch:

git checkout -b newBranch

Delete a Branch:

git branch -d newBranch

Switch Back to master:

git checkout master

Push to Remote Repository:

git push origin /branch/

Merge a Branch into the Current Branch:

git merge /branch/

Logs

View Recent Commits:

git log

– Displays commit history with details.

Cancel Local Changes:

git checkout -- /file/

or

git fetch origin

– Fetches and integrates changes from the remote repository, canceling local commits.