Skip to main content

Command Palette

Search for a command to run...

Getting Started with GIT | Basics and Essential Commands

Updated
4 min read
Getting Started with GIT | Basics and Essential Commands

Heyy there, today in this blog we are gonna discuss about everyone’s favoutite verson control system, git. Now matter you’re a complete begginer, or an expert software developer or just a geek. If you write code, then git is a life saver & time saver tool for you as a developer.

What is Git?

Git is a distributed version control system (DVCS) specifically design to manage and track changes in a project's codebase over time. It allows multiple developers working on the same project to maintain their own complete history of changes, providing a comprehensive record of who made what changes and when. This system is like a "time machine" for your code, enable developers to easily revert to previous versions if needed, compare different history of the project, and collaborate efficiently without overwriting each other's work. Git's distributed nature means that every developer has a full copy of the entire project history on their local machine, ensuring that the project is prevented against data loss and allow seamless collaboration across diverse teams.

Why Git is Used?

Git solves many real problems developers face while building software in a team (or even solo).

  1. Track Changes: See what changes are made in which files, when, and by whom.

  2. Undo Mistakes: Safely undo any mistaken changes made to any file.

  3. Work in Parallel: Multiple developers can create branches to work on different features simultaneously without waiting for each others.

  4. Merge Code: Combine code from different developers.

  5. Detect Conflicts: While merging, Git helps track conflicts in the code and resolve them.

  6. Maintain a Remote Repository: Keep a remote repository for the codebase on a local or cloud server for backup and deployments.

Git Basics and Core Terminologies

  • Repository

    A repository (or repo) is a virtual storage space for managing and storing digital assets like a codebase, data, or project files. At its core, a Git repository is the hidden .git directory located in the root of your project folder. Read this blog to learn more about the .git folder.

  • Commit

    A commit in Git is a snapshot of your project at a specific moment. Essentially, it saves the project's history at a particular time, including the staged changes and metadata.

  • Staged changes

    These are modifications in your project's files that have been marked in their current version to be included in the next commit. This is done using the git add command.

  • Branch

    A branch is like a separate workspace created from a specific commit in a repository where you can make changes. It can be merged with another branch as a commit or kept as a separate branch.

  • HEAD

    HEAD points to the current commit we are working on in a specific branch. When you make a commit, it is added on top of HEAD. When you checkout a branch, HEAD moves to that branch.

  • Checkout

    Checkout in Git is a command used to move HEAD to a different branch or commit and update your working files accordingly.

Common Git Commands

  • Initialize a new Git repository:
    git init

  • Copy an existing remote repository to your machine:
    git clone <url>

  • See the current state of files in the repository:
    git status

  • Stage file changes for the next commit:
    git add <filename> → This stages specific file for the next commit
    git add . → This stages all file changes in the repository for the next commit.

  • Save/commit staged changes to the repository history:
    git commit -m “message here”

  • Display the commit history:
    git log

  • Combines another branch into the current branch.

    git merge <branch>

  • Show differences between file versions:
    git diff → This show changes we’ve made in the files that haven't yet added to the staging area with git add .
    git diff <branch1> <branch2> → This displays all changes that are in branch2 but not in branch1.

    git diff <commit-id1> <commit-id2> → We can use commit hashes to see the differences between any two points in project's history.

  • Lists all branches in the repository:
    git branch

  • Create a new branch:

    git branch <branch-name>

  • Switches to another branch or commit:

    git checkout <branch/commit hash>

  • Create and switch to a new branch:

    git checkout -b <branch>

  • To add a remote repository in git repo:

    git remote add <remote-name> <remote-url>

    replace <remote-name> with a name for the remote (e.g., origin) and <remote-url> with the URL of remote location (eg. GitHub or GitLab).

  • Upload local commits to the remote repository:

    git push

  • Fetch and merge changes from the remote repository:

    git pull

36 views