Welcome to the comprehensive guide on mastering Git and GitHub. This guide will take you through the most beginner to advanced topics, helping you become proficient in version control,collaborative and open source development.
- Introduction to Git
- Setting Up Git
- Basic Git Commands
- Branching and Merging
- Remote Repositories
- Stashing and Cleaning
- Collaborating with GitHub
- Advanced Git Topics
- Git vs Github
- Git Under The Hood
- Git Hooks
- Resources
Git is a distributed version control system that helps you track changes in your code and collaborate with others, which was developed by Linus Torvalds. GitHub is a platform that hosts Git repositories, making it easier to share and manage code collaboratively.
- Linux:
sudo apt-get install git
- Mac:
brew install git
- Windows: For windows please navigate to the donwloads page for Git and download and execute the Installer, it will install the Git on you machine.
- Set your name:
git config --global user.name "Your Name"
- Set your email:
git config --global user.email "your.email@example.com"
- Check your settings:
git config --list
- Check your user level settings:
git config --global --list
- Check your system level settings:
git config --system --list
- Check your single local repo level settings:
git config --local --list
- Initialize a new repository:
git init
- Clone an existing repository:
git clone <repository-url>
- Check the status of your repository:
git status
- Add files to staging:
git add <file>
- Add all files to staging:
git add .
orgit add --all
- Commit changes:
git commit -m "Your commit message"
- View commit history:
git log
- Show changes in files:
git diff
- Show changes between commits:
git diff <commit1> <commit2>
- Show changes in the staging area:
git diff --staged
- List all branches:
git branch
- Create a new branch:
git branch <branch-name>
- Switch to a branch:
git checkout <branch-name>
- Create and switch to a new branch:
git checkout -b <branch-name>
- Delete a branch:
git branch -d <branch-name>
- Merge a branch into the current branch:
git merge <branch-name>
- Abort a merge:
git merge --abort
- Resolve merge conflicts manually, then add the resolved files:
git add <file>
, and commit.
- Add a remote repository:
git remote add origin <repository-url>
- View remote repositories:
git remote -v
- Push changes to a remote repository:
git push origin <branch-name>
- Pull changes from a remote repository:
git pull origin <branch-name>
- Fetch changes from a remote repository:
git fetch origin
- Stash changes:
git stash
- List stashes:
git stash list
- Apply a stash:
git stash apply
- Apply and drop the stash:
git stash pop
- Drop a stash:
git stash drop
- Remove untracked files:
git clean -f
- Remove untracked directories:
git clean -fd
- Perform a dry run to see what would be deleted:
git clean -n
- Navigate to the repository on GitHub.
- Click the "Fork" button at the top right.
- Clone your forked repository:
git clone <forked-repo-url>
- Make changes and commit them to your fork.
- Push changes to your forked repository:
git push origin <branch-name>
- Navigate to the original repository on GitHub.
- Click "New Pull Request" and follow the prompts.
- Rebase current branch onto another branch:
git rebase <branch>
- Abort a rebase:
git rebase --abort
- Continue a rebase after resolving conflicts:
git rebase --continue
- Apply the changes from a specific commit:
git cherry-pick <commit>
- Revert a commit:
git revert <commit>
- Reset to a previous commit:
git reset --hard <commit>
Feature | Git | GitHub |
---|---|---|
Type | Distributed Version Control System (Offline) | Web-based hosting service for Git repositories (Online) |
Functionality | Manages source code history and versioning locally and remotely | Adds collaboration features, such as pull requests and issues |
Installation | Installed on local machines | Accessed via web browser, with additional tools available for desktop |
Repository | Local and remote repositories | Remote repositories hosted on GitHub's servers |
Branching | Supports branching and merging | Provides visual tools for managing branches |
Collaboration | Primarily local, with push/pull to remote repositories | Facilitates team collaboration, code reviews, and discussions |
Access Control | SSH keys, HTTPS, and GPG | Granular access control and permissions |
Integration | Works with various CI/CD tools | Native integration with GitHub Actions, and third-party CI/CD tools |
Community | Command-line focused community | Large community with social coding features, such as following and stars |
Learning Curve | Steeper learning curve due to command-line interface | More accessible with a user-friendly interface |
The .git directory is a hidden folder that exists in the root of a Git repository. It contains all the metadata and information that Git needs to manage the version history of a project. Here’s a brief overview of its main components:
-
Configuration Files (config): Contains project-specific Git settings, such as remote repository URLs and branch information.
-
Object Store (objects/): Stores all the content tracked by Git in the form of objects, including commits, trees (directories), and blobs (files).
-
References (refs/): Tracks pointers to commit objects, such as branches, tags, and remote references.
-
Index (index): A staging area where changes are listed before they are committed to the repository. It acts as a snapshot of the working directory.
-
Logs (logs/): Contains a history of updates to the branches and references, allowing you to see how the repository has evolved over time.
-
HEAD: A special reference that points to the current branch or commit checked out in your working directory.
-
Hooks (hooks/): Contains scripts that can be triggered by specific Git events, such as commit, push, or merge. These scripts can automate tasks like code formatting or running tests.
-
Packed References (packed-refs): Stores a more compact representation of refs (branches, tags) when the number of references grows large.
Git hooks are scripts that Git executes before or after events such as commit, push, and receive. They allow you to automate tasks and enforce policies.There are client side and server side hooks. Git Client Hooks vs Server Hooks
Git hooks are scripts that run automatically at specific points in the Git workflow. They can be used to enforce policies, validate data, and automate tasks. There are two types of Git hooks: client-side hooks and server-side hooks.
Client-side hooks run on the developer's local machine, before the code is pushed to the remote repository. These hooks are stored in the .git/hooks directory of the local repository.
Advantages:
- Flexibility: Client-side hooks can be customized to fit individual developer needs.
- Performance: Since they run locally, client-side hooks don't impact the performance of the remote repository.
- Security: Client-side hooks can help prevent sensitive data from being committed to the repository.
Disadvantages:
- Lack of enforcement: Client-side hooks can be bypassed or disabled by developers.
- Inconsistency: Different developers may have different client-side hooks, leading to inconsistencies.
Server-side hooks run on the remote repository, after the code has been pushed. These hooks are stored in the hooks directory of the remote repository.
Advantages:
- Enforcement: Server-side hooks are enforced for all developers, ensuring consistency and compliance.
- Centralized management: Server-side hooks can be managed centrally, making it easier to maintain and update.
- Security: Server-side hooks can help prevent unauthorized changes to the repository.
Disadvantages:
- Performance impact: Server-side hooks can impact the performance of the remote repository.
- Limited flexibility: Server-side hooks are less flexible than client-side hooks, as they need to be compatible with all developers' environments.
- Code formatting: Enforce code formatting standards before committing code.
- Commit message validation: Validate commit messages to ensure they meet specific requirements.
- Sensitive data detection: Detect and prevent sensitive data from being committed to the repository.
- Access control: Enforce access control policies, such as restricting pushes to specific branches or users.
- Code review: Enforce code review policies, such as requiring approvals before merging code.
- Data validation: Validate data before accepting it into the repository.
In summary, client-side hooks are useful for enforcing individual developer policies and detecting sensitive data, while server-side hooks are better suited for enforcing centralized policies, access control, and data validation.
- Official Git Documentation
- Pro Git Book
- Git and Github Mastery Video Tutorial
- Git Internals
- Connecting Git and Github
This cheat sheet is for everyone be it beginner or a seasoned professional, who want to learn git or master git concepts, more updations will be added and we're expecting collaboration from other computer science enthusiasts.