-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
marc
committed
Aug 23, 2024
1 parent
f740a29
commit 81b8224
Showing
4 changed files
with
251 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,248 @@ | ||
# Git Tutorial: A Step-by-Step Guide | ||
|
||
## Introduction | ||
|
||
Git is a distributed version control system designed to handle everything from small to very large projects with speed and efficiency. It allows multiple developers to work on a project simultaneously without interfering with each other’s work. This guide will walk you through the basics of Git, from installation to advanced features. | ||
|
||
## Table of Contents | ||
|
||
1. [Understanding Version Control](#understanding-version-control) | ||
2. [Installing Git](#installing-git) | ||
3. [Getting Started with Git](#getting-started-with-git) | ||
4. [Basic Git Commands](#basic-git-commands) | ||
- [git init](#git-init) | ||
- [git clone](#git-clone) | ||
- [git status](#git-status) | ||
- [git add](#git-add) | ||
- [git commit](#git-commit) | ||
- [git push](#git-push) | ||
- [git pull](#git-pull) | ||
5. [Branching and Merging](#branching-and-merging) | ||
- [Creating a Branch](#creating-a-branch) | ||
- [Switching Branches](#switching-branches) | ||
- [Merging Branches](#merging-branches) | ||
6. [Collaborating with Others](#collaborating-with-others) | ||
7. [Rebasing](#rebasing) | ||
8. [Undoing Changes](#undoing-changes) | ||
9. [Conclusion](#conclusion) | ||
|
||
--- | ||
|
||
## Understanding Version Control | ||
|
||
Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. There are two main types of version control systems: | ||
|
||
- **Centralized Version Control Systems (CVCS)**: A single server stores all the versions of the project files, and clients check out files from this central place. | ||
- **Distributed Version Control Systems (DVCS)**: Each client mirrors the entire repository. This means that if any server dies, any of the client repositories can be copied back up to the server to restore it. | ||
|
||
Git is a DVCS, which means every developer has a complete history of the project on their local machine. | ||
|
||
## Installing Git | ||
|
||
### On Windows | ||
|
||
1. Download Git from [Git for Windows](https://gitforwindows.org/). | ||
2. Run the installer and follow the instructions. Choose the default options unless you have a specific reason to change them. | ||
|
||
### On macOS | ||
|
||
You can install Git using Homebrew: | ||
|
||
```bash | ||
brew install git | ||
``` | ||
|
||
Alternatively, you can install it as part of Xcode Command Line Tools: | ||
|
||
```bash | ||
xcode-select --install | ||
``` | ||
|
||
### On Linux | ||
|
||
For Debian/Ubuntu-based distributions: | ||
|
||
```bash | ||
sudo apt-get update | ||
sudo apt-get install git | ||
``` | ||
|
||
For Fedora: | ||
|
||
```bash | ||
sudo dnf install git | ||
``` | ||
|
||
To verify the installation, run: | ||
|
||
```bash | ||
git --version | ||
``` | ||
|
||
## Getting Started with Git | ||
|
||
### Setting Up Your Identity | ||
|
||
After installing Git, the first thing you should do is set up your username and email address. Git uses this information to label the commits you make. | ||
|
||
```bash | ||
git config --global user.name "Your Name" | ||
git config --global user.email "your.email@example.com" | ||
``` | ||
|
||
### Initializing a Repository | ||
|
||
A Git repository is a directory that contains your project files and the history of all changes made to those files. | ||
|
||
To create a new repository: | ||
|
||
```bash | ||
mkdir my_project | ||
cd my_project | ||
git init | ||
``` | ||
|
||
This command creates a `.git` directory inside `my_project` that tracks changes. | ||
|
||
## Basic Git Commands | ||
|
||
### `git init` | ||
|
||
This command initializes a new Git repository. | ||
|
||
```bash | ||
git init | ||
``` | ||
|
||
### `git clone` | ||
|
||
To clone an existing repository: | ||
|
||
```bash | ||
git clone https://github.com/user/repo.git | ||
``` | ||
|
||
This command copies the repository from GitHub (or any other remote) to your local machine. | ||
|
||
### `git status` | ||
|
||
To check the status of your files in the working directory and staging area: | ||
|
||
```bash | ||
git status | ||
``` | ||
|
||
### `git add` | ||
|
||
To add a file to the staging area (preparing it for commit): | ||
|
||
```bash | ||
git add file_name | ||
``` | ||
|
||
To add all changes: | ||
|
||
```bash | ||
git add . | ||
``` | ||
|
||
### `git commit` | ||
|
||
To commit the changes in the staging area: | ||
|
||
```bash | ||
git commit -m "Commit message" | ||
``` | ||
|
||
### `git push` | ||
|
||
To push your changes to a remote repository: | ||
|
||
```bash | ||
git push origin branch_name | ||
``` | ||
|
||
### `git pull` | ||
|
||
To fetch and merge changes from the remote repository: | ||
|
||
```bash | ||
git pull origin branch_name | ||
``` | ||
|
||
## Branching and Merging | ||
|
||
### Creating a Branch | ||
|
||
To create a new branch: | ||
|
||
```bash | ||
git branch branch_name | ||
``` | ||
|
||
### Switching Branches | ||
|
||
To switch to a different branch: | ||
|
||
```bash | ||
git checkout branch_name | ||
``` | ||
|
||
### Merging Branches | ||
|
||
To merge a branch into your current branch: | ||
|
||
```bash | ||
git merge branch_name | ||
``` | ||
|
||
## Collaborating with Others | ||
|
||
When working with others, it's common to create branches for new features or bug fixes. Here's how you can collaborate: | ||
|
||
1. **Create a new branch**: `git checkout -b feature_branch` | ||
2. **Work on your changes** and commit them. | ||
3. **Push the branch**: `git push origin feature_branch` | ||
4. **Create a Pull Request** on GitHub for code review and merging. | ||
|
||
## Rebasing | ||
|
||
Rebasing is the process of moving or combining a sequence of commits to a new base commit. This can be useful for keeping a clean project history. | ||
|
||
```bash | ||
git rebase branch_name | ||
``` | ||
|
||
## Undoing Changes | ||
|
||
### Undo a Commit | ||
|
||
To undo the last commit (but keep the changes): | ||
|
||
```bash | ||
git reset --soft HEAD~1 | ||
``` | ||
|
||
To undo the last commit and discard the changes: | ||
|
||
```bash | ||
git reset --hard HEAD~1 | ||
``` | ||
|
||
### Discard Changes in a File | ||
|
||
To discard changes in a specific file: | ||
|
||
```bash | ||
git checkout -- file_name | ||
``` | ||
|
||
## Conclusion | ||
|
||
This tutorial has covered the basics of Git, from installation to more advanced topics like branching, merging, and rebasing. By mastering these commands and concepts, you'll be well on your way to managing your projects efficiently with Git. | ||
|
||
For more detailed information, consult the [official Git documentation](https://git-scm.com/doc). | ||
|
||
--- | ||
|
||
|
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters