Mastering Git Commands: A Comprehensive Guide

Mastering Git Commands: A Comprehensive Guide

Introduction

Git, the powerful version control system, is a must-know for developers and teams collaborating on projects. In this guide, we'll explore essential Git commands, GitHub usage, and best practices to streamline your development workflow.

Getting Started with Git

  1. Configuring User Information

     git config --global user.name "Your Name"
    
  2. Checking Configuration

     git config --list
    
  3. Initializing a Git Repository

     git init
    

Basic Git Commands

  1. Checking Repository Status

     git status
    
  2. Adding and Committing Changes

     git add . 
     git commit -m "Your commit message"
    
  3. Viewing Commit History

     git log
    

Navigating and Undoing Changes

  1. Changing Directories

     cd <directory>
    
  2. Ignoring Files Create a .gitignore file and list files or directories to be ignored.

  3. Comparing Changes

     git diff
    
  4. Undoing Changes

     git restore --staged <file>
     git checkout -- <file>
    

Branching in Git

  1. Viewing and Creating Branches

     git branch
     git branch <branch-name>
    
  2. Switching Between Branches

     git checkout <branch-name>
    
  3. Deleting Branches

     git branch -d <branch-name>
    

GitHub Integration

  1. Cloning a Repository

     git clone <repository-link>
    
  2. Pushing Changes to GitHub

     git push origin <branch-name>
    
  3. Deleting Remote Branches

     git push -d origin <branch-name>
    

GitHub Configuration

  1. Linking a Local Repository to GitHub

     git remote add origin <repository-link>
    
  2. Creating SSH Keys for GitHub GitHub SSH Key Guide

  3. Removing a Git Repository

     rm -rf .git
    

Additional Tips

  1. Copy-Paste Shortcuts

    • Shift + Insert: Paste anything in the terminal.
  2. Checking Present Working Directory

     pwd
    
  3. Log Navigation After git log, press q + enter to exit.

  4. Ignoring Files Permanently Use .gitignore to list files/folders to be ignored.

  5. Pushing Without Staging

     git add -A -m "Your commit message"
    
  6. Renaming and Deleting Files

     git rm <file-name>
     git mv <old-name> <new-name>
    
  7. Log with Statistics

     git log --stats
    
  8. Log Formatting

     git log --pretty=oneline
    
  9. Log Filtering by Date

     git log --since=2.days
    
  10. Checking Remote Repositories

    git remote -v
    
  11. Matching Working Directory

    git checkout -f
    

Conclusion

Mastering these Git commands and GitHub practices will empower you to efficiently manage your projects and collaborate seamlessly. Remember, practice is key to becoming a Git expert!

Thank you so much for considering my message. I understand your time is valuable, and any guidance you can provide would be immensely helpful.