Comprehensive Guide to Git and GitHub
Table of Contents:
1. Introduction to Git
2. Core Concepts
- Repository
- Commits
- Branches
- Tags
3. Basic Git Commands
4. Advanced Git Techniques
- Interactive Rebase
- Bisect
- Cherry-pick
- Stashing
5. GitHub Workflows
- Using Pull Requests
- Managing Releases
- Comparing Versions
- Tagging and Branch Management
6. Sharing Git Repositories with Non-Git Users
7. Git Cheat Sheet
8. Useful Resources
---
1. Introduction to Git
Git is a distributed version control system designed to handle projects of any size with speed and efficiency
---
2. Core Concepts
Repository:
A repository (repo) is a container for your project. It holds all the files, history, branches, and metadata rela
Commits:
Commits are snapshots of your project. Each commit represents a set of changes, tracked with a unique id
Branches:
Branches allow parallel development. The main branch (formerly master) is typically the production-ready b
Tags:
Tags are references to specific commits, often used for marking release versions (e.g., v1.0).
---
3. Basic Git Commands
Initialize Repository: `git init`
Clone Repository: `git clone <url>`
Stage Changes: `git add <file>`
Commit Changes: `git commit -m "Commit message"`
Check Status: `git status`
View Log: `git log`
Push Changes: `git push origin <branch>`
Pull Changes: `git pull origin <branch>`
---
4. Advanced Git Techniques
Interactive Rebase:
Rebasing lets you edit, combine, or reorder commits.
`git rebase -i HEAD~<n>`
Bisect:
Find the commit that introduced a bug.
`git bisect start`
`git bisect bad`
`git bisect good <commit>`
Cherry-pick:
Apply a specific commit to the current branch.
`git cherry-pick <commit-hash>`
Stashing:
Temporarily save changes without committing.
`git stash`
Retrieve stashed changes:
`git stash pop`
---
5. GitHub Workflows
Using Pull Requests:
Pull requests (PRs) are a way to propose and review changes.
1. Create a branch for your work.
2. Push the branch to GitHub.
3. Open a pull request from the branch into main.
Managing Releases:
1. Tag the release: `git tag -a v1.0 -m "Release v1.0"`
2. Push tags: `git push origin --tags`
Comparing Versions:
`git diff <commit1> <commit2>`
Branch Management:
`git branch -d <branch-name> # Delete local branch`
`git push origin --delete <branch-name> # Delete remote branch`
---
6. Sharing Git Repositories with Non-Git Users
To provide read-only access to a Git repository via a Linux directory:
1. Create a Shared Directory:
- Export the repository as a bare repository: `git clone --bare . /path/to/share`
- Share it via NFS or Samba.
2. Set Permissions:
- Ensure the directory is read-only for the users: `chmod -R 755 /path/to/share`
3. Automate Updates:
- Add a cron job to sync the latest updates to the shared directory.
---
7. Git Cheat Sheet
Setup Commands:
`git config --global user.name "Your Name"`
`git config --global user.email "your_email@example.com"`
`git config --list`
Branch Commands:
`git branch # List branches`
`git checkout <branch> # Switch branches`
`git checkout -b <branch> # Create and switch`
Commit and Staging Commands:
`git add <file>`
`git commit -m "Message"`
`git reset HEAD <file> # Unstage`
Remote Commands:
`git remote add origin <url>`
`git push -u origin <branch>`
`git fetch origin`
---
8. Useful Resources:
1. Pro Git Book (free online): https://git-scm.com/book/en/v2
2. GitHub Documentation: https://docs.github.com/
3. Oh My Git!: https://ohmygit.org/ (for gamified Git learning)