DEV Community

Cover image for Git Commands Every Developer Should Know
Anuj Srivastav
Anuj Srivastav

Posted on

Git Commands Every Developer Should Know

Git is a powerful version control system that has become an integral part of modern software development. Whether you are a beginner or an experienced developer, understanding and mastering essential Git commands is crucial for efficient collaboration, code management, and tracking changes. In this blog, we will explore some of the most commonly used Git commands and how they can streamline your development workflow.

Essential Git Commands

1. Initializing a Repository:
To start using Git, you first need to create a new repository or clone an existing one. The following commands help you set up a new repository:

  • git init: Initializes a new Git repository in the current directory.
  • git clone <repository-url>: Clones an existing repository from a remote server to your local machine.

2. Basic Configuration:
Before you start committing code, it's essential to set your identity in Git:

  • git config --global user.name "Your Name": Set your name for all commits.
  • git config --global user.email "youremail@example.com": Set your email for all commits. **
  • Adding and Committing Changes:**
    The heart of Git is its ability to track and manage changes to your code. To save changes to the repository, use the following commands:

  • git status: Shows the status of your repository, including changed files and untracked files.

  • git add <file-name>: Adds a specific file to the staging area to be included in the next commit.

  • git add .: Adds all changes in the working directory to the staging area.

  • git commit -m "Commit message": Commits the changes in the staging area with a descriptive message.

4. Branching and Merging:
Git allows you to work on separate branches to develop new features or fix bugs without affecting the main codebase. Use the following commands to manage branches:

  • git branch: Lists all branches in the repository, highlighting the current branch.
  • git branch <branch-name>: Creates a new branch with the given name.
  • git checkout <branch-name>: Switches to the specified branch.
  • git merge <branch-name>: Merges the changes from the specified branch into the current branch.

5. Updating and Synchronizing:
To keep your local repository up to date with the remote repository and vice versa, use these commands:

  • git pull: Fetches and merges changes from the remote repository to your local branch.
  • git push: Pushes your local commits to the remote repository.

6. Viewing History and Differences:
Git provides tools to review changes and commit history:

  • git log: Shows a detailed commit history, including author, date, and commit message.
  • git diff: Displays the differences between the working directory and the staging area.
  • git diff <commit-id-1> <commit-id-2>: Shows the differences between two specific commits.

Advanced Git Commands for Efficient Version Control

1. Git Rebase:
Git rebase is a powerful command used to integrate changes from one branch to another by moving or combining commits. It allows you to maintain a linear commit history and avoid the complexities of merge commits. Use the following commands:

  • git rebase <branch-name>: Rebases the current branch onto the specified branch.
  • git rebase -i <commit-id>: Interactive rebase that lets you squash, reword, or reorder commits.

2. Git Cherry-Pick:
Cherry-picking allows you to apply a specific commit from one branch to another. It is useful when you need to add a single commit from a different branch without merging the entire branch. Use the following command:

  • git cherry-pick <commit-id>: Applies the specified commit to the current branch.

3. Git Stash:
When you need to switch to a different branch but have uncommitted changes in your current branch, you can use Git stash to save your changes temporarily. Use the following commands:

  • git stash: Stashes your changes, saving them for later use.
  • git stash apply: Applies the most recent stash to the current branch.
  • git stash pop: Applies the most recent stash and removes it from the stash list.

4. Git Reflog:
The Git reflog keeps a record of all changes made to the repository, including commits, checkouts, and resets. It is a safety net that helps you recover lost or deleted commits. Use the following command:

  • git reflog: Displays the history of all Git actions in the repository.

5. Git Bisect:
Git bisect is a powerful command used to perform a binary search through the commit history to find the exact commit that introduced a bug. It is an effective way to identify the root cause of issues. Use the following commands:

  • git bisect start: Initiates the binary search process.
  • git bisect bad: Marks the current commit as a bad (buggy) commit.
  • git bisect good <commit-id>: Marks the specified commit as a good (working) commit.
  • git bisect reset: Exits the bisect session.

6. Git Submodules:
Git submodules allow you to include one Git repository as a subdirectory of another repository. This is useful for managing dependencies and integrating external projects. Use the following commands:

  • git submodule add <repository-url> <destination-path>: Adds a new submodule to the parent repository.
  • git submodule update: Initializes and updates submodules after cloning the parent repository.

Note : Remember to use these commands judiciously and always create backups or use Git branches when experimenting with code changes. Happy coding and version controlling!

Top comments (0)