DEV Community

Cover image for Git Basic Commands: A Comprehensive Guide with Examples
Shaikh AJ
Shaikh AJ

Posted on

Git Basic Commands: A Comprehensive Guide with Examples

Introduction:
Git, a distributed version control system, is an essential tool for managing source code and collaborating with others in software development projects. Understanding the basic Git commands is fundamental to effectively track changes, create branches, merge code, and more. In this comprehensive guide, we will explore the most commonly used Git commands, accompanied by examples, to help you get started with Git and streamline your development workflow.

Table of Contents:

  1. Initializing a Git Repository
  2. Committing Changes
  3. Viewing Repository Status
  4. Working with Branches
  5. Merging Branches
  6. Inspecting Commits and History
  7. Collaborating with Remote Repositories
  8. Additional Useful Commands
  9. Conclusion

1. Initializing a Git Repository:
To start using Git in your project, initialize a Git repository with the following command:

git init
Enter fullscreen mode Exit fullscreen mode

This creates a hidden .git folder to track changes and store metadata.

2. Committing Changes:
Committing changes allows you to save a snapshot of your code. Use the following commands:

git add <file>        # Stage changes for commit
git commit -m "Message"    # Commit changes with a descriptive message
Enter fullscreen mode Exit fullscreen mode

Example:

git add file1.js
git commit -m "Add file1.js"
Enter fullscreen mode Exit fullscreen mode

3. Viewing Repository Status:
To check the status of your repository and see which files are modified, added, or deleted, use:

git status
Enter fullscreen mode Exit fullscreen mode

Example:

git status
Enter fullscreen mode Exit fullscreen mode

4. Working with Branches:
Branches allow you to work on different features or bug fixes independently. Use the following commands:

git branch                # List all branches
git branch <branch-name>   # Create a new branch
git checkout <branch-name>    # Switch to a branch
Enter fullscreen mode Exit fullscreen mode

Example:

git branch
git branch new-feature
git checkout new-feature
Enter fullscreen mode Exit fullscreen mode

5. Merging Branches:
To integrate changes from one branch into another, use the git merge command. It combines the changes and resolves conflicts if any.

git checkout <target-branch>     # Switch to the target branch
git merge <source-branch>        # Merge changes from the source branch
Enter fullscreen mode Exit fullscreen mode

Example:

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

6. Inspecting Commits and History:
Git provides various commands to inspect commits and view repository history:

git log                    # View commit history
git diff                   # Show changes between commits or branches
Enter fullscreen mode Exit fullscreen mode

Example:

git log
git diff main feature-branch
Enter fullscreen mode Exit fullscreen mode

7. Collaborating with Remote Repositories:
To collaborate with others and sync your local repository with a remote repository (e.g., GitHub), use the following commands:

git remote add origin <remote-url>    # Add a remote repository
git push -u origin <branch-name>      # Push local changes to the remote repository
git pull origin <branch-name>         # Pull latest changes from the remote repository
Enter fullscreen mode Exit fullscreen mode

Example:

git remote add origin https://github.com/username/repository.git
git push -u origin main
git pull origin main
Enter fullscreen mode Exit fullscreen mode

8. Additional Useful Commands:

  • git clone <repository-url>: Clone a remote repository to your local machine.
  • git checkout -b <branch-name>: Create and switch to a new branch in a single command.
  • git reset <commit>: Undo commits and move the branch pointer to a specific commit.
  • git stash: Temporarily save changes that are not ready to be committed.

9. Conclusion:
Mastering the basic Git commands is crucial for efficient version control and collaboration in software development. This comprehensive guide covered initializing a repository, committing changes, working with branches, merging code, inspecting commits, collaborating with remote repositories, and additional useful commands. By leveraging Git's power, you can streamline your development workflow and effectively manage your codebase. Start incorporating Git into your projects today, and happy coding!

Top comments (0)