Top Git Commands you must know | Git and Github

Top Git Commands you must know | Git and Github

12.4K views
Summary
Git is an essential tool for modern software development, enabling developers to manage and collaborate on code efficiently. In this comprehensive guide, we'll explore the top Git commands and workflows that every developer should know. From initializing a repository and committing changes to branching, merging, and working with remote repositories, we'll cover it all. Whether you're a beginner or an experienced Git user, this article will provide you with a solid foundation and valuable tips to streamline your Git workflows.

Top Git Commands and Tutorial

Introduction to Git and GitHub

Git is a powerful distributed version control system that allows developers to track changes made to their codebase over time. GitHub, on the other hand, is a web-based hosting service for Git repositories, providing a user-friendly interface and collaboration tools for teams working on projects together.

Git provides a robust set of features for managing and collaborating on code, including branching, merging, and distributed workflows. It allows multiple developers to work on the same codebase simultaneously, while keeping track of changes and preventing conflicts.

Getting Started with Git

  1. git init

    Initializes a new Git repository in the current directory. This command creates a hidden `.git` folder that contains all the repository data.

  2. git clone

    Creates a local copy of a remote repository on your machine.

    git clone <repository-url>

    This command downloads the entire repository from the specified URL, including all files, branches, and commit history.

Working with Git

  1. git add

    Stages changes to be committed. You can add specific files or entire directories.

    git add <file-name>
    git add <directory-name>
  2. git commit

    Records changes to the repository with a descriptive message.

    git commit -m "<commit-message>"

    Commit messages should be clear and concise, describing the changes made in the commit.

  3. git push

    Uploads local repository changes to a remote repository.

    git push <remote-name> <branch-name>

    The `remote-name` typically refers to the remote repository URL (e.g., `origin`), and `branch-name` specifies the branch you want to push your changes to.

  4. git pull

    Fetches and merges changes from a remote repository into your local repository.

    git pull <remote-name> <branch-name>

    It's a good practice to pull changes from the remote repository before pushing your local changes to avoid conflicts.

Branching and Merging

  1. git branch

    Lists, creates, or deletes branches.

    git branch <branch-name>

    Branches allow you to work on separate features or bug fixes without affecting the main codebase (typically the `main` or `master` branch).

  2. git checkout

    Switches between branches or restores files.

    git checkout <branch-name>

    You can also use this command to create a new branch and switch to it simultaneously:

    git checkout -b <new-branch-name>
  3. git merge

    Combines changes from one branch into another.

    git merge <branch-name>

    When merging branches, Git tries to automatically resolve any conflicts. If conflicts occur, you'll need to manually resolve them before completing the merge.

Additional Git Commands

  • git status: Shows the current state of the working directory and staging area.
  • git log: Displays a log of all commits in the repository, including commit messages and author information.
  • git diff: Shows the differences between the current working directory and the last commit, or between two commits or branches.
  • git reset: Undoes changes to the staging area or committed changes, depending on the provided options.
  • git revert: Reverts a specific commit by creating a new commit that undoes the changes from the specified commit.

Additional Resources

Top Git Commands you must know | Git and Github - Skytup Blog