Comprehensive Guide to Git and GitHub Workflows

Core Concepts

  • Working Directory: The directory you see on your file system.
  • Staging Area (Index): Stored in the .git/index file within the working directory.
  • Repository (Local Repo): Stored in the .git directory.
  • HEAD:
    • A pointer to the current local branch.
    • Can point to a branch or a specific commit.
    • Defines which branch is associated with the working directory and staging area for comands like add, checkout, and commit.
    • Example commands:
      • git checkout branch_name redirects HEAD to the specified branch.
      • git checkout commit_hash moves HEAD to a specific commit.
  • master and origin:
    • master is the default name of the main branch in the local repository.
    • origin is the default alias for the remote repository, typically assigned during git clone.
  • Local Repository: A copy of the remote repository (simplified).
  • Remote Repository: Used for collaborative development and backup.
  • Branch:
    • Represents an independent line of development.
    • Branches exist only in repositories, not in the working directory or staging area.
    • Common commands:
      • git checkout -b feature-branch origin/feature-branch – Creates a local branch from a remote branch.
      • git checkout -b local_branch – Creates and switches to a new local branch.
      • git push --set-upstream origin local_branch – Pushes a local branch to the remote.
      • git branch – Lists local branches.
      • git branch -a – Lists all branches (local and remote).
      • git branch -v – Shows the last commit on each local branch.
      • git branch --merged – Shows branches merged into the current branch.
      • git branch --no-merged – Shows branches not merged into the current branch.
      • git merge <branch> – Merges a branch into the current branch.
      • git branch -d [branchname] – Deletes a local branch.
      • git push origin --delete [branchname] – Deletes a remote branch.

Basic Commands

Working Directory, Staging Area, and Local Repository

Command Description
git remote -v View remote repository URLs.
git add Add files to the staging area.
git status Show the state of the working directory and staging area.
git diff Compare changes between the working directory, staging area, and repository.
git commit Commit the staging area to the local repository.
git reset Unstage files or revert commits.
git rm Remove files from the staging area and working directory.
git mv Move or rename files.
git checkout Switch branches or restore files.
git switch Switch branches (introduced in Git 2.23).
git restore Restore or undo changes (introduced in Git 2.23).

Remote and Local Repository Operations

Command Description
git remote Manage remote repositories.
git clone Clone a remote repository.
git clone -b <branch> <url> Clone a specific branch.
git pull Fetch and merge remote changes.
git push Push local changes to the remote.
git submodule update --init Initialize submodules.
git submodule update --init --recursive Initialize and update submodules recursively.

Logging

Command Description
git log View commit history.
git blame <file> View file change history per line.

Fork Synchronization Without Overwriting Local Changes

To sync an upstream repository (ArduPilot/ardupilot:master) to your fork (huangyangl/ardupilot) without losing your own changes (e.g., on a custom branch adrc-1.0-copy):

  1. Use the Sync fork button on GitHub to update your master branch from the upstream.
  2. Clone your fork locally:
    git clone https://github.com/huangyangl/ardupilot.git
    git checkout -b adrc-1.0 origin/adrc-1.0
    git checkout -b adrc-1.0-copy origin/adrc-1.0-copy
    
  3. Merge the updated master into your custom branch:
    git checkout adrc-1.0-copy
    git config pull.rebase false
    git pull origin master:adrc-v1.0-copy
    
  4. Resolve conflicts manually, then stage and commit.
  5. Update submodules if needed:
    git submodule init
    git submodule update --recursive
    # Or for ArduPilot:
    ./waf submodule_force_clean
    
  6. Push the merged branch to your remote:
    git push origin adrc-1.0-copy
    

Configuring a Custom Editor

  • View current editor: git config --global core.editor
  • Set editor: git config --global core.editor <editor_name>

Detailed Command Examples

Local Operations

  • git init: Initialize a local directory.
  • git remote:
    • git remote -v – View remote URLs.
  • git --help:
    • git --help – Show general help.
    • git help -a – List all subcommands.
    • git help -g – List concepts.
  • git status:
    • git status – Full status.
    • git status -s – Short format.
    • git status --ignored – Include ignored files.
  • .gitignore:
    • Syntax examples:
      # Ignore directory
      .idea
      # Ignore file type
      *.iml
      # Ignore all .o and .a files
      *.[oa]
      # But keep my.b
      *.[bB]
      !my.b
      # Ignore dbg directory
      dbg/
      
    • To apply .gitignore to already tracked files:
      git rm -r --cached .
      git add .
      git commit -m 'update .gitignore'
      
  • git diff:
    • Working directory vs staging area: git diff
    • Working directory vs repository: git diff HEAD
    • Staging area vs repository: git diff --cached
    • Between commits: git diff <commit1> <commit2>
    • Use --stat for summary.
  • git add:
    • git add * – Add all changes.
    • git add . – Add all (tracked and untracked).
    • git add -u . – Add only tracked changes.
    • git add <file> – Add a specific file.
  • git reset:
    • git reset – Unstage all files.
    • git reset -- <file> – Unstage a specific file.
  • git commit:
    • `git commit -m

Tags: Git GitHub version-control branch submodule

Posted on Fri, 31 Jul 2026 16:10:52 +0000 by koddos