Git#
Git is a distributed version control system that is widely used for source code management. It is designed to handle everything from small to very large projects with speed and efficiency. Git is essential for collaboration, version tracking, and maintaining project history.
Why Use Git?#
Collaboration: Multiple developers can work on the same project without overwriting each other’s changes.
Version Control: Git keeps a history of all changes, enabling you to revert to previous versions if needed.
Branching and Merging: You can work on new features or bug fixes in isolated branches and merge them back when complete.
Distributed Workflow: Unlike centralized version control systems, every developer has a full copy of the repository.
Installation#
To install Git:
Visit the Git website and download the installer for your operating system.
Run the downloaded installer and follow the installation instructions. During installation, you may be prompted to:
Set up your default text editor (e.g., VS Code).
Adjust your PATH environment variable (recommended).
Verify Installation#
After installation, verify that Git is installed correctly by running the following command in your terminal:
git --version
You should see the installed version of Git.
Configuration#
After installing Git, you need to configure some basic settings.
Set Username and Email#
Run these commands to configure your name and email (these will appear in your commits):
git config --global user.name "Your Name"
git config --global user.email "Your Email"
Check Your Configuration#
To view your current Git configuration, run:
git config --list
This will display all globally set options.
Basic Usage#
Initialize a New Repository#
To create a new Git repository:
Navigate to your project directory:
cd /path/to/your/project
Initialize the repository:
git init
This creates a
.git
folder in your directory, which stores all version control information.
Add Files to Staging#
Add files to the staging area (prepare them for commit):
git add <file_name> # Add a specific file
git add . # Add all changes in the current directory
Commit Changes#
Create a snapshot of your staged changes:
git commit -m "Descriptive commit message"
Tip: Use clear, concise commit messages that describe what you changed.
Connect to a Remote Repository#
To link your local repository to a remote repository (e.g., on GitHub):
git remote add origin <repository_url>
Verify the remote connection:
git remote -v
Push Changes to a Remote Repository#
Push your commits to the remote repository:
git push -u origin main # Push the current branch and set upstream
Pull Changes from a Remote Repository#
Update your local repository with the latest changes from the remote:
git pull origin main
Clone an Existing Repository#
Copy an existing repository to your local machine:
git clone <repository_url>
This creates a new directory containing the repository.
Branching and Merging#
Branches allow you to work on new features or fixes without affecting the main codebase.
Create a New Branch#
git checkout -b feature_branch
This creates and switches to a new branch named feature_branch
.
Switch Between Branches#
git checkout main
Switch to an existing branch.
Merge Branches#
To merge changes from one branch into another:
Switch to the target branch (e.g.,
main
):git checkout main
Merge the feature branch:
git merge feature_branch
Tip: Resolve any merge conflicts manually, then stage and commit the resolved files.
Common Git Commands Cheat Sheet#
Command |
Description |
---|---|
|
Show the status of your working directory |
|
View commit history |
|
Show changes not yet staged |
|
List all branches in the repository |
|
Temporarily save changes without committing |
|
Reapply commits from one branch onto another |
|
Create an annotated tag |
Best Practices#
Commit Often: Make small, logical commits with meaningful messages.
Branching: Use branches for features, fixes, and experiments. Keep
main
ormaster
clean and deployable.Code Reviews: Push changes to remote branches and create pull requests for review.
Document Workflow: Define naming conventions for branches (e.g.,
feature/
,bugfix/
) and commits.Avoid Large Commits: Commit only related changes in a single commit.
Pull Before Push: Always pull the latest changes before pushing to avoid conflicts.
This guide covers essential Git concepts and commands to get you started with version control. With consistent practice and adherence to best practices, you can harness Git to streamline collaboration and maintain a robust project history.