Git Snippets
About Git
Git helps you keep track of changes in your files and work with others on the same project.
Install Git (Windows)
Download Git from the official website (https://git-scm.com/) and follow the installation instructions. After installation, open Command Prompt and run the following command to verify the installation.
git --version
Install Git (Mac)
Install Git using Homebrew, a package manager for macOS. Open Terminal and run the following commands to install Git and verify the installation.
brew install git
git --version
Install Git (Linux)
Install Git using your Linux distribution's package manager. Use the appropriate command for your system (e.g., apt for Ubuntu/Debian, yum for RedHat/CentOS). After installation, run the following command to verify the installation.
sudo apt install git # Ubuntu/Debian
sudo yum install git # RedHat/CentOS
git --version
Set Up Git Username and Email
Configure Git with your name and email address, which will be used to label your commits. Replace 'Your Name' and 'your-email@example.com' with your actual name and email address.
git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"
Check Git Settings
Display your current Git configuration settings, including your username and email address, to ensure they are set correctly.
git config --global --list
Create SSH Key
Generate a new SSH key pair for secure authentication with GitHub. Replace 'your-email@example.com' with your email address. Follow the prompts to save the key and optionally set a passphrase.
ssh-keygen -t rsa -b 4096 -C "your-email@example.com"
Add SSH Key to GitHub
Display the contents of your public SSH key file. Copy the output and add it to your GitHub account under Settings > SSH and GPG keys.
cat ~/.ssh/id_rsa.pub
Test SSH Connection
Test your SSH connection to GitHub to ensure your SSH key is set up correctly. You should see a success message indicating that you have successfully authenticated.
ssh -T git@github.com
Start a New Git Project
Initialize a new Git repository in your current directory. This will create a .git directory and start tracking changes in your project.
git init
Check Project Status
Display the status of your working directory and staging area, showing which files have been modified, added, or deleted.
git status
Save a File for Commit
Stage a specific file for commit, adding it to the list of changes to be saved. Replace 'filename.txt' with the name of the file you want to stage.
git add filename.txt
Save All Changes
Stage all modified and new files in your working directory for commit, adding them to the list of changes to be saved.
git add .
Commit Your Changes
Commit the staged changes to your repository with a descriptive message explaining what you did. Replace 'Initial commit' with your own message.
git commit -m "Initial commit"
See Commit History
Display a concise list of all commits in your repository, showing the commit hash and message for each commit.
git log --oneline
Link to GitHub
Add a remote repository on GitHub to your local Git repository. Replace 'https://github.com/your-username/your-repo.git' with the URL of your GitHub repository.
git remote add origin https://github.com/your-username/your-repo.git
Upload Changes to GitHub
Push your committed changes to the remote repository on GitHub. The '-u' flag sets 'origin main' as the default upstream branch for future pushes.
git branch -M main
git push -u origin main
Download a Project
Clone a GitHub repository to your local machine, creating a copy of the repository in a new directory. Replace 'https://github.com/your-username/your-repo.git' with the URL of the repository you want to clone.
git clone https://github.com/your-username/your-repo.git
Get Latest Changes
Fetch and merge the latest changes from the remote repository on GitHub into your local repository. This updates your local project with any new commits from the 'main' branch.
git pull origin main