0% found this document useful (0 votes)
1 views

git and GitHub 2024_011928

Git is a distributed version control system that enables developers to track and manage code collaboratively, while GitHub is a web platform that hosts Git repositories and facilitates project management. Key concepts include repositories, branches, commits, and basic commands like git init, git clone, and git push. The document also covers advanced commands and tips for using Git effectively.

Uploaded by

temmybryan74
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

git and GitHub 2024_011928

Git is a distributed version control system that enables developers to track and manage code collaboratively, while GitHub is a web platform that hosts Git repositories and facilitates project management. Key concepts include repositories, branches, commits, and basic commands like git init, git clone, and git push. The document also covers advanced commands and tips for using Git effectively.

Uploaded by

temmybryan74
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Overview of Git and GitHub

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.

Core Git Terminology

- 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.

Basic Git Commands

1.1 `git init` – Initialize a New Repository

`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

```

1.2 `git clone` – Copy a Remote Repository

`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

git clone <repository-url>

```

- Examples:

- Cloning from GitHub: You copy a repository to your machine using HTTPS.

```bash

git clone https://github.com/username/repo-name.git

```

- Cloning with SSH: You use SSH for secure cloning, typically after setting up SSH keys.

```bash

git clone [email protected]:username/repo-name.git


```

1.3 `git add` – Stage Changes

`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 <file>

git add .

```

- Examples:

- Staging a single file: Adds `index.html` to the staging area.

```bash

git add index.html

```

- Staging all modified files: Adds all changes to the staging area.

```bash

git add .

```

1.4 `git commit` – Save a Snapshot

`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

git commit -m "Commit message"

```

- Examples:

- Single commit with a message: Saves changes with a description.

```bash

git commit -m "Added login functionality"

```

- Commit all changes: Adds and commits all changes directly without separate staging.

```bash

git commit -a -m "Updated header and footer"

```

1.5 `git status` – Check Repository Status

`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

```

Intermediate Git Commands

2.1 `git branch` – Manage Branches

Branches enable independent development paths. The `git branch` command lists branches and can
create new ones.

- Syntax:

```bash

git branch

git branch <branch-name>

```

- Examples:

- Listing branches: View all branches in your repository.

```bash

git branch

```

- Creating a new branch: Make a branch for a new feature.

```bash
git branch feature-login

```

2.2 `git checkout` – Switch Branches

`git checkout` lets you switch between branches or create new branches.

- Syntax:

```bash

git checkout <branch-name>

git checkout -b <new-branch-name>

```

- Examples:

- Switching to an existing branch:

```bash

git checkout feature-login

```

- Creating and switching to a new branch:

```bash

git checkout -b bugfix

```

2.3 `git merge` – Combine Branches

`git merge` integrates changes from one branch into another, like combining a feature branch into `main`.

- Syntax:
```bash

git merge <branch-name>

```

- Examples:

- Merging feature branch into `main`:

```bash

git checkout main

git merge feature-login

```

- Handling merge conflicts: If conflicts arise, resolve them, then continue the merge.

```bash

git add .

git commit -m "Resolved merge conflicts"

```

2.4 `git push` – Upload Changes

`git push` sends local changes to a remote repository, updating it with recent commits.

- Syntax:

```bash

git push <remote> <branch>

```

- Examples:

- Pushing to the main branch:


```bash

git push origin main

```

- Pushing a feature branch:

```bash

git push origin feature-login

```

2.5 `git pull` – Download Updates

`git pull` retrieves and merges changes from a remote repository to your local one.

- Syntax:

```bash

git pull <remote> <branch>

```

- Examples:

- Pulling updates from `main`:

```bash

git pull origin main

```

- Pulling from a feature branch:

```bash

git pull origin feature-login

```
Advanced Git Commands

3.1 `git stash` – Temporarily Save Changes

`git stash` stores uncommitted changes, allowing you to work on other things temporarily.

- Syntax:

```bash

git stash

git stash apply

```

- Examples:

- Stash changes: Temporarily saves modifications.

```bash

git stash

```

- Retrieve stashed changes:

```bash

git stash apply

```

3.2 `git rebase` – Reapply Commits

`git rebase` allows you to move commits to a new base, like aligning a feature branch with `main`
without a merge.
- Syntax:

```bash

git rebase <branch-name>

```

- Examples:

- Rebase onto `main`:

```bash

git checkout feature-login

git rebase main

```

- Interactive rebase to edit commits:

```bash

git rebase -i HEAD~3

```

3.3 `git cherry-pick` – Apply Specific Commits

`git cherry-pick` copies specific commits from one branch to another.

- Syntax:

```bash

git cherry-pick <commit-hash>

```

- Examples:

- Cherry-pick one commit:


```bash

git cherry-pick abc1234

```

Multiple commits:

```bash

git cherry-pick abc1234 def5678

```

Additional Tips

Using Aliases

Git aliases simplify frequently used commands.

- Example 1: Alias for `git status`:

```bash

git config --global alias.st status

```

Use `git st` instead of `git status`.

- Example 2: Alias for a graphical log:

```bash
git config --global alias.lg "log --oneline --graph"

```

Use `git lg` to view a visual log.

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.

You might also like