Git and GitHub Tutorial

Jul 16, 2024

Git and GitHub Tutorial

Overview

  • Detailed explanation of Git and GitHub from scratch.
  • Key concepts: commands, usage, and uploading projects to GitHub.
  • Importance of Git for developers in managing both small and large projects.
  • Cheat sheet for Git commands is provided for download.

Git Overview

What is Git?

  • A version control system (VCS) to track changes in code, similar to a bank account statement for code changes.
  • Important for developers as it helps in collaborating on projects and maintaining code history.

Why Use Git?

  • Popular: Widely used in the industry, e.g., Microsoft internships use GitHub for code management.
  • Free and Open Source: Accessible to all without costs.
  • Fast and Scalable: Can handle projects of all sizes efficiently.
  • Primary Purposes:
    • Track History: Maintains a record of all changes and stages of code development.
    • Collaboration: Enables multiple developers to work on the same project without conflicts.

GitHub Overview

What is GitHub?

  • A web-based platform for storing and managing Git repositories.
  • Allows developers to upload their code, collaborate with others, and showcase their projects for job applications.
  • Projects on GitHub are stored in repositories or repos.

GitHub Profile Setup

  • **Steps to create a GitHub account: email required, either personal or college email.
  • Repositories: Create and manage projects, add README files for descriptions.

Basic Git Commands

  • Clone: git clone <repo-link> to duplicate a repo from GitHub to your local machine.
  • Status: git status to check the current state of your repository.
  • Add: git add <file-name> to stage changes.
  • Commit: git commit -m <message> to save changes with a message.
  • Push: git push origin main to upload local changes to the GitHub repo.

Using Visual Studio Code (VSCode)

  • Recommended editor for coding and Git operations.
  • Setup steps detailed for both Windows and Mac users, including Git installation verification.
  • Allows integrated terminal usage for Git commands.

Git Workflow

  1. Start on GitHub: Create a repo on GitHub and clone it locally.
  2. Make Changes: Edit code, add new features or fix bugs, stage changes (git add), commit changes (git commit), and push them to GitHub (git push).
  3. Git Branches: Use branches for different features or tasks, merge branches to incorporate changes.

Git Branches

  • Concept: Branches allow parallel development without affecting the main codebase.
  • **Common Commands:"
    • git branch to list branches.
    • git checkout -b <branch-name> to create and switch to a new branch.
    • git merge <branch> to merge a branch into the current one.
    • git branch -d <branch> to delete a branch.
  • Conflict Resolution: Handle merge conflicts by manually editing conflicted files and completing the merge.

Example Workflow with Branches

  1. Create a feature branch (git checkout -b feature-branch), make changes, and commit them.
  2. Merge the feature branch into the main branch (git merge feature-branch), resolving any conflicts.
  3. Push the merged changes (git push origin main).

Undoing Changes

  • Unstaged Changes: git reset <file> to unstage a file.
  • Undo Commits: git reset HEAD~1 to undo the latest commit.
  • Hard Reset: git reset --hard <commit-hash> to revert to a specific commit, discarding all subsequent changes.

Forking GitHub Repositories

  • Fork: Create a personal copy of someone else's repository to make changes freely.
  • Pull Request: Submit changes from the forked repo back to the original for review and merging.

Conclusion

  • Git and GitHub are essential tools for modern development, enabling version control, collaboration, and project management.
  • Following the detailed tutorials will enhance your understanding and proficiency in using these tools effectively.