Skip to main content

Command Palette

Search for a command to run...

Git and git Basics

Updated
•3 min read•View as Markdown

Git is a distributed version control system used to track changes in files, especially source code, during software development.
Git allows developers to:

  • Save different versions of your code

  • Go back to an older version if something breaks

  • Work with multiple developers on the same project

Reasons to Use Git

  • Git keeps a complete history of all changes made to the code.
    You can easily see what was changed, who made the change, and when it was done.

  • Git allows multiple developers to work on the same project at the same time.
    Each person can work independently without overwriting others’ work.

  • If something goes wrong or code breaks, Git allows you to restore a previous working version of the project easily.

  • Git allows developers to create branches to work HEAD:on new features or bug fixes.
    This keeps the main code safe until the feature is ready.

  • Most Git operations, like committing and viewing history, can be done without an internet connection.

Git Basics and Core Terminologies

  • Repository (Repo): A repository is a collection of files and directories that are stored together. It is a way to store and manage your code. A repository is like a folder on your computer, but it is more than just a folder. It can contain other files, folders, and even other repositories. You can think of a repository as a container that holds all your code.

  • Working Directory: The actual local files on your computer that you are currently editing. This is where you make changes before staging them.

  • Staging Area (Index/Cache): An intermediary layer where you prepare a snapshot of changes before committing them to the project history. It allows you to select which specific changes will be included in the next commit.

  • Branch: An independent line of development. Branches serve as an abstraction, allowing developers to work on new features or bug fixes in isolation without affecting the main codebase (commonly called the main or master branch).

    HEAD: The HEAD is a pointer to the current branch that you are working on. It points to the latest commit in the current branch. When you create a new branch, it is automatically set as the HEAD of that branch.

  • Remote: A connection (alias) to a repository hosted on a different machine or a web service. The conventional name for the primary remote is origin.

Common Git Commands

  • git init: Initializes a new Git repository in the current directory.

  • git clone: Creates a local copy of an existing remote repository, including all its files, history, and branches.

  • git add: Moves changes from the working directory to the staging area, marking them for inclusion in the next commit.

  • git commit: Saves the staged snapshot to the project history with a descriptive message.

  • git status: Shows the state of the working directory and the staging area, indicating which files are untracked, modified, or staged.

  • git push: Uploads local commits to a remote repository (e.g., to GitHub).

  • git pull: Fetches changes from a remote repository and immediately merges them into your current local branch. It's a combination of git fetch and git merge.

  • git fetch: Downloads branches and commits from a remote repository but does not automatically integrate them into your current working files, allowing you to inspect changes first.

  • git merge: Integrates changes from a separate branch into your current active branch.

  • git checkout: Used to switch between branches or restore files to a previous state.

  • git branch: It can list existing branches, create new branches, rename branches, and delete branches.