Skip to content

Instantly share code, notes, and snippets.

@edoves
Last active October 30, 2024 14:17
Show Gist options
  • Save edoves/752e679fb6bbbf772fe5915660ab962d to your computer and use it in GitHub Desktop.
Save edoves/752e679fb6bbbf772fe5915660ab962d to your computer and use it in GitHub Desktop.

Git and GitHub Essentials

1. Basic Git Commands for Cloning and Collaboration

  • Clone a Repository: git clone <repository-url>
  • Navigate to Directory: cd <directory>
  • List Directory Contents: ls
  • Check Git Status: git status (useful for verification)
  • Add File to Staging Area: git add <filename>
  • Commit Changes: git commit -m "Your commit message here"
  • Push Changes: git push

Working with Remotes

  • View Remotes: git remote -v
  • Add Remote Upstream: git remote add upstream <repository-url>
  • Pull from Upstream Master: git pull upstream master

Branch Management

  • Create a Branch: git branch <branch-name>
  • Switch to a Branch: git checkout <branch-name>

2. Configuring Git

Configuration Levels

  1. System: Configuration for all users on the computer

    • Location: /etc/gitconfig or Program Files\Git\etc\gitconfig
    • Command: git config --system
  2. Global (User): Configuration for a specific user

    • Location: ~/.gitconfig or $HOME/.gitconfig
    • Command: git config --global
  3. Project (Local): Configuration for a specific repository

    • Location: <project>/.git/config
    • Command: git config

Basic Configuration Settings

  • Set Username: git config --global user.name "Your Name"
  • Set Email: git config --global user.email "[email protected]"
  • View Configurations: git config --list
  • Edit Global Config: git config --global --edit
  • Unset Configurations:
    • Remove specific setting: git config --global --unset <config-name>
    • Remove all values of a setting: git config --global --unset-all <config-name>

Access Config Files

  • View Config File Content: cat ~/.gitconfig
  • Edit Config in Windows: nano ~/.gitconfig or vi ~/.gitconfig

Color-Coding in Git

  • Enable Color UI: git config --global color.ui true

3. Configuring Line Endings

  • Windows: git config --global core.autocrlf true
  • Mac OS: git config --global core.autocrlf input

4. Setting Up Git Aliases

  • Status Shortcut: git config --global alias.s "status -s"
  • Log Shortcut: git config --global alias.lg "log --oneline --all --graph --decorate"

5. Getting Started with Git

  • Initialize Repository: git init <project-name>
  • Add Files with Pattern: git add ab* (adds files starting with "ab")
  • Simple Log View: git log --oneline --decorate
    • Press q to exit log view

6. Managing Files in Git

  • Rename a File: git mv oldname.html newname.html
    • Alternative: mv oldname.css newname.css
  • Delete a File:
    • Using Git: git rm <filename>
    • Directly: rm <filename>, then update Git

Working with .gitignore

  • Exclude specific files or folders. Example:
    • !special.log (Don’t ignore special.log)

7. Branching, Merging, and Rebasing

  • Add & Commit in One Command: git commit -am "message"
    • Note: This won’t work for untracked files
  • Branch Management:
    • Check branches: git branch
    • Create a branch: git branch <new-branch-name>
    • Switch branches: git checkout <branch-name>

8. Difference Between Clone and Fork

  • Clone: Use if you don’t plan to contribute back or are a collaborator.
  • Fork: Required if you don’t know the repository owner and want to contribute back.

9. Creating Files with Content

  • Create and Write to a File: echo "Your text here" >> README.md

This organized guide should help streamline your understanding and usage of Git and GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment