Skip to content

Latest commit

 

History

History
369 lines (279 loc) · 8.28 KB

Guide.md

File metadata and controls

369 lines (279 loc) · 8.28 KB

😽 Git Tutorial


📄 Table of Contents:




🔶 Download Git Linux


To install Git on Linux, you can use your distribution's package manager. Here are the commands for some popular distributions:

  • Ubuntu/Debian:

    sudo apt update
    sudo apt install git

⚠️ It is important to run the command sudo apt update before installing anything on Linux for several fundamental reasons: it updates the list of available packages; ensures you are installing the latest version; synchronizes with the repositories; improves security; and avoids dependency issues.

  • Check the installed version:

    git --version


🔶 Download Git Windows


To install Git on Windows:

  1. Download the installer: Visit git-scm.com and download the latest version of Git for Windows.

  2. Run the installer: Follow the instructions in the installer, accepting the default settings or customizing them as needed. Pay attention to the options, especially the PATH settings and how you want to use Git Bash.

  3. Verify the installation: After installation, open the terminal (Git Bash or CMD) and run:

    git --version


🔶 Configure Your User


To set your name and email, which will be used in your commits, use the following commands:

🔹 For global configuration (valid for all user repositories on the machine):

git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"

🔹 For local configuration (valid only for a specific repository):

git config user.name "Your Name"
git config user.email "your-email@example.com"


🔶 SSH Key


An SSH key is a secure form of authentication that allows you to connect to remote repositories without having to enter your credentials every time.

  1. Generate a new SSH key:

    ssh-keygen -t rsa -b 4096 -C "your-email@example.com"
    Press Enter to accept the default file location, or specify a different location. You will then be prompted to enter an optional passphrase.

  1. Add the SSH key to the SSH agent:

    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_rsa

  1. Add the SSH key to your GitHub: Copy the contents of the public key:

    cat ~/.ssh/id_rsa.pub

  1. And add it on GitHub at:

    Settings > SSH and GPG keys > New SSH key


  • Test the connection to GitHub using:

     ssh -T git@github.com
    You should see a success message.


🔶 Commands - Repositories


🔹 Create a new repository:

git init

Initializes a new Git repository in the current directory. This creates a .git directory that stores all the files and metadata of the repository.


🔹 Clone a repository:

git clone <repository-url>

Clones the remote repository to the local directory. Replace <repository-url> with the URL of the repository you wish to clone.

Example: git clone https://github.com/user/repository.git


🔹 Add a new remote repository:

git remote add origin <repository-url>

Adds a remote repository with the name origin (or another name you choose) and the provided URL. This is useful for associating a local repository with a remote repository.

Example: git remote add origin https://github.com/user/repository.git


🔹 List remote repositories:

git remote -v

Shows the list of remote repositories associated with the local repository, including their URLs.


🔹 Remove a remote repository:

git remote remove <repository-name>

Removes a remote repository associated with the local repository. Replace <repository-name> with the name of the remote repository you wish to remove.

Example: git remote remove origin


🔹 Change the URL of a remote repository:

git remote set-url origin <new-repository-url>

Updates the URL associated with the remote repository origin. Use this command if the URL of the remote repository changes.


🔹 Rename a remote repository:

git remote rename <old-name> <new-name>

Changes the name of a remote repository.

Example: git remote rename origin upstream


🔹 Check the status of files:

git status

Displays the status of files in the repository, showing which files have been modified, which are staged for commit, and which are untracked.


🔹 Add files to the staging area (to be committed):

git add <file>

Adds a specific file to the staging area, preparing it for commit.

Example: git add index.html

OR

git add .

Adds all modified files.


🔹 Commit the changes:

git commit -m "Commit message"

Records the changes in the repository with a descriptive message. The message should explain what was changed.

Example: git commit -m "[FEAT][WIP] Introduces a new payment method"


🔹 Push:

git push

Sends changes from the local repository to the associated remote repository. By default, the command pushes to the current branch.


🔹 Pull changes from the remote repository:

git pull

Downloads and integrates changes from the remote repository into the local repository. It combines git fetch and git merge into a single command.


🔹 Fetch updates from the remote repository:

git fetch

Downloads changes from the remote repository without integrating them into the local repository. It is useful for checking updates before performing a merge or rebase.


🔹 Merge changes from the remote repository:

git merge <branch>

Merges changes from the specified branch into the current branch. Replace <branch> with the name of the branch you wish to merge.

Example: git merge feature/new-feature



🔶 Branches


Branches allow you to work on different versions of a project simultaneously.

🔹 Create a new branch:

git branch <branch-name>

Creates a new branch.

Example: git branch new-feature


🔹 Switch to an existing branch:

git checkout <branch-name>

Example: git checkout new-feature


🔹 Create and switch to a new branch:

git checkout -b <branch-name>

Example: git checkout -b new-feature


🔹 List all branches:

git branch

🔹 Merge one branch into another:

git merge <branch-name>

Merges the specified branch into the current branch.


🔹 Rebase the changes from a branch:

git rebase <branch>

Reapplies your changes on top of the specified branch. Rebasing is useful for maintaining a linear history when integrating changes.

Example: git rebase main


🔹 Delete a branch:

git branch -d <branch-name>

Removes a local branch. Use -D to force deletion.

Example: git branch -d new-feature



🔶 Cheat Sheets


Here are some links to useful Git cheat sheets: