- Download Git on Linux
- Download Git on Windows
- Configure Your User
- SSH Key
- Commands - Repositories
- Branches
- Commit Messages
- Cheat Sheets
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
-
Check the installed version:
git --version
To install Git on Windows:
-
Download the installer: Visit git-scm.com and download the latest version of Git for Windows.
-
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.
-
Verify the installation: After installation, open the terminal (Git Bash or CMD) and run:
git --version
To set your name and email, which will be used in your commits, use the following commands:
git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"
git config user.name "Your Name"
git config user.email "your-email@example.com"
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.
- Generate a new SSH key:
Pressssh-keygen -t rsa -b 4096 -C "your-email@example.com"
Enter
to accept the default file location, or specify a different location. You will then be prompted to enter an optional passphrase.
- Add the SSH key to the SSH agent:
eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_rsa
- Add the SSH key to your GitHub:
Copy the contents of the public key:
cat ~/.ssh/id_rsa.pub
-
And add it on GitHub at:
Settings > SSH and GPG keys > New SSH key
- Test the connection to GitHub using:
You should see a success message.ssh -T git@github.com
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.
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
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
git remote -v
Shows the list of remote repositories associated with the local repository, including their URLs.
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
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.
git remote rename <old-name> <new-name>
Changes the name of a remote repository.
Example: git remote rename origin upstream
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.
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.
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"
git push
Sends changes from the local repository to the associated remote repository. By default, the command pushes to the current branch.
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.
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.
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 allow you to work on different versions of a project simultaneously.
git branch <branch-name>
Creates a new branch.
Example: git branch new-feature
git checkout <branch-name>
Example: git checkout new-feature
git checkout -b <branch-name>
Example: git checkout -b new-feature
git branch
git merge <branch-name>
Merges the specified branch into the current 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
git branch -d <branch-name>
Removes a local branch. Use -D
to force deletion.
Example:
git branch -d new-feature
Here are some links to useful Git cheat sheets: