Essential Git & GitHub Terminal Commands

Essential Git & GitHub Terminal Commands

Comprehensive command-line reference for version control, repository setup, branching, stashing, and syncing.

  • Repository Initialisation & Config: Start a new local repository and set your global identity.
    git init
    git config --global user.name "Your Name"
    git config --global user.email "your.email@example.com"
    git config --list
  • Check Status & Changes: Inspect modified files and view line-by-line diffs.
    git status
    git diff
  • Staging & Committing: Package modified code into local version snapshots.
    git add .
    git commit -m "Your descriptive commit message"
  • Branch Management (main vs master): Modern Git defaults to main (replacing the older term master). Create, switch, and merge isolated feature branches.
    git branch                        # List local branches
    git checkout -b feature-branch    # Create and switch to a new branch
    git switch main                   # Switch back to main branch
    git merge feature-branch          # Merge changes into current branch
  • Stashing Changes: Temporarily set aside uncommitted work when you need to switch branches quickly without committing incomplete code.
    git stash                         # Save uncommitted changes to stack
    git stash list                    # View saved stashes
    git stash pop                     # Reapply and remove the latest stash
  • Remotes, Syncing & Forks: Connect to GitHub, manage origin/upstream links, and sync your repository. A fork is your personal server-side copy of someone else's repository.
    git remote add origin URL         # Link local repo to GitHub origin
    git remote set-url origin URL     # Update remote URL if changed
    git remote -v                     # Verify connected remote URLs
    git push origin main              # Push commits to remote main
    git pull origin main              # Fetch and merge updates from remote
    git remote add upstream URL       # Link to original repo (used with Forks)
  • Diagnostics & Commit History: Inspect past logs with visual branch graphs.
    git log --oneline                 # Compact commit history list
    git log --oneline --graph --all   # Visual ASCII tree of all branches and merges

Essential Google Cloud (`gcloud`) Commands

Command-line toolkit for managing GCP project configurations, enabling container APIs, and deploying serverless applications.

  • Project Configuration: View active session settings and switch projects.
    gcloud config list
    gcloud config set project YOUR_PROJECT_ID
  • Enabling Infrastructure APIs: Turn on required services for container building and serverless execution.
    gcloud services enable run.googleapis.com cloudbuild.googleapis.com artifactregistry.googleapis.com
  • Deploying to Cloud Run: Compile container images from source code and provision a public HTTPS URL.
    gcloud run deploy service-name \
        --source . \
        --platform managed \
        --region us-central1 \
        --allow-unauthenticated \
        --port 7860
  • IAM & Service Permissions: Grant required compilation roles to default project service accounts.
    gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
        --member="serviceAccount:YOUR_SERVICE_ACCOUNT@developer.gserviceaccount.com" \
        --role="roles/cloudbuild.builds.builder"

The "When & Why" of Git & GitHub

Understanding the underlying logic behind version control commands instead of treating them like magic.

  • git init — The Initial Setup
    When: Exactly once per project, when starting a new folder.
    Why: Tells Git to watch the folder and creates the hidden `.git` tracking history.
    git init
  • git status & git diff — The Inspection
    When: Constantly, before staging or committing, to verify what changed.
    Why: Shows modified files (`status`) and exact line-by-line alterations (`diff`).
    git status
    git diff
  • git add . & git commit — The Snapshot
    When: Whenever you finish a working chunk of logic or fix a bug.
    Why: Bundles changes (`add`) and takes a permanent local timeline snapshot with a descriptive label (`commit`).
    git add .
    git commit -m "Descriptive commit message"
  • git push origin main — The Cloud Backup
    When: At the end of a session to secure local snapshots on GitHub.
    Why: Uploads your local commit history to the remote cloud server.
    git push origin main

The "When & Why" of Google Cloud (GCP)

Demystifying cloud serverless deployment steps.

  • gcloud config set project — Workspace Targeting
    When: When beginning a cloud session or switching projects.
    Why: Directs terminal commands to the correct billing account and project workspace.
    gcloud config set project YOUR_PROJECT_ID
  • gcloud services enable — Infrastructure Provisioning
    When: Once during initial setup on a fresh GCP project.
    Why: Turns on necessary backend APIs (Cloud Build, Artifact Registry, Cloud Run) that are disabled by default for security.
    gcloud services enable run.googleapis.com cloudbuild.googleapis.com artifactregistry.googleapis.com
  • gcloud run deploy — Automated Packaging & Launch
    When: When your local app is ready for public web access.
    Why: Packages local source files, builds a container in the cloud, and provisions a live HTTPS URL in a single step.
    gcloud run deploy service-name --source . --region us-central1 --allow-unauthenticated

Essential Git & GitHub Terminal Commands

Comprehensive command-line reference for version control, repository setup, staging scopes, rebasing, and syncing.

  • Repository Initialization & Config: Start a new local repository and set your global identity.
    git init
    git config --global user.name "Your Name"
    git config --global user.email "your.email@example.com"
    git config --list
  • Check Status & Changes: Inspect modified files and view line-by-line diffs.
    git status
    git diff
  • Staging Scopes & Committing: Choose between local folder tracking (`git add .`) or repository-wide staging (`git add -A`), then snapshot your work.
    git add .                         # Stage changes only in current directory
    git add -A                        # Stage all new, modified, and deleted files repo-wide
    git commit -m "Your descriptive commit message"
  • Rebasing Timelines (`git rebase`): Replay local commits on top of updated code to maintain a clean, linear history instead of messy merge bubbles.
    git pull --rebase origin main     # Fetch remote updates and replay your local commits cleanly
    git rebase main                   # Replay current branch commits on top of main
  • Branch Management: Create, switch, and merge isolated feature branches.
    git branch                        # List local branches
    git checkout -b feature-branch    # Create and switch to a new branch
    git switch main                   # Switch back to main branch
    git merge feature-branch          # Merge changes into current branch
  • Stashing Changes: Temporarily set aside uncommitted work when switching branches.
    git stash                         # Save uncommitted changes to stack
    git stash list                    # View saved stashes
    git stash pop                     # Reapply and remove the latest stash
  • Remotes, Syncing & Forks: Connect to GitHub and sync your repository. A fork is your personal server-side copy of someone else's repository.
    git remote add origin URL         # Link local repo to GitHub origin
    git remote -v                     # Verify connected remote URLs
    git push origin main              # Push commits to remote main
    git remote add upstream URL       # Link to original repo (used with Forks)
  • Diagnostics & Commit History: Inspect past logs with visual branch graphs.
    git log --oneline                 # Compact commit history list
    git log --oneline --graph --all   # Visual ASCII tree of all branches and merges

1. master vs main: Just Names

Historically, Git named the default initial branch master. A few years ago, the developer community shifted to main as the standard default.
  • The Conflict: If your local repository calls your default branch master, but Hugging Face (or GitHub) expects main, they are essentially two independent labels pointing at timelines. When you ran git push origin master:main, you were explicitly telling Git: "Take my local master branch and force it to populate the remote main branch."

2. Divergent Histories ("Ahead by X, Behind by Y")

Git tracks your project as a timeline of commits. When your local repository and remote Hugging Face repository say they are "ahead by X and behind by Y," it means:

  • Ahead: You made commits locally that the remote server doesn't have yet.

  • Behind: Someone (or the web interface) made commits directly on the remote server that your local machine doesn't have yet.

  • When both happen simultaneously, your timelines diverge. Git refuses to overwrite data blindly, which causes sync blocks and errors.

3. git pull: Fetch + Merge

When you run git pull, Git does two things in one command:

  1. Fetch: It downloads all the latest changes and history from the remote server.

  2. Merge: It attempts to combine the remote history with your local history. If there are conflicting changes, it creates a "merge commit" to tie the two divergent branches back together into a webbed history.

4. git rebase: Rewriting History Linearly

Instead of creating a messy merge commit that web-links two divergent histories, rebase reorganizes your local history so it looks like you started working after all the latest remote changes were already there.

  • How it works: Rebase temporarily lifts your local commits off the old base, updates your project to match the latest remote state, and then replays your local commits one by one on top. The result is a clean, straight, linear history without merge clutter.


Essential Google Cloud (`gcloud`) Commands

Command-line toolkit for managing GCP project configurations, enabling container APIs, and deploying serverless applications.

  • Project Configuration: View active session settings and switch projects.
    gcloud config list
    gcloud config set project YOUR_PROJECT_ID
  • Enabling Infrastructure APIs: Turn on required services for container building and serverless execution.
    gcloud services enable run.googleapis.com cloudbuild.googleapis.com artifactregistry.googleapis.com
  • Deploying to Cloud Run: Compile container images from source code and provision a public HTTPS URL.
    gcloud run deploy service-name \
        --source . \
        --platform managed \
        --region us-central1 \
        --allow-unauthenticated \
        --port 7860
  • IAM & Service Permissions: Grant required compilation roles to default project service accounts.
    gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
        --member="serviceAccount:YOUR_SERVICE_ACCOUNT@developer.gserviceaccount.com" \
        --role="roles/cloudbuild.builds.builder"

Comments

Popular posts from this blog

CSV to Markdown Table Converter

🌌 Planetarium: Measuring the Cosmos — From Starlight to Signal

Markdown Utilities