Version control is a system that tracks changes to files over time so you can recall specific versions later. It's essential for collaborative work, backups, and tracking progress.
<!-- Initialize a Git repository in your project folder --> $ git init <!-- Creates a new local Git repository in the current directory -->
Version control systems can be categorized into three types:
<!-- Example of using a centralized system like SVN --> svn checkout http://example.com/repo <!-- Checks out a repository from a central server -->
Git is popular because it's fast, supports non-linear development (branches), works offline, and has robust merging and branching capabilities.
<!-- Cloning a Git repository --> git clone https://github.com/user/project.git <!-- Copies a Git repo from a remote server to your machine -->
Git offers better performance and flexibility compared to older systems like CVS and SVN. Unlike centralized systems, Git allows each user to have the full history of the project locally.
<!-- Show the full log of changes in Git --> git log <!-- Displays the history of commits in the repository -->
You can install Git by downloading it from the official website or using a package manager like `apt`, `brew`, or `choco`, depending on your OS.
<!-- Ubuntu/Debian --> sudo apt install git <!-- Installs Git using APT package manager --> <!-- macOS --> brew install git <!-- Installs Git using Homebrew --> <!-- Windows --> choco install git <!-- Installs Git using Chocolatey -->
Common terms in Git:
<!-- Basic Git workflow --> git add . <!-- Stages all modified files for commit --> git commit -m \"Init\" <!-- Commits the staged files with a message --> git push origin main <!-- Pushes changes to the remote 'main' branch -->
This step is essential to associate your identity with your Git commits. You configure your name and email address globally using Git commands.
git config --global user.name "Your Name" git config --global user.email "your.email@example.com"
Output: (No output if successful, values are set silently)
SSH keys allow secure communication with services like GitHub without using passwords every time.
ssh-keygen -t ed25519 -C "your.email@example.com" Enter file in which to save the key (/home/user/.ssh/id_ed25519): Enter passphrase (empty for no passphrase): Enter same passphrase again:
Output: The key files will be generated in your .ssh directory.
The .gitignore
file tells Git which files or directories to ignore in a repository.
node_modules/ *.log .env build/
Output: These files won't be tracked or committed to your Git repository.
Use this command to start a new Git repository in your current folder.
git init
Output: Initialized empty Git repository in /your/path/.git/
To copy an existing repository from GitHub or another remote source, use the clone command.
git clone https://github.com/username/repository.git
Output: Directory "repository" created with all files from the remote repo.
The basic Git workflow involves editing files, staging changes, committing them, and pushing to a remote.
git status git add filename.txt git add . git commit -m "Your commit message" git push origin main
Output: Files are committed and pushed to the remote repository.
The working directory is where you edit files. The staging area is a place where you prepare a snapshot of changes before committing them. Git tracks these changes separately.
<!-- Check the current state of the working directory and staging area --> git status <!-- Shows untracked, modified, and staged files -->
To stage changes, you use the git add
command. It tells Git to include changes in the next commit.
<!-- Add a specific file to the staging area --> git add index.html <!-- Stages 'index.html' for commit --> <!-- Add all changes in the working directory --> git add . <!-- Stages all modified and new files -->
Committing saves your staged changes to the repository history. Each commit includes a message describing the change.
<!-- Commit staged changes with a message --> git commit -m \"Add homepage layout\" <!-- Commits the staged changes with a descriptive message -->
You can view the history of commits in your repository using the git log
command.
<!-- Show the full commit history --> git log <!-- Displays all previous commits with details like hash, author, and date --> <!-- Show concise commit history --> git log --oneline <!-- Displays a simplified one-line summary of each commit -->
Every commit in Git has a unique SHA-1 hash (commit ID) that identifies it. You can use this ID to reference or revert to a specific state.
<!-- Example of using commit ID to check out a previous state --> git checkout a1b2c3d4 <!-- Switches to the commit with ID a1b2c3d4 (detached HEAD state) -->
Git provides commands to undo changes in the working directory, staging area, or even to reset commits.
<!-- Unstage a file --> git reset index.html <!-- Removes 'index.html' from staging but keeps the file changes --> <!-- Discard changes in a file --> git checkout -- index.html <!-- Reverts 'index.html' to the last committed version --> <!-- Undo the last commit but keep changes staged --> git reset --soft HEAD~1 <!-- Moves HEAD back one commit, preserving the changes --> <!-- Undo the last commit and discard changes --> git reset --hard HEAD~1 <!-- Permanently removes the last commit and all associated changes -->
A branch in Git allows you to diverge from the main codebase to develop features or test ideas independently. The default branch is usually called main
or master
.
git branch * main feature-x
You can create a new branch to work on a feature or bug fix. Switching to a branch allows you to work in that isolated environment.
git branch feature-login git checkout feature-login git checkout -b feature-login
Output: Switched to branch 'feature-login'
After finishing work in a branch, you can merge it into another branch (typically the main branch).
git checkout main git merge feature-login
Output: Fast-forward or Merge commit message depending on history.
Conflicts occur when the same parts of a file are changed in different branches. Git will ask you to resolve them manually.
<<<<<<< HEAD Current content ======= Incoming content from merged branch >>>>>>> feature-login git add conflicted-file.txt git commit
Output: Merge made successfully, or message about manual resolution.
Once a branch is merged and no longer needed, you can delete it to keep your repository clean.
git branch -d feature-login git branch -D feature-login
Output: Deleted branch 'feature-login' (was 1a2b3c4d)
Using strategies helps teams manage development effectively. Common strategies include:
main develop feature/* release/* hotfix/* git checkout -b feature/add-login develop
Output: Branching aligned with workflow structure and naming conventions.
This involves linking a local Git repository to a remote one, such as a repository hosted on GitHub or GitLab. This enables collaboration and remote storage of your code.
# Command to add a remote repository git remote add origin https://github.com/user/repo.git # Adds a remote named 'origin' pointing to the given URL
Fetching downloads the latest data from the remote repository without merging it into your local branch. Pulling is a combination of fetch and merge, which updates your local branch with remote changes.
# Fetching updates from remote without merging git fetch origin # Downloads commits, files, and refs from the remote repo # Pulling updates (fetch + merge) git pull origin main # Fetches and merges changes from the 'main' branch of 'origin'
This sends your committed changes to the remote repository. You must push to share your updates with others.
# Pushing local changes to the remote repository git push origin main # Pushes your local 'main' branch to the remote 'origin'
Remote tracking branches let you keep tabs on changes in the remote. When you clone a repository, Git automatically sets up tracking.
# Checking remote-tracking branches git branch -r # Lists all remote-tracking branches # Setting up a local branch to track a remote branch git branch --set-upstream-to=origin/main # Sets the local branch to track 'origin/main'
You can remove a remote if it is no longer needed or if the URL has changed.
# Removing a remote git remote remove origin # Deletes the remote named 'origin'
Team collaboration with Git typically involves using feature branches, pull requests, and frequent pushes/pulls to ensure everyone stays in sync.
# Cloning a shared repository git clone https://github.com/team/repo.git # Copies the remote repository to your local system # Creating a feature branch git checkout -b feature-new-ui # Creates and switches to a new branch for your feature # Committing and pushing changes git add . # Stages all changes git commit -m "Add new UI feature" # Commits with a message git push origin feature-new-ui # Pushes your feature branch for review
Rebasing is used to move or combine a sequence of commits to a new base commit. It provides a cleaner project history by linearizing commit sequences.
<!-- Rebase current branch onto 'main' --> git rebase main <!-- Applies current branch commits on top of 'main' branch --> <!-- Abort the rebase if there are conflicts or issues --> git rebase --abort <!-- Cancels the rebase process and restores original state --> <!-- Continue the rebase after resolving conflicts --> git rebase --continue <!-- Continues rebasing after resolving merge conflicts -->
Git stash allows you to save uncommitted changes temporarily so you can work on something else and come back later.
<!-- Stash current changes --> git stash <!-- Saves uncommitted changes to a stash stack --> <!-- Apply the most recent stash --> git stash apply <!-- Applies the latest stash without removing it --> <!-- Apply and remove the stash --> git stash pop <!-- Applies and deletes the most recent stash from the stack --> <!-- List all stashes --> git stash list <!-- Shows all stashed changes -->
Tags are used to mark specific points in history as important. Often used to mark release versions.
<!-- Create a lightweight tag --> git tag v1.0 <!-- Tags the latest commit with 'v1.0' --> <!-- Create an annotated tag with a message --> git tag -a v1.1 -m "Version 1.1 Release" <!-- Annotated tag with description --> <!-- Push tags to remote repository --> git push origin v1.1 <!-- Pushes tag 'v1.1' to the remote --> <!-- View all tags --> git tag <!-- Lists all available tags -->
Git hooks are scripts that run automatically on certain git events like commits or merges. They help automate tasks such as testing or formatting code.
<!-- Example hook location --> .git/hooks/pre-commit <!-- Script here runs before every commit --> <!-- Example: pre-commit hook content --> #!/bin/sh <!-- Starts a shell script --> npm test <!-- Runs tests before allowing a commit -->
Submodules allow you to keep a Git repository as a subdirectory of another repository. This is useful for managing dependencies.
<!-- Add a submodule --> git submodule add https://github.com/example/lib.git lib-folder <!-- Adds a repo as a submodule --> <!-- Initialize submodules after cloning --> git submodule init <!-- Initializes local config for submodules --> <!-- Update all submodules --> git submodule update <!-- Downloads the submodule content -->
Git stores data in objects such as blobs, trees, and commits. References like branches and tags point to these objects.
<!-- Show internal object information --> git cat-file -t HEAD <!-- Shows the type of object at HEAD (usually a commit) --> <!-- View content of an object --> git cat-file -p HEAD <!-- Prints the commit object at HEAD --> <!-- List internal objects --> git rev-parse HEAD <!-- Returns the SHA-1 hash of the current commit -->
GitHub is a web-based platform that hosts Git repositories and provides tools for collaboration, issue tracking, pull requests, and project management.
GitHub = Git (version control) + Hub (social coding platform)
To use GitHub, you first need to create a free account on github.com.
1. Go to <a href="https://github.com">github.com</a> 2. Click <Sign up> at the top right corner. 3. Enter your email, create a username and password. 4. Verify your email address to activate your account.
Once logged in, GitHub provides several interface elements:
<Top Navigation Bar> <Repositories Section> <Profile Page> <Settings>
A repository is where your project files and version history live. You can create one via the web interface.
1. Click <New> under the Repositories tab. 2. Enter a repository name (e.g., my-first-repo). 3. Optionally add a description. 4. Choose public or private. 5. Optionally initialize with README. 6. Click <Create repository>.
You can customize your GitHub profile to showcase your work and identity.
1. Click on your profile picture > Settings 2. Update your Name, Bio, Location, Company, etc. 3. Add a README to a repo named the same as your username to create a profile readme. <h1>Hi, I'm John Doe 👋</h1> <p>I build cool web apps using JavaScript and Python.</p>
Public repositories are visible to everyone, while private repositories are accessible only to you and invited collaborators.
<Public> <Private> 1. Go to repository > Settings > Danger Zone 2. Click <Change repository visibility> 3. Select Public or Private
Cloning means creating a local copy of a GitHub repository. This allows you to make changes and push them back to GitHub.
# Clone a GitHub repository to your local machine git clone https://github.com/username/repository.git # Replace with your GitHub URL
This allows you to take a repository from your local machine and push it to GitHub, usually after initializing it with git init
.
# Initialize the local repository git init # Creates a new Git repository in the current directory # Add remote origin to push to GitHub git remote add origin https://github.com/username/repository.git # Links to remote repo # Add and commit changes git add . # Stages all files git commit -m "Initial commit" # Adds a message for the commit # Push to GitHub git push -u origin main # Pushes your code to the 'main' branch on GitHub
Branches on GitHub are useful for managing features, experiments, and bug fixes separately from the main codebase.
# Create and switch to a new branch git checkout -b new-feature # Creates and moves to 'new-feature' branch # Push branch to GitHub git push origin new-feature # Sends the branch to GitHub # View branches git branch -a # Shows all local and remote branches
GitHub repository settings allow you to manage features like branch protection, collaborators, repository visibility, and more. These are managed via GitHub UI, not Git CLI.
# No Git command for this. Use GitHub web interface: # Go to GitHub.com > Open your repository > Click on "Settings" # Adjust: # - Description and topics # - Branch protections # - Access permissions # - Secrets and webhooks
These files help in documentation, licensing, and excluding unnecessary files from being tracked by Git.
# Create a README file echo "# Project Title" > README.md # Adds a heading to the README # Create a License file echo "MIT License" > LICENSE # Adds license text (replace with full license later) # Create a .gitignore file echo "node_modules/" > .gitignore # Ignores the node_modules directory # Add and commit these files git add . # Stages all new files git commit -m "Add README, LICENSE, and .gitignore" # Commit message git push # Pushes to GitHub
GitHub lets you mark a repository as a template, so others can generate new repositories based on it. This is useful for bootstrapping similar projects.
# No CLI command. Follow these GitHub UI steps: # 1. Open the repository on GitHub # 2. Go to "Settings" # 3. Check the box "Template repository" # To use a template: # 1. Click "Use this template" on the repo page # 2. Name your new repository # 3. Click "Create repository from template"
Forking creates a personal copy of someone else’s repository on your GitHub account. It allows you to freely make changes without affecting the original project.
<!-- No command for forking from CLI --> <!-- Use GitHub UI: Click "Fork" button on the top-right of a repository page -->
When you click the "Fork" button on GitHub, it duplicates the repository under your account. You can then clone and work on it locally.
<!-- Clone the forked repository to your machine --> git clone https://github.com/your-username/forked-repo.git <!-- Downloads the forked repo to local system --> cd forked-repo <!-- Navigate into the forked project directory -->
Over time, the original (upstream) repository might have changes. You need to sync your fork to keep it up to date.
<!-- Add the original repository as a remote called 'upstream' --> git remote add upstream https://github.com/original-owner/original-repo.git <!-- Links the source repo as 'upstream' --> <!-- Fetch the latest changes from upstream --> git fetch upstream <!-- Downloads changes from the original repository --> <!-- Merge upstream changes into your local main branch --> git checkout main <!-- Switch to your main branch --> git merge upstream/main <!-- Merge changes from upstream into your branch -->
After making changes to your fork, you can open a Pull Request (PR) to propose merging your changes into the original repository.
<!-- Commit and push your changes to your fork --> git add . <!-- Stages all modified files --> git commit -m "Added new feature" <!-- Commits the changes with a message --> git push origin main <!-- Pushes changes to your fork's main branch --> <!-- Open GitHub and click "Compare & pull request" button --> <!-- Provide title, description, and submit the PR -->
Repository maintainers or collaborators can review PRs. They can comment, suggest changes, or approve them.
<!-- Use GitHub interface to review --> <!-- Options include: Comment, Approve, or Request changes --> <!-- Leave feedback in the Files Changed tab or main conversation -->
Once a pull request is reviewed and approved, it can be merged into the main branch of the original repository.
<!-- Use GitHub interface to merge --> <!-- Options: Create a merge commit, squash, or rebase --> <!-- Click "Merge pull request" and then "Confirm merge" -->
Issues help track bugs, feature requests, or tasks within a repository.
1. Navigate to the Issues tab in your repository. 2. Click <New issue> button. 3. Add a title and description explaining the problem or request. 4. Submit the issue. Bug: Login page crashes on invalid input
Labels categorize issues by type, priority, or status. Milestones group issues under a common goal or release.
1. In the Issues tab, select an issue. 2. Click <Labels> on the right sidebar. 3. Choose or create labels like "bug", "enhancement", or "urgent". 1. Click <Milestones> tab in Issues. 2. Create a milestone (e.g., Version 1.0). 3. Assign issues to this milestone to track progress.
You can assign issues to yourself or collaborators to indicate responsibility.
1. Open an issue. 2. Click <Assignees> on the right sidebar. 3. Select yourself or team members from the list. Assigned to @username
GitHub Projects offer Kanban-style boards to organize issues, pull requests, and notes.
1. Go to the <Projects> tab in your repository or profile. 2. Click <New project>. 3. Choose "Board (Kanban)" template. 4. Add columns like "To do", "In progress", "Done". 5. Add cards by linking issues, PRs, or notes.
Issue templates help standardize the information contributors provide when opening new issues.
1. Create a <.github/ISSUE_TEMPLATE> folder in your repository. 2. Add markdown files like "bug_report.md" and "feature_request.md". 3. Each template pre-fills issue form with relevant fields. <!-- Bug Report Template --> ## Description Describe the bug here. ## Steps to Reproduce 1. Step one 2. Step two ## Expected Behavior What you expected to happen.
Issues can be closed when resolved and reopened if the problem persists.
1. Go to the issue page. 2. Click <Close issue> button at the bottom. 1. Open a closed issue. 2. Click <Reopen issue> button. git commit -m "Fix login bug. Closes #23"
GitHub Actions allow you to automate tasks like building, testing, and deploying code directly from your GitHub repository.
# GitHub Actions use YAML files to define workflows stored in .github/workflows/ # They run on GitHub-hosted runners or self-hosted runners to automate tasks
Here is a simple workflow example that runs tests on every push to the main branch.
name: CI # Names the workflow on: # Defines events that trigger the workflow push: # Runs when code is pushed branches: [ main ] # Only on pushes to 'main' branch jobs: # Defines the jobs in this workflow build: # Job named 'build' runs-on: ubuntu-latest # Runs on the latest Ubuntu runner steps: # Steps to run in this job - uses: actions/checkout@v3 # Checks out the repo code - name: Run tests # Step to run tests run: | echo "Run your test commands here" # Placeholder for test commands
GitHub Marketplace offers pre-built actions that simplify your workflow setup, such as setting up languages, sending notifications, or deploying.
# Example: Using the setup-node action to set Node.js version - uses: actions/setup-node@v3 # Uses the Node.js setup action with: node-version: '16' # Specifies Node.js version 16
CI means automatically building and testing your code after every change to detect problems early.
# A typical CI workflow runs on every push or pull request on: [push, pull_request] # Triggers on push or PR jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 # Checkout code - name: Install dependencies run: npm install # Install project dependencies - name: Run tests run: npm test # Run your test suite
You can extend workflows to deploy your code automatically after successful tests.
jobs: build-and-deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Build project run: npm run build # Build your project - name: Deploy run: ./deploy.sh # Run a deploy script (replace with your deployment commands)
Secrets like API keys or passwords are stored securely in GitHub and accessed in workflows using environment variables.
# To use secrets in a workflow: jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Deploy with secret env: API_KEY: ${{ secrets.API_KEY }} # Access secret named API_KEY run: | echo "Using API key $API_KEY to deploy" # Example usage of secret
You can invite other GitHub users to collaborate on your repository by adding them as collaborators.
<!-- On GitHub repository page: Go to Settings > Manage Access > Invite a collaborator --> <!-- Enter GitHub username or email, then send invite -->
Collaborators can have different permission levels like Read, Write, or Admin, controlling what actions they can perform.
<!-- Access levels explanation --> <!-- Read: Can view and clone repo --> <!-- Write: Can push changes and create branches --> <!-- Admin: Full control including managing settings and collaborators -->
Branch protection rules enforce workflows and restrictions on branches to prevent unwanted changes.
<!-- On GitHub: Settings > Branches > Add branch protection rule --> <!-- Example rules: Require pull request reviews before merging --> <!-- Prevent force pushes or deletions -->
You can require code reviews and passing status checks before pull requests can be merged.
<!-- Enable required reviews in branch protection settings --> <!-- Integrate CI tools (like GitHub Actions) for automated status checks --> <!-- Merging blocked until reviews and checks pass -->
Within organizations, teams can be created with specific access permissions to multiple repositories.
<!-- Organization owner creates teams in GitHub Organization Settings --> <!-- Assign repositories and permissions per team --> <!-- Simplifies managing large groups of collaborators -->
GitHub provides audit logs to track collaborator activities, including access changes, pushes, and pull requests.
<!-- Available for organizations on GitHub Enterprise or paid plans --> <!-- Access audit logs via Organization Settings > Audit Log --> <!-- Helps ensure security and compliance -->
GitHub Pages is a static site hosting service that lets you publish web pages directly from a GitHub repository.
<!-- GitHub Pages hosts static HTML, CSS, JS files directly from your repo --> <!-- Great for project sites, portfolios, and documentation -->
To create a GitHub Pages site, you enable it in your repository settings and choose a source branch.
<!-- On GitHub repo: Settings > Pages > Source --> <!-- Select branch (main or gh-pages) and folder (root or /docs) --> <!-- Save changes to publish your site -->
You can link a custom domain name to your GitHub Pages site by configuring DNS records.
<!-- Add a CNAME file with your custom domain to your repo root --> <!-- Example content of CNAME file --> mycustomdomain.com <!-- Configure DNS: Create CNAME or A records pointing to GitHub Pages servers -->
Markdown is a lightweight markup language used for writing formatted text, often used in README files and documentation.
<!-- Example markdown syntax: --> # Heading 1 ## Heading 2 **Bold text** *Italic text* - List item 1 - List item 2 <!-- Rendered as HTML by GitHub automatically -->
GitHub provides built-in wikis for repositories, and you can also maintain docs using markdown files in a docs folder.
<!-- Wiki tab in GitHub repo lets you create and edit pages --> <!-- Docs can live in a /docs folder with markdown files --> <!-- Both support collaborative editing and version control -->
Jekyll is a static site generator integrated with GitHub Pages, allowing you to build more complex, templated websites.
<!-- Add _config.yml to configure Jekyll site --> <!-- Use layouts, includes, and markdown files --> <!-- GitHub Pages automatically builds and serves your Jekyll site -->
The GitHub CLI (gh) tool allows you to interact with GitHub from your terminal.
brew install gh sudo apt install gh gh --version
Some useful commands for daily GitHub tasks using CLI.
gh repo clone username/repository gh repo create my-new-repo --public gh issue list gh issue create --title "Bug report" --body "Description of the bug" gh pr create --title "Feature X" --body "Add new feature X"
Before using gh commands, you need to authenticate with your GitHub account.
gh auth login gh auth status
You can script and automate repetitive GitHub workflows using the CLI.
gh issue create --title "Automated issue" --body "Created by script" gh repo create auto-repo --public --description "Repo created by script"
The GitHub REST API allows programmatic access to GitHub data and actions.
curl -H "Accept: application/vnd.github.v3+json" https://api.github.com/users/username { "login": "username", "id": 123456, "public_repos": 10, ... }
GitHub’s GraphQL API allows more flexible queries than REST.
{ user(login: "username") { name bio repositories(first: 5) { nodes { name description } } } }
2FA adds an extra layer of security by requiring a second form of verification besides your password.
# To enable 2FA on GitHub: # 1. Go to GitHub Settings > Security > Two-factor authentication # 2. Choose authentication method (e.g., authentication app or SMS) # 3. Follow setup instructions to secure your account
SSH keys allow secure access to GitHub without using passwords. Always protect your private keys.
# Generate a new SSH key pair ssh-keygen -t ed25519 -C "your_email@example.com" # Creates a new SSH key with your email # Start the ssh-agent in the background eval "$(ssh-agent -s)" # Starts the ssh-agent # Add your SSH private key to the ssh-agent ssh-add ~/.ssh/id_ed25519 # Adds the private key to the agent # Copy the public key to add to GitHub cat ~/.ssh/id_ed25519.pub # Displays the public key for GitHub settings
Tokens replace passwords for GitHub API and Git CLI operations, especially when 2FA is enabled.
# To create a personal access token (PAT): # 1. Go to GitHub Settings > Developer settings > Personal access tokens # 2. Click "Generate new token" # 3. Select scopes (permissions) for the token # 4. Copy the token and use it instead of a password for Git operations # Example usage: git clone https://username:<token>@github.com/username/repo.git # Use token in place of password
Branch protection rules help prevent force pushes and require reviews before merging changes on critical branches.
# To enable branch protection: # 1. On GitHub, go to your repository > Settings > Branches # 2. Under "Branch protection rules", click "Add rule" # 3. Specify the branch name pattern (e.g., main) # 4. Enable protections like: # - Require pull request reviews before merging # - Require status checks to pass before merging # - Restrict who can push to matching branches
GitHub’s code scanning automatically analyzes your code for security vulnerabilities and bugs.
# To enable code scanning: # 1. Go to your GitHub repository > Security > Code scanning alerts # 2. Set up a scanning workflow or use GitHub Advanced Security features # 3. Review alerts and fix issues found in your codebase
Secret scanning helps detect accidentally committed sensitive data like API keys and passwords.
# To enable secret scanning: # 1. Go to your GitHub repository > Settings > Security > Secret scanning # 2. Enable scanning for your repository # 3. GitHub will scan commits and alert if secrets are detected
This workflow uses a single central repository where all collaborators push changes directly to main branches.
<!-- Developers clone the central repo --> <!-- Make changes locally, then git push to central repo --> <!-- Simple but risks conflicts if multiple push at once -->
Each feature is developed in its own branch, isolated from main branches, and merged back when ready.
<!-- Create a feature branch --> git checkout -b feature/my-feature <!-- Work and commit on feature branch --> git add . git commit -m "Add new feature" <!-- Merge feature branch back to main --> git checkout main git merge feature/my-feature
A robust branching model with dedicated branches for features, releases, and hotfixes.
<!-- Main branches: main (production), develop (integration) --> <!-- Feature branches branch off develop --> <!-- Release branches prepare production releases --> <!-- Hotfix branches fix production issues -->
Each contributor forks the main repo, works on changes, then submits pull requests for review.
<!-- Fork repo on GitHub --> <!-- Clone your fork locally --> git clone https://github.com/yourusername/repo.git <!-- Push changes to your fork and open pull request --> git push origin your-branch
Use clear, concise commit messages to describe changes for easier collaboration.
<!-- Format: --> <!-- feat: Add new login feature --> <!-- fix: Correct typo in README --> <!-- docs: Update API documentation --> <!-- style: Format code with Prettier -->
Code reviews improve code quality and knowledge sharing; reviewers should provide constructive feedback.
<!-- Review for correctness, style, performance, and security --> <!-- Ask questions if unclear --> <!-- Approve only when ready --> <!-- Keep comments respectful and clear -->
You can undo recent commits or staged changes with these commands.
git reset --soft HEAD~1 git reset --hard HEAD~1 git reset HEAD filename.txt git checkout -- filename.txt
Merge conflicts occur when changes collide; you must manually fix and commit them.
git status <<<<<<< HEAD Your changes here ======= Incoming changes here >>>>>>> branch-name git add filename.txt git commit
If you deleted a branch accidentally, you can recover it if the commits still exist.
git reflog git branch feature <commit-hash> git checkout feature
A detached HEAD means you are not on a branch; you can fix it by creating a branch.
git checkout -b new-branch git checkout main
Remove unnecessary files and optimize the repository.
git clean -f git clean -fd git gc --prune=now --aggressive
Git Large File Storage (LFS) manages big files efficiently in Git repos.
git lfs install git lfs track "*.psd" git add .gitattributes git commit -m "Track PSD files with Git LFS" git push origin main
Effective collaboration in Git requires clear workflows such as Git Flow, feature branching, or trunk-based development.
# Example: Using feature branches for collaboration git checkout -b feature/new-feature # Create and switch to a new feature branch # Work on the feature and commit changes git add . # Stage changes git commit -m "Add new feature implementation" # Commit changes # Push the feature branch to remote git push origin feature/new-feature # Push branch for review and collaboration
Pull Requests (PRs) let team members review, discuss, and approve code changes before merging into main branches.
# Typical Pull Request workflow: # 1. Push your branch to remote git push origin feature/new-feature # 2. Open a PR on GitHub from feature/new-feature to main branch # 3. Team members review and comment on PR # 4. After approval, merge PR to main branch via GitHub UI or CLI
Managing many contributors involves setting clear roles, permissions, and ensuring contributors work on separate branches.
# Example: Add a contributor to your GitHub repo # 1. Go to repository Settings > Manage access # 2. Invite collaborators by username or email # 3. Assign roles: Admin, Write, Read, etc. # Use branch permissions to protect main branches
Good communication reduces conflicts and improves team productivity. Use clear commit messages, descriptive PR titles, and status updates.
# Example commit message style: git commit -m "Fix bug: resolve issue with login validation" # Clear and descriptive # Use issue comments and PR discussions for detailed explanations # Keep team updated on progress via Slack, Teams, or emails
Tags mark specific points in history as releases, helping track versions and deployments.
# Create a new annotated tag for a release git tag -a v1.0.0 -m "Release version 1.0.0" # Tag current commit with message # Push tags to remote git push origin v1.0.0 # Share tag with collaborators # List tags git tag # Show all tags in repository
Protected branches prevent force pushes, require PR reviews, and enforce status checks to safeguard important branches like main.
# To enable branch protection on GitHub: # 1. Go to repository Settings > Branches # 2. Add branch protection rules for branches like main or release # 3. Enable settings such as: # - Require pull request reviews before merging # - Require status checks to pass before merging # - Restrict who can push to the branch
GitHub Sponsors allows you to financially support open source contributors directly through GitHub.
<!-- Visit sponsors.github.com to set up sponsorship --> <!-- Users can sponsor individuals or projects --> <!-- Helps fund ongoing open source development -->
Discussions provide a forum-like space within repositories for community engagement beyond issues and pull requests.
<!-- Enable Discussions tab in repo settings --> <!-- Use to ask questions, share ideas, and collaborate --> <!-- Categories and labels help organize discussions -->
Codespaces provide instant, cloud-hosted development environments accessible directly from GitHub.
<!-- Click the 'Code' button in GitHub repo, then 'Open with Codespaces' --> <!-- Full VS Code experience in browser or desktop app --> <!-- Preconfigured dev environments speed up onboarding -->
Advanced Projects enable customizable Kanban boards and workflows for issue and pull request tracking.
<!-- Create projects from repo or organization level --> <!-- Automate workflows with custom fields and automation rules --> <!-- Integrate with issues, pull requests, and external tools -->
Templates provide reusable repository setups, and Actions Marketplace offers automation workflows you can add to your repos.
<!-- Use repo templates to quickly create new projects --> <!-- Explore Actions Marketplace for CI/CD, linting, and more --> <!-- Customize workflows using YAML configuration -->
GitHub Packages is a package hosting service that supports multiple package types for easy distribution.
<!-- Publish and consume packages within GitHub repos --> <!-- Supports npm, Docker, Maven, NuGet, and more --> <!-- Control access and versioning via GitHub permissions -->
Continuous Integration (CI) automates testing of code changes, and Continuous Deployment (CD) automates releasing code to production.
GitHub Actions lets you define workflows that run on events like pushes or pull requests.
name: CI on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 # Checkout repository code - name: Set up Node.js uses: actions/setup-node@v2 with: node-version: '14' # Setup Node.js environment - name: Install dependencies run: npm install # Install project dependencies - name: Run tests run: npm test # Run unit tests
Automate building your software and running tests to ensure quality before deployment.
- name: Run lint run: npm run lint - name: Build project run: npm run build
Define workflows to deploy your application automatically after successful tests.
jobs: deploy: needs: build runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 # Checkout code for deployment - name: Deploy to server run: | ssh user@server "cd /var/www/app && git pull && systemctl restart app" # Deploy code by SSH and restart service
Monitor your CI/CD workflows via GitHub Actions UI to check success or failure of each job.
Set up notifications and rollback procedures in case deployments fail.
- name: Notify on failure if: failure() uses: some-notification-action@v1 with: message: "Deployment failed" ssh user@server "cd /var/www/app && git reset --hard HEAD~1 && systemctl restart app"
Git stores data as objects: blobs (file contents), trees (directories), commits, and tags.
# List objects in Git database git cat-file -p <object-hash> # View content of an object by hash # Example: show the commit object for HEAD git cat-file -p HEAD # Displays commit metadata and tree hash
A commit points to a tree object representing the snapshot of the project at that commit, plus metadata like author and message.
# Show the tree object linked to a commit git cat-file -p <commit-hash> # Shows commit details including tree hash # Show tree contents git cat-file -p <tree-hash> # Lists files and subtrees in the tree object
Git stores content as compressed objects in the .git/objects directory, using SHA-1 hashes as keys.
# Explore raw Git objects directory ls .git/objects # Shows directories named after first 2 chars of hashes # Objects are stored as compressed files by hash suffix # Git reconstructs repository state using these objects
Git packs multiple objects into packfiles for efficient storage and runs garbage collection to clean unused objects.
# Manually run garbage collection git gc # Compresses objects and cleans unreferenced data # View packfiles ls .git/objects/pack # Shows packfiles storing compressed objects # To repack objects manually git repack -a -d --depth=250 --window=250 # Optimizes packfiles
Reflogs record updates to branch tips and other refs, allowing recovery of lost commits.
# Show reflog entries git reflog # Lists recent updates to HEAD # Recover a commit using reflog git checkout <reflog-hash> # Checkout a commit from reflog if lost
Low-level commands allow direct interaction with Git objects and refs for advanced operations.
# Show object type git cat-file -t <object-hash> # Outputs type: commit, tree, blob, or tag # Create a new blob object from a file git hash-object -w <filename> # Writes file content as a blob to object store # Update refs manually git update-ref refs/heads/new-branch <commit-hash> # Create/update branch pointer
Aliases allow you to create shortcuts for frequently used Git commands to save time.
<!-- Create an alias for git status --> git config --global alias.st status <!-- Now you can type git st instead of git status -->
Git hooks are scripts that run automatically on certain Git events like commits or pushes.
<!-- Example: pre-commit hook to run tests before commit --> <!-- Create file .git/hooks/pre-commit and make it executable --> <!--!/bin/sh npm test if [ $? -ne 0 ]; then echo "Tests failed, commit aborted." exit 1 fi
You can configure Git to use external tools for comparing and merging files.
<!-- Set custom diff tool (e.g., meld) --> git config --global diff.tool meld <!-- Use custom merge tool --> git config --global merge.tool meld <!-- Launch diff tool --> git difftool <!-- Launch merge tool --> git mergetool
Use templates for new repositories and .gitignore files to exclude files from tracking.
<!-- Set a global gitignore file --> git config --global core.excludesfile ~/.gitignore_global <!-- Example .gitignore_global content --> node_modules/ .DS_Store *.log
Git integrates well with various IDEs, CI/CD systems, and third-party tools to improve workflow.
<!-- Example: integrating Git with VS Code --> <!-- Open project in VS Code, use built-in Git features --> <!-- Example: setting up Git with Jenkins for CI --> <!-- Jenkins pulls repo and runs build scripts automatically -->
Configure Git settings for better performance on large repositories.
<!-- Increase pack window memory for faster operations --> git config --global pack.windowMemory "100m" <!-- Increase delta cache size --> git config --global pack.deltaCacheSize "100m" <!-- Enable multi-threading --> git config --global pack.threads "4"
Start by creating a public repository for your open source project and initialize it properly.
git clone git@github.com:username/repo-name.git echo "# Project Title" > README.md git add README.md git commit -m "Add README" git push origin main
Contribution guidelines help contributors understand how to contribute properly.
echo "# Contribution Guidelines" > CONTRIBUTING.md git add CONTRIBUTING.md git commit -m "Add contribution guidelines" git push origin main
Engage with contributors through issues, discussions, and pull requests.
Create templates to standardize issues and pull requests.
<!-- Example bug report template content --> <!-- Example PR template content --> git add .github/ISSUE_TEMPLATE/bug_report.md .github/PULL_REQUEST_TEMPLATE.md git commit -m "Add issue and PR templates" git push origin main
Choose an open source license and add a code of conduct to set community standards.
echo "MIT License content" > LICENSE echo "# Code of Conduct" > CODE_OF_CONDUCT.md git add LICENSE CODE_OF_CONDUCT.md git commit -m "Add license and code of conduct" git push origin main
Promote your open source project via social media, blogs, and GitHub topics.
GitHub Enterprise provides a self-hosted or cloud-hosted platform tailored for large organizations with enhanced security and control.
# Example: Connect to GitHub Enterprise instance via Git git remote add enterprise <https://github.enterprise.example.com/username/repo.git> # Add remote for enterprise repo git push enterprise main # Push code to Enterprise remote repository
Organizations group repositories and teams, managing access and permissions centrally.
# Add a team to an organization on GitHub Enterprise via API example curl -H "Authorization: token <YOUR_TOKEN>" \ -X POST \ -d '{"name":"developers","permission":"push"}' \ https://github.enterprise.example.com/api/v3/orgs/<org-name>/teams # Assign repositories to teams to control access
GitHub Enterprise supports security features like SAML SSO, LDAP integration, and audit logs.
# Enable SAML single sign-on for your organization (via GitHub UI) # This ensures all users authenticate through your enterprise identity provider. # Audit logs track all user and repository activities for compliance.
Enterprise offers detailed audit logs and compliance reports to meet organizational policies.
# Access audit logs on GitHub Enterprise UI # Use API to fetch audit logs for automated monitoring: curl -H "Authorization: token <YOUR_TOKEN>" \ https://github.enterprise.example.com/api/v3/orgs/<org-name>/audit-log # Review logs for unauthorized access or changes
Enterprise supports handling large repos with Git LFS and repository archiving features.
# Use Git Large File Storage (LFS) for big assets git lfs install # Initialize Git LFS in your repo git lfs track "*.psd" # Track Photoshop files with LFS git add .gitattributes # Add LFS tracking rules to repo git commit -m "Track large binary files with Git LFS" git push enterprise main # Push large files managed by LFS to Enterprise repo
GitHub Enterprise supports marketplace apps and custom integrations for CI/CD, code quality, and more.
# Install a GitHub App via Enterprise Marketplace (via UI) # Example: Integrate CI tool (like Jenkins or CircleCI) with Enterprise repos # Use GitHub Actions in Enterprise for automation # Define workflows in .github/workflows/ directory in your repo
Move your existing Git repositories from other hosts or local systems to GitHub easily.
<!-- Clone existing repository locally --> git clone <existing-repo-url> <!-- Add new GitHub remote --> git remote add origin <github-repo-url> <!-- Push all branches and tags to GitHub --> git push -u origin --all git push -u origin --tags
Export your repository data for backups or migration using Git bundle files.
<!-- Create a bundle file of the repository --> git bundle create repo.bundle --all <!-- Share repo.bundle or store as backup -->
Regularly backup GitHub repositories by cloning or using GitHub APIs.
<!-- Clone repo locally as backup --> git clone --mirror <github-repo-url> <!-- Use GitHub API or third-party tools for scheduled backups -->
GitHub provides import tools to migrate projects from services like Bitbucket, GitLab, or SVN.
<!-- Use GitHub's import repository tool at https://github.com/new/import --> <!-- Provide source repo URL and credentials --> <!-- Follow steps to import issues, pull requests, and history -->
Archive repositories on GitHub to make them read-only but still accessible.
<!-- In GitHub repo settings --> <!-- Scroll to "Danger Zone" and select "Archive this repository" --> <!-- Archived repos cannot be pushed to or edited -->
Prepare for data loss by having multiple backups and recovery plans in place.
<!-- Keep local clones and bundle backups offsite --> <!-- Use automated backup scripts or services --> <!-- Test recovery procedures regularly -->
Webhooks are HTTP callbacks triggered by events in GitHub that notify your external services in real-time.
You configure webhooks in your GitHub repository settings to send event data to your server.
Many apps integrate with GitHub via APIs or webhooks to automate workflows, e.g., Slack, Jenkins, CircleCI.
Zapier and IFTTT help automate tasks by connecting GitHub with many other apps without coding.
Build your own server to handle webhook events and automate workflows as needed.
<!-- Sample Node.js Express webhook handler --> const express = require('express'); const app = express(); app.use(express.json()); // Parse JSON payloads app.post('/webhook', (req, res) => { const event = req.headers['x-github-event']; const payload = req.body; console.log('Received event:', event); // Process event (e.g., trigger CI, update DB, notify team) res.status(200).send('Webhook received'); }); app.listen(3000, () => console.log('Server listening on port 3000'));
Common issues include payload delivery failures, incorrect URLs, or secret token mismatches.
The GitHub Marketplace offers apps and actions to enhance workflow automation, CI/CD, security, and more.
# Browse marketplace in the browser: # https://github.com/marketplace # Example: Install an action for automated code review in your repo # Use the GitHub UI or add a workflow file referencing the action
Popular apps include Dependabot for dependency updates, Snyk for security scanning, and CodeQL for code analysis.
# Enable Dependabot for automated dependency updates # Create dependabot.yml in .github directory with rules # Example snippet in dependabot.yml: version: 2 updates: - package-ecosystem: "npm" directory: "/" # Location of package.json schedule: interval: "daily"
Tools like CodeQL and Snyk help identify vulnerabilities and enforce security best practices.
# Example: Using CodeQL in GitHub Actions workflow name: "CodeQL Analysis" on: [push, pull_request] jobs: analyze: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Initialize CodeQL uses: github/codeql-action/init@v2 - name: Perform CodeQL Analysis uses: github/codeql-action/analyze@v2
Apps like ZenHub and Jira integrate with GitHub to manage issues, sprints, and project boards.
# Example: Link Jira issues to GitHub commits # Use smart commits in commit messages like: git commit -m "JIRA-123 Fix login bug" # Jira app updates issue status automatically
Marketplace tools help automate testing, build, and deployment pipelines (e.g., CircleCI, Travis CI, GitHub Actions).
# Example GitHub Actions workflow to run tests and deploy name: CI/CD Pipeline on: [push] jobs: build-and-test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Run Tests run: npm test deploy: needs: build-and-test runs-on: ubuntu-latest steps: - name: Deploy to server run: ./deploy.sh
ChatOps integrates chat platforms (like Slack) with GitHub for real-time collaboration and automation.
# Example: Use GitHub Actions to notify Slack on PR creation name: Notify Slack on PR on: pull_request jobs: notify: runs-on: ubuntu-latest steps: - uses: slackapi/slack-github-action@v1 with: payload: '{"text":"New PR opened: ${{ github.event.pull_request.title }}"}' env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
Git errors often happen due to conflicts, detached HEAD, or missing commits. Understanding error messages helps fix them quickly.
<!-- Example: resolving "detached HEAD" state --> git checkout main <!-- Switch back to main branch --> <!-- Example: fixing merge conflicts manually --> <!-- Edit conflicted files, then --> git add <file> git commit -m "Resolve merge conflict"
Issues connecting to GitHub usually relate to SSH keys, tokens, or network settings.
<!-- Check SSH connection --> ssh -T git@github.com <!-- Refresh GitHub personal access token --> <!-- Update remote URL with token if using HTTPS --> git remote set-url origin https://<token>@github.com/<user>/<repo>.git
Sync issues occur when local and remote branches diverge. Use pull, fetch, and rebase carefully.
<!-- Fetch latest changes --> git fetch origin <!-- Rebase local changes on top of origin/main --> git rebase origin/main <!-- Push rebased changes --> git push origin main --force-with-lease
Use logs and verbose mode to debug GitHub workflows.
<!-- Enable debug logging in workflow --> env: ACTIONS_STEP_DEBUG: true <!-- Check detailed logs in GitHub Actions UI -->
Report issues via GitHub Issues or official support channels, providing detailed logs and steps.
<!-- Open new issue on GitHub repo --> <!-- Include reproduction steps and error messages -->
Monitor GitHub's system status and review logs to diagnose service disruptions.
<!-- Visit https://www.githubstatus.com/ to check status --> <!-- Use git log for local commit history review --> git log --oneline --graph --decorate --all
GitHub has introduced new features like Discussions, GitHub Actions improvements, and Codespaces for cloud-based development.
<!-- Discussions replace forums for Q&A inside repos -->
Git is evolving with features like partial clone improvements, sparse checkout enhancements, and performance optimizations.
git clone --filter=blob:none git@github.com:user/repo.git git sparse-checkout init --cone git sparse-checkout set folder/subfolder
GitHub Copilot uses AI to assist coding by suggesting whole lines or functions inside your IDE.
Cloud-native Git solutions like GitHub Codespaces and Gitpod enable instant development environments in the cloud.
GitHub’s mobile and desktop apps improve workflow by allowing issue tracking, notifications, and repo management on the go.
Follow GitHub’s official blog, release notes, and community forums to keep up with the latest updates and features.
GitHub Actions allows building multi-job workflows with dependencies and conditions to automate sophisticated CI/CD pipelines.
# Example: Workflow with multiple jobs and job dependencies name: Complex CI Pipeline on: [push] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 # Check out repo code - name: Build project run: make build # Run build command test: runs-on: ubuntu-latest needs: build # Wait for build job to complete steps: - uses: actions/checkout@v3 - name: Run tests run: make test # Run tests deploy: runs-on: ubuntu-latest needs: test # Wait for test job to succeed if: success() # Only deploy if previous jobs succeeded steps: - name: Deploy to production run: ./deploy.sh # Run deployment script
Define reusable workflows that can be called by other workflows to avoid duplication and improve maintainability.
# Example: Call reusable workflow from another workflow # In caller workflow: jobs: call-workflow: uses: user/repo/.github/workflows/reusable.yml@main with: param1: value1 param2: value2
Matrix strategy lets you run jobs with multiple configurations, like different OS or versions, in parallel.
# Example: Run tests on multiple Node.js versions jobs: test: runs-on: ubuntu-latest strategy: matrix: node-version: [12, 14, 16] steps: - uses: actions/checkout@v3 - name: Setup Node.js ${{ matrix.node-version }} uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test
Securely store sensitive data in secrets and use environment variables to access them in workflows.
# Example: Use secret API key in a workflow jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Deploy using secret key env: API_KEY: ${{ secrets.API_KEY }} # Use secret stored in repo settings run: ./deploy.sh --key $API_KEY
Workflows can be triggered by various GitHub events, scheduled times, or manually.
# Example: Trigger workflow on push to main or on schedule on: push: branches: - main schedule: - cron: '0 0 * * 0' # Every Sunday at midnight
Create a repository to showcase your work and projects, including a README and project structure.
<!-- Initialize a new git repo --> git init portfolio <!-- Change directory to portfolio folder --> cd portfolio <!-- Create a README file --> echo "# My Portfolio" > README.md <!-- Add all files to staging --> git add . <!-- Commit the initial portfolio setup --> git commit -m "Initial commit: add README"
Work with multiple team members by branching and merging changes collaboratively.
<!-- Create and switch to a new feature branch --> git checkout -b feature-login <!-- Make changes and stage them --> git add login.html <!-- Commit the changes --> git commit -m "Add login page" <!-- Push branch to remote --> git push origin feature-login <!-- Create pull request on GitHub to merge feature-login into main -->
Fork a repo, clone it, make changes, and submit a pull request to contribute.
<!-- Fork the repository on GitHub UI --> <!-- Clone your fork locally --> git clone https://github.com/yourusername/project.git <!-- Add upstream remote to original repo --> git remote add upstream https://github.com/originalowner/project.git <!-- Sync your fork with upstream --> git fetch upstream git checkout main git merge upstream/main <!-- Create a new branch for your changes --> git checkout -b fix-typo <!-- Commit your changes and push --> git add . git commit -m "Fix typo in README" git push origin fix-typo <!-- Open a pull request on GitHub -->
Use GitHub Actions to automate tests and deployment on every push.
<!-- Example workflow file: .github/workflows/ci.yml --> name: CI Pipeline on: [push] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up Node.js uses: actions/setup-node@v3 with: node-version: '16' - name: Install dependencies run: npm install - name: Run tests run: npm test - name: Deploy if: success() run: echo "Deploying app..." <!-- Add your deploy script here --> <!-- See GitHub Actions docs for more details -->
Host static sites directly from your GitHub repo using GitHub Pages.
<!-- Create gh-pages branch --> git checkout -b gh-pages <!-- Add your website files --> git add . <!-- Commit and push to gh-pages branch --> git commit -m "Deploy website to GitHub Pages" git push origin gh-pages <!-- Enable GitHub Pages in repo settings to serve from gh-pages branch -->
Use branching strategies, code reviews, and automation to manage large projects effectively.
<!-- Use protected branches to prevent direct pushes to main --> <!-- Require pull request reviews before merge --> <!-- Automate testing and linting with CI workflows --> <!-- Regularly update dependencies and refactor code -->
Overview of how version control is crucial for AI projects.
<!-- Initialize Git repo for AI project --> git init ai-project <!-- Start version control for your AI code and experiments --> <!-- Add files to staging --> git add . <!-- Add all files including code, configs, notebooks --> <!-- Commit initial project setup --> git commit -m "Initial commit: AI project setup"Output:
Initialized empty Git repository in /path/to/ai-project/.git/ [main (root-commit) abc1234] Initial commit: AI project setup
Best practices for handling AI source code and datasets in Git repos.
<!-- Use .gitignore to exclude large datasets --> echo "data/" >> .gitignore <!-- Ignore data folder from Git tracking --> <!-- Track code files --> git add src/ notebooks/ <!-- Commit changes --> git commit -m "Add AI source code and notebooks"Output:
[data/ added to .gitignore] [main abc5678] Add AI source code and notebooks
Managing large datasets and model files with Git LFS.
<!-- Install Git LFS --> git lfs install <!-- Track large files (e.g., model weights) --> git lfs track "*.h5" git lfs track "*.pt" <!-- Add .gitattributes for LFS tracking --> git add .gitattributes <!-- Commit the LFS tracking setup --> git commit -m "Configure Git LFS for large AI model files"Output:
Git LFS initialized. Tracking *.h5 Tracking *.pt [main abc9012] Configure Git LFS for large AI model files
How to organize branches for AI model training, testing, and production.
<!-- Create branch for new AI experiment --> git checkout -b experiment-new-model <!-- Work on experiment, then commit --> git add model.py train.py git commit -m "Add new model training code" <!-- Merge experiment branch back to main when validated --> git checkout main git merge experiment-new-modelOutput:
Switched to a new branch 'experiment-new-model' [experiment-new-model abc3456] Add new model training code Switched to branch 'main' Merge made by the 'recursive' strategy.
Using GitHub features to coordinate teams working on AI.
<!-- Push branch to remote repo --> git push origin experiment-new-model <!-- On GitHub: Open a Pull Request to merge branch --> <!-- Team members review code and discuss changes -->Output:
Enumerating objects: 10, done. Counting objects: 100% (10/10), done. Delta compression using up to 8 threads Compressing objects: 100% (7/7), done. Writing objects: 100% (10/10), 1.5 KiB | 1.5 MiB/s, done. Total 10 (delta 1), reused 0 (delta 0) To https://github.com/user/ai-project.git * [new branch] experiment-new-model -> experiment-new-model
Versioning models and code together for reproducibility.
<!-- Tagging a model version --> git tag -a v1.0 -m "Release model version 1.0" <!-- Push tags to remote --> git push origin v1.0Output:
[main abc5678] Release model version 1.0 To https://github.com/user/ai-project.git * [new tag] v1.0 -> v1.0
Managing bugs, feature requests, and research tasks.
<!-- Create issue on GitHub UI --> <!-- Example: "Improve data preprocessing pipeline" --> <!-- Use labels, milestones, and assignees to organize tasks -->Output:
Issue #12 created: Improve data preprocessing pipeline Labels: enhancement, data Assignee: @team-member
Setting up CI/CD pipelines for AI model training and deployment.
<!-- Example .github/workflows/ai-ci.yml --> name: AI CI Pipeline on: [push] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Setup Python uses: actions/setup-python@v4 with: python-version: '3.8' - name: Install dependencies run: pip install -r requirements.txt - name: Run tests run: pytest tests/ - name: Deploy model if: success() run: python deploy.py <!-- Add your deployment script here -->Output:
Run AI CI Pipeline - Checkout code - Setup Python 3.8 - Install dependencies - Run tests - Deploy model if tests succeed
Automating unit tests and validation of AI models.
<!-- Use pytest to test AI model functions --> def test_model_accuracy(): assert model.evaluate() > 0.9 <!-- Ensure accuracy is above threshold --> <!-- Add test step in GitHub Actions workflow --> - name: Run AI model tests run: pytest tests/model_tests.pyOutput:
============================= test session starts ============================= collected 3 items test_model_accuracy.py ... [100%] ============================== 3 passed in 0.12s ==============================
Securing AI credentials with GitHub Secrets.
<!-- Store secrets in GitHub repo settings > <!-- Example: AI_API_KEY --> <!-- Use secrets in GitHub Actions --> - name: Use AI API Key run: python run_ai.py env: AI_API_KEY: ${{ secrets.AI_API_KEY }}Output:
Using secret AI_API_KEY securely in CI workflow.
Collaborative review of AI code and experiments.
<!-- Push feature branch --> git push origin feature-experiment <!-- Open Pull Request on GitHub --> <!-- Team reviews code and leaves comments --> <!-- Address feedback by pushing commits --> git add . git commit -m "Fix issues raised in PR review" git push origin feature-experimentOutput:
PR comments addressed, updated branch pushed.
Creating docs and demos for AI projects.
<!-- Create docs folder and add markdown files --> <!-- Push docs to gh-pages branch --> git checkout -b gh-pages git add docs/ git commit -m "Add AI model documentation" git push origin gh-pages <!-- Enable GitHub Pages to serve from gh-pages branch -->Output:
Documentation published at https://username.github.io/ai-project
Facilitating communication and brainstorming.
<!-- Enable Discussions in GitHub repo settings --> <!-- Use Discussions for brainstorming new features and research ideas -->Output:
Community engagement through Discussions.
Publishing models and dependencies via GitHub Packages.
pip install twine twine upload --repository-url https://upload.pypi.org/legacy/ dist/* pip install your-ai-modelOutput:
Package uploaded and available for installation.css Copy Edit
End-to-end walkthrough of versioning, collaborating, testing, and deploying an AI model using Git and GitHub.
git clone https://github.com/user/ai-project.git cd ai-project git checkout -b experiment-transformer git add . git commit -m "Add transformer model training" git push origin experiment-transformer git checkout main git pull origin main git tag -a v2.0 -m "Transformer model release" git push origin v2.0Output:
Repository cloned. Branch created: experiment-transformer Code committed and pushed. Pull Request opened. Release tagged v2.0. CI pipeline deployed the model.
Track metrics, logs, and model outputs alongside code changes.
<!-- Commit new performance logs --> git add logs/performance-2025-05-18.csv git commit -m "Add latest model performance logs" git push origin mainOutput:
[main abc7890] Add latest model performance logs To https://github.com/user/ai-project.git
Maintain rich documentation on AI algorithms, usage, and results.
<!-- Edit Wiki pages directly on GitHub UI --> <!-- Add sections like 'Data Collection', 'Model Architecture', 'Training Details' -->Output:
Wiki updated: Added training details section.
Link tools like MLflow or Weights & Biases with GitHub for reproducibility.
<!-- Save experiment metadata in Git repo --> git add experiments/exp_2025_05_18.json git commit -m "Add MLflow experiment metadata" git push origin mainOutput:
[main abc8912] Add MLflow experiment metadata
How forks enable contributors to safely experiment and contribute.
<!-- Fork repo on GitHub --> <!-- Clone fork locally --> git clone https://github.com/contributor/ai-project.git <!-- Make changes and push to fork --> git push origin feature-update <!-- Open Pull Request from fork to original repo -->Output:
Feature branch pushed to fork. Pull Request opened for review.
Standardizing contributions and bug reports in AI projects.
<!-- Add ISSUE_TEMPLATE.md and PULL_REQUEST_TEMPLATE.md in .github folder --> <!-- Example: Require experiment description and metrics for PRs -->Output:
Templates added. Contributors must follow format.
Including external repos like datasets or shared utilities as submodules.
<!-- Add dataset repo as submodule --> git submodule add https://github.com/data/dataset.git data/ <!-- Initialize and update submodules --> git submodule update --init --recursiveOutput:
Submodule 'data' added.
Resolving merge conflicts when multiple contributors change the same files.
<!-- During merge, conflicts will appear --> git merge feature-branch <!-- Edit conflicted files to resolve issues --> git add resolved-file.py git commit -m "Resolve merge conflicts"Output:
Merge conflict resolved.
Using Data Version Control (DVC) for large data files alongside Git.
<!-- Initialize DVC in repo --> dvc init <!-- Track dataset --> dvc add data/train.csv <!-- Commit DVC files --> git add data/train.csv.dvc .gitignore git commit -m "Track train.csv with DVC"Output:
Data file tracked by DVC.
Trigger model retraining pipelines on data updates.
<!-- Example trigger on data push --> on: push: paths: - data/** jobs: retrain: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Retrain model run: python retrain.pyOutput:
Triggered retraining job on data update.
Use GitHub checks to display test metrics before merging.
<!-- Use GitHub Actions to run tests and comment metrics on PR --> - name: Run model tests run: pytest --metrics <!-- Use action to post results as PR comment -->Output:
PR comment: Model accuracy = 92%, Loss = 0.15
Version control of environment specs like requirements.txt or conda.yaml.
<!-- Add dependencies file --> git add requirements.txt git commit -m "Add dependencies list" git push origin mainOutput:
Dependencies file committed.
Leverage cloud dev environments with pre-configured AI tools.
<!-- Open Codespace on GitHub UI --> <!-- Develop AI models with zero local setup -->Output:
Codespace environment ready.
Use Git tags and branches to archive finalized experiment states.
<!-- Tag experiment version --> git tag -a exp-v1.1 -m "Finalized experiment 1.1" <!-- Push tags --> git push origin exp-v1.1Output:
Experiment archived as tag exp-v1.1.
Track features, bugs, and releases in Kanban boards.
<!-- Create Project Board on GitHub --> <!-- Add cards for AI features and tasks -->Output:
Project board created and updated.
Summary of workflows: small commits, meaningful messages, branches for features, code reviews, and CI/CD.
<!-- Example commit message --> git commit -m "Refactor data loader for efficiency" <!-- Push regularly and request reviews --> git push origin feature-branchOutput:
Commit pushed, ready for review.
Store and version intermediate model checkpoints for easy rollback and analysis.
<!-- Save checkpoint files --> git add checkpoints/model_epoch_10.pth git commit -m "Add checkpoint after epoch 10" git push origin mainOutput:
[main abc1234] Add checkpoint after epoch 10
Store sensitive credentials safely for use in CI/CD workflows.
<!-- Add secret in GitHub repo settings --> <!-- Use secret in GitHub Actions workflow --> env: API_KEY: ${{ secrets.AI_API_KEY }}Output:
Secret API key used securely in workflow.
Link GitHub commits and PRs to experiment dashboards for traceability.
<!-- Include commit hash in experiment metadata --> commit_id = os.getenv('GITHUB_SHA') <!-- Push metadata to dashboard API -->Output:
Experiment linked with commit abc1234.
Run scripts to validate data quality before merging new data changes.
<!-- Example GitHub Action step --> - name: Validate data files run: python validate_data.py data/new_data.csvOutput:
Data validation passed successfully.
Keep track of hyperparameter config files alongside code changes.
<!-- Commit hyperparameter JSON or YAML --> git add configs/hparams.yaml git commit -m "Update hyperparameters for model v2" git push origin mainOutput:
[main abc5678] Update hyperparameters for model v2
Ensure contributors provide necessary experiment details before review.
<!-- Add PULL_REQUEST_TEMPLATE.md --> <!-- Include sections for experiment setup, results, and code changes -->Output:
PR template enforced for consistent experiment info.
Collaborate asynchronously on research questions and project directions.
<!-- Create discussion categories for AI topics --> <!-- Use markdown formatting to organize discussions -->Output:
Active discussions fostering collaboration.
Set up workflows to regularly evaluate models on test data and report results.
<!-- Schedule workflow to run tests nightly --> on: schedule: - cron: "0 0 * * *" jobs: evaluate: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Run evaluation run: python evaluate.pyOutput:
Model evaluation completed; results posted.
Track licensing info and compliance documents within the repo.
<!-- Add LICENSE and compliance.txt files --> git add LICENSE compliance.txt git commit -m "Add dataset license and compliance info" git push origin mainOutput:
License and compliance documents committed.
Consolidate multiple AI projects and microservices into one repo.
<!-- Move projects into subdirectories --> mkdir -p ai-projects/model-training mkdir -p ai-projects/data-pipelines git add ai-projects/ git commit -m "Migrate projects into monorepo structure" git push origin mainOutput:
Monorepo structure committed.