I initially wrote this for folks engaged with the #100daysofMiva coding challenge, but due to the feedback I've been getting from it. I'll be reviewing it from time to time to help a wider audience.
Introduction
Git and GitHub are essential tools for modern software development. Git is a distributed version control system that helps you track changes in your code, while GitHub is a platform that hosts your Git repositories and facilitates collaboration. This guide will walk you through the basics of using Git and GitHub, from installation to advanced features.
What is Git?
Git is a version control system created by Linus Torvalds in 2005. It allows multiple developers to work on a project simultaneously without overwriting each other’s changes. Git tracks changes in your code and helps you manage different versions of your project.
GitHub is a web-based platform that uses Git for version control. It provides a collaborative environment where developers can host and review code, manage projects, and build software together. GitHub also offers features like issue tracking, pull requests, and project management tools.
Installing Git
To start using Git, you need to install it on your computer. Follow these steps:
Step-by-Step Process to Install Git on Windows
The download should start automatically for the latest version of Git for Windows.
Run the Installer:
Locate the downloaded .exe file in your downloads folder and double-click to run it.
If prompted by the User Account Control (UAC), click "Yes" to allow the installer to make changes to your device.
Setup Wizard:
The Git Setup wizard will open. Click "Next" to proceed.
Select Installation Location:
Choose the directory where you want to install Git or leave the default location. Click "Next."
Select Components:
Choose the components to install. The default selection should be fine for most users. Click "Next."
Choose Start Menu Folder:
Select the Start Menu folder where you want the Git shortcuts to be created. Click "Next."
Adjusting Your PATH Environment:
Choose how Git should be used from the command line: Use Git from Git Bash only (recommended if you don't need Git in the Windows Command Prompt).
Use Git from the command line and also from 3rd-party software (recommended for more flexibility).
Click "Next."
Choose HTTPS Transport Backend:
Select the default option, "Use the OpenSSL library." Click "Next."
Configuring the Line Ending Conversions:
Choose "Checkout Windows-style, commit Unix-style line endings." Click "Next."
Choose the Terminal Emulator:
Select "Use MinTTY (the default terminal of MSYS2)." Click "Next."
Choose Default Git Pull Behavior:
Select "Default (fast-forward or merge)." Click "Next."
Additional Configurations:
Choose if you want to enable extra options like system caching and experimental features. Click "Next" or "Install" to begin the installation.
Complete the Installation:
Once the installation is complete, click "Finish."
You can choose to launch Git Bash or view the release notes.
Verify Installation:
Open "Git Bash" from the Start Menu (scroll down for more on Git Bash).
Type git --version
and press Enter to verify that Git is installed correctly.
Step-by-Step Process to Install Git on macOS
Check if Git is Already Installed:
Open Terminal (Command + Space and type "Terminal").
Type git --version
and press Enter.If Git is already installed, the version number will be displayed. If not, you'll be prompted to install it. Install Git via Xcode Command Line Tools (Recommended):
If you were prompted to install Git after checking the version, click "Install" in the pop-up window.The Xcode Command Line Tools, which include Git, will be installed.
Install Git via Homebrew (Alternative Method):
If you prefer to use Homebrew (a package manager for macOS), open Terminal. First, install Homebrew (if you haven't already) by running:
shell
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Then, install Git by running:
shell
brew install git
Verify the Installation:
Once the installation is complete, verify it by typing git --version
in Terminal.
Step-by-Step Process to Install Git on Linux
Open Terminal:
Access the Terminal from your applications menu or by pressing Ctrl + Alt + T.
Update Package Index:
Before installing Git, update your package index to ensure you're getting the latest version:
shell
sudo apt update
Install Git:
Use the package manager specific to your Linux distribution to install Git.
For Debian/Ubuntu-based distributions:
shell
sudo apt install git
For Fedora:
shell
sudo dnf install git
For CentOS/RHEL:
shell
sudo yum install git
Verify Installation:
After installation, check the installed version of Git to ensure it was installed correctly:
shell
git --version
`
Setting Up Git:
After installing Git, you need to configure it with your name and email address. These details will be associated with your commits.
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Basic Git Commands
Here are some fundamental Git commands to get you started:
Initialize a repository: git init
Clone a repository: git clone <repository-url>
Check the status: git status
Add changes to the staging area: git add <file>
Commit changes: git commit -m "Commit message"
View commit history: git log
Working with GitHub
To use GitHub, you need to create an account on GitHub. Once you have an account, you can create a new repository:
Click the New button on your GitHub dashboard.
Enter a repository name and description.
Choose between public or private visibility.
Click Create repository.
To connect your local repository to GitHub, use the following commands:
`
git remote add origin
git push -u origin master
`
Just in case you're working with a newer version of git 2.0, you might have to set it's push.default
, see example below:
Branching and Merging
Branches allow you to work on different features or fixes independently. To create and switch to a new branch:
git checkout -b <branch-name>
To merge changes from one branch to another:
`
git checkout master
git merge
`
Collaborating with Others
GitHub makes collaboration easy with features like pull requests and forks. To contribute to an open-source project:
Fork the repository to your GitHub account.
Clone the forked repository to your local machine.
Create a new branch, make your changes, and commit them.
Push the changes to your forked repository.
Open a pull request on the original repository.
Advanced Git Features
Git offers many advanced features to enhance your workflow:
Stashing: Temporarily save changes without committing them using git stash
.
Rebasing: Reapply commits on top of another base branch using git rebase
.
Cherry-picking: Apply specific commits from one branch to another using git cherry-pick <commit-hash>
.
Command Lines for Git using Git Bash
Installing Git Bash
To install Git Bash on Windows, follow these steps:
Download Git Bash: Go to the Git for Windows website and download the installer.
Run the Installer: Double-click the downloaded file to start the installation process.
Select Components: During installation, you can choose the components you want to install. It’s recommended to select all default options.
Choose the Default Editor: Select your preferred text editor (e.g., Notepad++).
Adjust Your PATH Environment: Choose the option to use Git from the command line and third-party software.
Complete Installation: Follow the remaining prompts to complete the installation.
Once installed, you can open Git Bash from the Start menu or by right-clicking on a folder and selecting “Git Bash Here.”
Basic Git Commands
Here are some fundamental Git commands you can use in Git Bash:
Initialize a Repository:
git init
This command creates a new Git repository in your current directory.
Clone a Repository:
git clone <repository-url>
This command copies an existing repository to your local machine.
Check the Status:
git status
This command shows the status of your working directory and staging area.
Add Changes to Staging Area:
git add <file>
This command adds changes in the specified file to the staging area. Use git add .
to add all changes.
Commit Changes:
git commit -m "Commit message"
This command records changes to the repository with a descriptive message.
View Commit History:
git log
This command displays the commit history for the repository.
Create a New Branch:
git checkout -b <branch-name>
This command creates and switches to a new branch.
Merge Branches:
git checkout master
git merge <branch-name>
This command merges the specified branch into the master branch.
Push Changes to Remote Repository:
git push origin <branch-name>
This command uploads your local branch commits to the remote repository.
Pull Changes from Remote Repository:
git pull
This command fetches and merges changes from the remote repository to your local branch.
Using Git Bash
Git Bash provides a Unix-like terminal environment on Windows, making it easier to use Git commands. Here are some common Bash commands you might find useful:
Navigate Directories:
cd <directory>
Change the current directory.
List Directory Contents:
ls
List files and directories in the current directory.
Print Working Directory:
pwd
Display the current directory path.
Move or Rename Files:
mv <source> <destination>
Move or rename files and directories.
Copy Files:
cp <source> <destination>
Copy files and directories.
Remove Files or Directories:
rm <file>
Remove files or directories (use rm -r
for directories).
Git Bash also supports many other Unix commands, making it a versatile tool for developers.
Setting Up Visual Studio Code for Git
Visual Studio Code (VS Code) is a popular code editor that integrates seamlessly with Git, making it a powerful tool for version control. This section will guide you through setting up VS Code for Git and using the integrated terminal for Git commands.
Installing Visual Studio Code
Download VS Code: Go to the Visual Studio Code website and download the installer for your operating system.
Run the Installer: Follow the installation instructions to complete the setup.
Setting Up Git in VS Code
Install Git: Ensure Git is installed on your computer. If not, download it from the official Git website.
Open VS Code: Launch Visual Studio Code.
Enable Git: VS Code usually detects Git automatically. If not, you can enable it manually:
Go to File > Preferences > Settings.
Search for “Git: Enabled” and ensure it is checked.
Using the Terminal in VS Code for Git
VS Code comes with an integrated terminal that you can use to run Git commands. Here’s how to set it up and use it:
Open the Terminal:
- Use the shortcut Ctrl+ (backtick) or go to View > Terminal.
- Set Git Bash as the Default Terminal (optional but recommended for Windows users):
- Open the command palette with Ctrl+Shift+P.
- Type and select Preferences: Open Settings (JSON).
Add the following configuration to set Git Bash as the default terminal:
`
JSON
"terminal.integrated.shell.windows": "C:\Program Files\Git\bin\bash.exe"
`
Top comments (0)