World News

Most Important Git Commands Handbook

Here is the cleanest, most complete, real-world list of important Git commands, grouped by purpose. This covers everything a DevOps/Cloud/Engineering professional uses daily. Perfect for adding to your website, notes, or cheat sheets.

Basic Git Commands

git init                 # Initialize a new Git repository
git clone <url>          # Clone a remote repository
git status               # Show changed files
git add .                # Stage all changes
git add <file>           # Stage a specific file
git commit -m "msg"      # Commit changes
git log                  # View commit history
git diff                 # Show unstaged changes
git diff --staged        # Show staged changes
  

Branching Commands

git branch               # List branches
git branch <name>        # Create new branch
git checkout <name>      # Switch to branch
git checkout -b <name>   # Create and switch to new branch
git branch -M main       # Rename branch to main
git merge <branch>       # Merge branch into current branch
git branch -d <branch>   # Delete branch
  

Remote Repository Commands

git remote -v                                   # Show remotes
git remote add origin <url>                     # Add remote origin
git remote set-url origin <url>                 # Change remote URL
git push -u origin main                         # Push main for the first time
git push                                        # Push changes
git pull                                        # Pull latest changes
git fetch                                       # Fetch changes without merging
git pull origin main                            # Pull specific branch
  

Undo / Fix Commands

git restore <file>                               # Undo changes in working directory
git restore --staged <file>                      # Unstage a file
git reset HEAD~1                                 # Undo last commit (keep changes)
git reset --hard HEAD~1                          # Undo last commit (delete changes)
git revert <commit-id>                           # Reverse a commit safely
git clean -fd                                    # Remove untracked files & folders
  

Stash Commands

git stash                                        # Save uncommitted changes
git stash pop                                    # Apply and remove stash
git stash apply                                  # Apply stash (keep stash)
git stash list                                   # Show stashes
  

Tagging Commands

git tag                                          # List tags
git tag <tagname>                                # Create tag
git push origin <tagname>                        # Push tag to GitHub
  

GitHub Workflow Commands

git clone <repo-url>                             # Clone a GitHub repo
git remote set-url origin <your-repo-url>        # Connect local repo to GitHub
git push -u origin <branch>                      # First-time push for new branch
git switch <branch>                              # Move between branches
git fetch --all                                  # Sync all branches
  

Create New Project and Push to GitHub

git init
git add .
git commit -m "Initial commit"
git remote add origin <repo-url>
git branch -M main
git push -u origin main
  

Cleaning & Maintenance

git gc                                           # Garbage collection
git prune                                        # Remove unnecessary objects
git clean -fd                                    # Remove untracked files
  

Git Config (User Settings)

git config --global user.name "Your Name"
git config --global user.email "your@email.com"
git config --list                                # View config
  

Post a Comment

0 Comments