git and GitHub 2024_011928
git and GitHub 2024_011928
Git is a distributed version control system (VCS) that allows developers to track, manage, and
collaborate on code over time. Unlike traditional version control, which stores changes on a central
server, Git allows every developer to have a complete copy of the project, making it efficient and
resilient.
GitHub is a web-based platform that hosts Git repositories and offers tools to manage projects,
collaborate, and share code online. GitHub is a great platform for both private projects and open-source
contributions, providing version control and collaboration in one place.
- Repository (repo): A directory or storage space where your project files are stored. A Git repository
keeps a history of all changes made to the files.
- Branch: A parallel version of your project that allows you to work on features independently of the
main codebase. The main branch (also called `main` or `master`) is often used for stable code, while
other branches can be used for development and testing.
- Commit: A snapshot of your project at a given time. Each commit records changes and has a unique ID.
- Working Directory: The area where you’re actively editing files. It reflects the most recent state of the
files, whether or not they’re staged or committed.
- Staging Area: A holding area where changes are listed before committing. Staging allows you to prepare
specific changes for your next commit.
- Remote Repository: A version of your repository hosted online, such as on GitHub. Changes made in
your local repository can be pushed to the remote repository to keep it updated.
Real-World Examples
1. Software Collaboration: A team building a website may use Git to work on different features (e.g.,
header, footer, authentication) in separate branches. Once each feature is complete, they merge it into
the main branch. Git ensures all features integrate smoothly.
2. Open Source Contributions: Developers worldwide contribute to large projects (e.g., Linux, Node.js)
by forking a repository on GitHub, making changes, and submitting pull requests for review. This
collaborative process is made possible through Git and GitHub.
`git init` sets up a new Git repository. It creates a hidden `.git` directory that tracks changes in your
project.
- Syntax:
```bash
git init
```
- Examples:
- Creating a new project: You make a new folder and initialize Git to start tracking changes.
```bash
mkdir my_project
cd my_project
git init
```
- Adding Git to an existing project: If you have an existing project, navigate to its directory and run `git
init`.
```bash
cd my_existing_project
git init
```
`git clone` copies a remote repository to your local machine. It’s often used to start working on a
project that’s hosted online.
- Syntax:
```bash
```
- Examples:
- Cloning from GitHub: You copy a repository to your machine using HTTPS.
```bash
```
- Cloning with SSH: You use SSH for secure cloning, typically after setting up SSH keys.
```bash
`git add` moves changes to the staging area. This prepares specific changes to be included in your next
commit, allowing you to control which changes are recorded.
- Syntax:
```bash
git add .
```
- Examples:
```bash
```
- Staging all modified files: Adds all changes to the staging area.
```bash
git add .
```
`git commit` records the changes staged in the repository. Commits should include messages that
explain the changes, helping track the project’s evolution.
- Syntax:
```bash
```
- Examples:
```bash
```
- Commit all changes: Adds and commits all changes directly without separate staging.
```bash
```
`git status` provides information on the working directory and staging area, showing which files are
modified, staged, or untracked.
- Syntax:
```bash
git status
```
- Examples:
- Checking status after edits: View which files have been modified.
```bash
git status
```
- After staging: Confirm which files are staged for the next commit.
```bash
git status
```
Branches enable independent development paths. The `git branch` command lists branches and can
create new ones.
- Syntax:
```bash
git branch
```
- Examples:
```bash
git branch
```
```bash
git branch feature-login
```
`git checkout` lets you switch between branches or create new branches.
- Syntax:
```bash
```
- Examples:
```bash
```
```bash
```
`git merge` integrates changes from one branch into another, like combining a feature branch into `main`.
- Syntax:
```bash
```
- Examples:
```bash
```
- Handling merge conflicts: If conflicts arise, resolve them, then continue the merge.
```bash
git add .
```
`git push` sends local changes to a remote repository, updating it with recent commits.
- Syntax:
```bash
```
- Examples:
```
```bash
```
`git pull` retrieves and merges changes from a remote repository to your local one.
- Syntax:
```bash
```
- Examples:
```bash
```
```bash
```
Advanced Git Commands
`git stash` stores uncommitted changes, allowing you to work on other things temporarily.
- Syntax:
```bash
git stash
```
- Examples:
```bash
git stash
```
```bash
```
`git rebase` allows you to move commits to a new base, like aligning a feature branch with `main`
without a merge.
- Syntax:
```bash
```
- Examples:
```bash
```
```bash
```
- Syntax:
```bash
```
- Examples:
```
Multiple commits:
```bash
```
Additional Tips
Using Aliases
```bash
```
```bash
git config --global alias.lg "log --oneline --graph"
```
These notes cover the essentials of Git and GitHub for effective version control, collaboration, and
project management. Git and GitHub streamline development, making it easy to work individually or as
part of a larger team.