DEV Community

Cover image for Mastering Git: A Curated List of Must-Know Commands for Efficient Version Control
MD ARIFUL HAQUE
MD ARIFUL HAQUE

Posted on

Mastering Git: A Curated List of Must-Know Commands for Efficient Version Control

Here's a detailed summary of common Git commands, compiled with hands-on examples and descriptions to help you understand and use Git effectively in your development workflow.


Git: Essential Commands and Usage Guide

Git is a powerful version control system used by developers to manage and track changes in their codebase. Below is a compilation of common Git commands along with practical examples and step-by-step descriptions.

1. Initializing a Repository

Command:

git init
Enter fullscreen mode Exit fullscreen mode

Description:
Initializes a new Git repository in the current directory. This creates a .git directory where Git stores all its tracking data.

Example:

mkdir my-project
cd my-project
git init
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Create a new directory for your project.
  2. Navigate into the directory.
  3. Initialize a Git repository in this directory.

2. Cloning a Repository

Command:

git clone <repository_url>
Enter fullscreen mode Exit fullscreen mode

Description:
Creates a copy of an existing Git repository. This command clones the repository from a remote server to your local machine.

Example:

git clone https://github.com/user/repo.git
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Provide the URL of the remote repository.
  2. Git copies all the files and commits from the remote repository to your local machine.

3. Checking Repository Status

Command:

git status
Enter fullscreen mode Exit fullscreen mode

Description:
Displays the state of the working directory and staging area. It shows changes that have been staged, changes that are not staged, and files that are not being tracked.

Example:

git status
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Run the command to see which files have been modified, added, or deleted.

4. Adding Changes to Staging Area

Command:

git add <file>
Enter fullscreen mode Exit fullscreen mode

Description:
Adds changes in the specified file to the staging area. This prepares the file for the next commit.

Example:

git add index.php
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Use this command to stage changes in index.php for committing.

To add all changes:

git add .
Enter fullscreen mode Exit fullscreen mode

5. Committing Changes

Command:

git commit -m "Commit message"
Enter fullscreen mode Exit fullscreen mode

Description:
Records the changes made to the files in the repository along with a commit message describing the changes.

Example:

git commit -m "Add user login functionality"
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Stage the changes first.
  2. Commit the staged changes with a descriptive message.

6. Viewing Commit History

Command:

git log
Enter fullscreen mode Exit fullscreen mode

Description:
Shows the commit history of the repository, including commit IDs, author names, dates, and commit messages.

Example:

git log
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Run this command to review the history of commits made to the repository.

7. Creating a New Branch

Command:

git branch <branch_name>
Enter fullscreen mode Exit fullscreen mode

Description:
Creates a new branch in the repository. Branches allow you to work on different features or bug fixes simultaneously.

Example:

git branch feature-branch
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Create a new branch called feature-branch.

8. Switching Branches

Command:

git checkout <branch_name>
Enter fullscreen mode Exit fullscreen mode

Description:
Switches to the specified branch. This command updates your working directory to reflect the state of the branch you switch to.

Example:

git checkout feature-branch
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Switch to the feature-branch to work on it.

Note: In Git versions 2.23 and later, you can use:

git switch <branch_name>
Enter fullscreen mode Exit fullscreen mode

9. Merging Branches

Command:

git merge <branch_name>
Enter fullscreen mode Exit fullscreen mode

Description:
Merges the specified branch into the current branch. This integrates changes from one branch into another.

Example:

git checkout main
git merge feature-branch
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Switch to the branch you want to merge changes into (e.g., main).
  2. Merge the changes from feature-branch into main.

10. Pushing Changes to Remote Repository

Command:

git push origin <branch_name>
Enter fullscreen mode Exit fullscreen mode

Description:
Uploads the local branch and its commits to the remote repository.

Example:

git push origin feature-branch
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Push the feature-branch to the remote repository.

11. Pulling Changes from Remote Repository

Command:

git pull
Enter fullscreen mode Exit fullscreen mode

Description:
Fetches and integrates changes from the remote repository into your current branch.

Example:

git pull
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Update your local branch with changes from the remote repository.

12. Removing Files from Staging Area

Command:

git reset <file>
Enter fullscreen mode Exit fullscreen mode

Description:
Unstages the specified file, but keeps the changes in the working directory.

Example:

git reset index.php
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Unstage index.php if it was added to the staging area but you don't want to commit it yet.

13. Deleting a Branch

Command:

git branch -d <branch_name>
Enter fullscreen mode Exit fullscreen mode

Description:
Deletes the specified branch from the local repository. Use -D to force delete.

Example:

git branch -d feature-branch
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Delete feature-branch after it has been merged or is no longer needed.

14. Stashing Changes

Command:

git stash
Enter fullscreen mode Exit fullscreen mode

Description:
Temporarily saves changes in a stash so you can work on something else. Apply the stash later with git stash apply.

Example:

git stash
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Save your changes temporarily if you need to switch tasks.

15. Viewing Stash List

Command:

git stash list
Enter fullscreen mode Exit fullscreen mode

Description:
Displays a list of all stashed changes.

Example:

git stash list
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Check the list of stashed changes to decide which to apply or drop.

16. Applying Stashed Changes

Command:

git stash apply
Enter fullscreen mode Exit fullscreen mode

Description:
Applies the most recent stash to your working directory.

Example:

git stash apply
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Retrieve your stashed changes and apply them back to your working directory.

17. Removing a Stash

Command:

git stash drop
Enter fullscreen mode Exit fullscreen mode

Description:
Removes a specific stash from the list of stashed changes.

Example:

git stash drop stash@{0}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Remove the specified stash from the stash list.

18. Reverting a Commit

Command:

git revert <commit_id>
Enter fullscreen mode Exit fullscreen mode

Description:
Creates a new commit that undoes the changes from a specified commit.

Example:

git revert a1b2c3d
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Create a new commit that undoes the changes introduced by a1b2c3d.

Conclusion

Understanding and using these common Git commands will help you manage your codebase effectively and collaborate efficiently with others. This guide provides a solid foundation for working with Git in your development projects. Feel free to adapt and expand these commands based on your specific workflow and requirements.

Top comments (0)