๐Ÿ“ฆ Asabeneh / git-github-beginners

โ˜… 17 stars โ‘‚ 7 forks ๐Ÿ‘ 17 watching
๐Ÿ“ฅ Clone https://github.com/Asabeneh/git-github-beginners.git
HTTPS git clone https://github.com/Asabeneh/git-github-beginners.git
SSH git clone git@github.com:Asabeneh/git-github-beginners.git
CLI gh repo clone Asabeneh/git-github-beginners
Asabeneh Asabeneh fix: improve logical flow and content in index.html d48fd26 6 months ago ๐Ÿ“ History
๐Ÿ“‚ main View all commits โ†’
๐Ÿ“„ .gitignore
๐Ÿ“„ index.html
๐Ÿ“„ README.md
๐Ÿ“„ README.md

Git and GitHub: A Comprehensive Guide for Beginners and Advanced Users

This guide provides a thorough introduction to Git and GitHub, covering essential commands, workflows, and advanced techniques. Whether you're new to version control or seeking to master complex Git operations, this document serves as a complete reference. All commands are designed for use in a terminal.

Introduction to Git and GitHub

Git is a distributed version control system that tracks changes to files, enabling multiple users to collaborate on projects efficiently. It records project history, allows branching for experimentation, and supports merging changes seamlessly.

GitHub is a platform for hosting Git repositories, facilitating collaboration through features like pull requests, issues, and code reviews. It integrates with Git to manage remote repositories.

This guide assumes basic familiarity with the command line. Commands are formatted as git <command> and can be run in a terminal (e.g., Bash, PowerShell, or Terminal.app).

Getting Started with Git

Installation and Setup

  • Install Git:
  • Windows: Download from git-scm.com and follow the installer.
  • MacOS: Install via Homebrew (brew install git) or Xcode Command Line Tools.
  • Linux: Use your package manager (e.g., sudo apt install git on Ubuntu).
  • Verify installation: git --version.
  • Configure Git:
Set your name and email for commit metadata:

git config --global user.name "Your Name"
   git config --global user.email "your.email@example.com"

Optionally, set your preferred editor:

git config --global core.editor "nano"

Initializing a Repository

  • git init
Creates a new Git repository in the current directory, initializing a .git folder to store version history.

git init

Checking Repository Status

  • git status
Displays the current state of the working directory and staging area, showing modified, staged, and untracked files.

git status

  • git log
Shows the commit history, including commit hashes, authors, dates, and messages.

git log

  • git log --oneline
Provides a compact, one-line summary of commits.

git log --oneline

Basic Git Operations

Staging and Committing

  • git add <filename>
Adds a specific file to the staging area for the next commit.

git add README.md

  • git add .
Stages all modified and new files in the current directory.

git add .

  • git commit -m "commit message"
Commits staged changes with a descriptive message (e.g., "Add initial project files").

git commit -m "Add initial project files"

  • git commit --amend
Modifies the most recent commit, allowing changes to the message or staged files.

git commit --amend -m "Updated commit message"

Viewing Changes

  • git diff
Shows differences between the working directory and the staging area.

git diff

  • git diff --staged
Displays changes in the staging area compared to the last commit.

git diff --staged

Undoing Changes

  • git reset <filename>
Unstages a file from the staging area but keeps changes in the working directory.

git reset README.md

  • git checkout -- <filename>
Discards changes to a file in the working directory, reverting to the last committed state.

git checkout -- README.md

  • git revert <commit>
Creates a new commit that undoes changes from a specified commit (identified by its hash).

git revert abc1234

  • git reset --hard <commit>
Resets the working directory and index to a specified commit, discarding all subsequent changes. Use with caution.

git reset --hard abc1234

Branching and Merging

Creating and Managing Branches

  • git branch <new-branch>
Creates a new branch named <new-branch>.

git branch feature-branch

  • git checkout <new-branch>
Switches to the specified branch.

git checkout feature-branch

  • git checkout -b <new-branch>
Combines creating and switching to a new branch.

git checkout -b feature-branch

  • git branch
Lists all branches, with the current branch marked by an asterisk.

git branch

  • git branch -d <branch>
Deletes a branch if it has been fully merged.

git branch -d feature-branch

Merging Branches

  • git merge <branch>
Merges the specified branch into the current branch. If conflicts arise, Git will pause and prompt for manual resolution.

git merge feature-branch

Working with Remote Repositories

Connecting to GitHub

  • Create a GitHub Repository:
  • Go to GitHub, sign in, and click "New repository."
  • Follow the prompts to create a repository (e.g., my-project).
  • Copy the repository URL (e.g., https://github.com/username/my-project.git).
  • Link Local Repository:
  • git remote add origin <url_of_remote_repo>
Links the local repository to the GitHub repository.

git remote add origin https://github.com/username/my-project.git

  • git remote -v
Lists all remote repositories and their URLs.

git remote -v

Cloning, Pushing, and Pulling

  • git clone <url_of_remote_repo>
Downloads a repository from GitHub to your local machine.

git clone https://github.com/username/my-project.git

  • git push -u origin <branch>
Pushes the specified branch to the remote repository and sets it as the upstream branch.

git push -u origin main

  • git pull origin <branch>
Fetches and merges changes from the remote branch into the current branch.

git pull origin main

  • git fetch origin
Retrieves updates from the remote repository without merging.

git fetch origin

Stashing Changes

  • git stash
Saves uncommitted changes (both staged and unstaged) to a temporary storage area, allowing you to switch branches.

git stash

  • git stash pop
Restores the most recently stashed changes and removes them from the stash.

git stash pop

  • git stash list
Lists all stashed changesets.

git stash list

Tagging

  • git tag <tagname>
Creates a lightweight tag at the current commit, useful for marking releases.

git tag v1.0.0

  • git tag -a <tagname> -m "tag message"
Creates an annotated tag with a message, providing additional metadata.

git tag -a v1.0.0 -m "Release version 1.0.0"

  • git push origin <tagname>
Pushes a specific tag to the remote repository.

git push origin v1.0.0

Advanced Git Concepts

Rebasing

Rebasing rewrites commit history by moving or combining commits, creating a cleaner, linear history compared to merging.

  • git rebase <branch>
Reapplies commits from the current branch onto the specified branch.

git rebase main

  • Interactive Rebase:
Allows editing, squashing, or reordering commits.

git rebase -i <commit>

Example: git rebase -i HEAD~3 to edit the last three commits.

Caution: Avoid rebasing commits that have been pushed to a shared repository, as it can disrupt collaborators' history.

Cherry-Picking

  • git cherry-pick <commit>
Applies a specific commit from another branch to the current branch.

git cherry-pick abc1234

Useful for selectively applying changes without merging an entire branch.

Git Hooks

Git hooks are scripts that run automatically at specific points in the Git workflow (e.g., before a commit or push). They are stored in the .git/hooks directory.

  • Example: Pre-Commit Hook
Create a pre-commit script to enforce code style checks:

# .git/hooks/pre-commit
  #!/bin/sh
  echo "Running code style checks..."
  # Add your checks here (e.g., linting)

Make it executable:

chmod +x .git/hooks/pre-commit

Interactive Rebase

Interactive rebasing allows you to modify commit history by squashing, reordering, or editing commits.

  • git rebase -i <commit>
Opens an editor to modify commits starting from the specified commit.

git rebase -i HEAD~3

Options include:

  • pick: Keep the commit.
  • squash: Combine with the previous commit.
  • edit: Pause to amend the commit.

Resolving Merge Conflicts

Merge conflicts occur when Git cannot automatically reconcile changes from two branches. To resolve:

  • Run git merge <branch> and note the conflicting files.
  • Open the files in your editor, where Git marks conflicts with <<<<<<<, =======, and >>>>>>>.
  • Edit the file to keep the desired changes.
  • Stage the resolved files: git add <filename>.
  • Complete the merge: git commit.

Best Practices and Tips

  • Write Clear Commit Messages: Use concise, descriptive messages (e.g., "Fix login bug in authentication module").
  • Commit Frequently, but Logically: Group related changes into single commits.
  • Use Branches for Features: Create separate branches for features, bug fixes, or experiments.
  • Pull Before Pushing: Always run git pull to avoid conflicts.
  • Review Changes: Use git diff and git status before staging or committing.
  • Backup Before Destructive Commands: Commands like git reset --hard are irreversible; ensure you have backups.
  • Leverage GitHub Features: Use pull requests for code reviews and issues for task tracking.
  • Keep .gitignore Updated: Prevent unwanted files (e.g., .env, node_modules) from being tracked.

Resources and Further Reading

Key Git Commands

  • git init
  • Initializes a new Git repository in the current directory, creating a .git folder for version history.
  • Example:
git init

  • git add <filename>
  • Stages a specific file for the next commit, preparing it for inclusion in the repository.
  • Example:
  • git add file1 file2 file3
-It is possible to add multiple or several files

git add README.md

  • git add .
  • Stages all modified and new files in the current directory for the next commit.
  • Example:
git add .

  • git commit -m "commit message"
  • Commits staged changes with a descriptive message to document the changes.
  • Example:
git commit -m "Add initial project files"

  • git remote add origin <github-url>
  • Links the local repository to a remote GitHub repository using the provided URL.
  • Example:
git remote add origin https://github.com/username/my-project.git

  • git push -u origin main
  • Pushes the local main branch to the remote repository and sets it as the upstream branch.
  • Example:
git push -u origin main

  • git pull
  • Fetches and merges changes from the remote repository into the current branch.
  • Example:
git pull

  • git branch
  • Lists all local branches, with the current branch marked by an asterisk.
  • Example:
git branch

  • git branch -a
  • Lists all branches, including local and remote branches.
  • Example:
git branch -a

  • git branch <branch-name>
  • Creates a new branch for developing features or fixes.
  • Example:
git branch feature-branch

  • git checkout <branch-name>
  • Switches to the specified branch to work on its changes.
  • Example:
git checkout feature-branch

  • git branch -d <branch-name>
  • Deletes a local branch if it has been fully merged.
  • Example:
git branch -d feature-branch

  • git push -d origin <branch-name>
  • Deletes a remote branch on GitHub.
  • Example:
git push -d origin feature-branch

  • git merge <branch-name>
  • Merges the specified branch into the current branch, combining their changes.
  • Example:
git merge feature-branch
  • git reset --hard HEAD~1
  • To remove the last git commit