Skip to content

Latest commit

 

History

History
238 lines (189 loc) · 4.53 KB

File metadata and controls

238 lines (189 loc) · 4.53 KB

Git Commands Reference

Adding a New Repository to GitHub

  1. Navigate to project folder:

    cd /path/to/your/project
  2. Initialize Git repository:

    git init
  3. Add remote repository:

    git remote add origin https://github.com/YourUsername/your-repository.git
  4. Add files to staging:

    git add .
  5. Commit changes:

    git commit -m "Initial commit message"
  6. Push to GitHub:

    git push -u origin master  # Or 'main' if your GitHub repo uses main branch

Force Local Files to Replace Repository Files

  1. Navigate to project:

    cd C:\Users\amnic\OneDrive\Desktop\Coding\Git\ProjectName
  2. Initialize and stage:

    git init
    git add .
    git commit -m "Initial commit of ProjectName"
  3. Configure remote:

    git branch --unset-upstream
    git remote -v
    git remote set-url origin https://github.com/YourUsername/your-repository.git
  4. Push and update branches:

    git push -u origin master
    git branch -m master main
    git fetch origin
    git push -f origin main
    git push origin --delete master

Cloning GitHub Repository

  1. Get the HTTPS URL from GitHub repository

    • Go to repository on GitHub.com
    • Click green "Code" button
    • Copy the HTTPS URL
  2. Navigate to desired directory

    cd your/desired/directory
  3. Clone the repository

    git clone https://github.com/YourUsername/your-repository.git
  4. Enter the project directory

    cd your-repository

Cleaning Repository History

  1. Navigate to repository:

    cd /path/to/your/project
  2. Create backup branch:

    git checkout -b backup-main
  3. Return to primary branch:

    git checkout master  # or 'main' depending on your branch name
  4. Clean repository history:

    git filter-branch --force --index-filter "git rm --cached --ignore-unmatch PATH-TO-FILE" --prune-empty --tag-name-filter cat -- --all
  5. Force push changes:

    git push origin --force --all

This process rewrites repository history to remove sensitive information from previous commits while preserving current code.

Resolving Main/Master Branch Conflicts

  1. Rename local branch to main:

    git branch -M main
  2. Pull remote changes with unrelated histories:

    git pull origin main --allow-unrelated-histories
  3. Push changes to main branch:

    git push -u origin main

This process synchronizes repositories when working across multiple machines and resolves branch naming conflicts.

Removing Files from Repository

  1. Remove file and stage deletion:

    git rm filename
  2. Commit the removal:

    git commit -m "Remove file"
  3. Push changes to remote:

    git push origin main  # or master depending on branch name

Managing File Contents

  1. Create backup of original files (optional):

    cp file1.md file1_backup.md
    cp file2.md file2_backup.md
  2. Edit files with new content

  3. Stage and commit changes:

    git add .
    git commit -m "Reorganized file contents"
  4. Push to remote:

    git push origin master

Pulling Changes from Repository

  1. Check remote repository status:

    git remote -v
  2. Fetch latest changes:

    git fetch origin
  3. Pull changes from remote:

    git pull origin main
  4. Pull with specific options:

    git pull --rebase origin main

Handling Vim Editor in Git

  1. When Vim appears (usually during merge operations):

    • You'll see a text editor interface with merge message
    • Cursor will be at the top of the file
  2. Basic Vim commands for editing:

    i           # Enter insert mode (allows typing)
    Esc        # Exit insert mode
    :wq        # Save and exit (write and quit)
    :q!        # Exit without saving (quit without write)
  3. Common workflow:

    • Press 'i' to enter insert mode
    • Type your merge message
    • Press 'Esc' to exit insert mode
    • Type ':wq' and press Enter to save and exit

This process handles the Vim editor that appears during Git merge operations or when Git needs a detailed commit message.