This readme provides a crash course on Git, a popular version control system that helps you track and manage changes to your codebase efficiently. Whether you're new to Git or need a quick refresher, this guide will cover the essential concepts and commands to get you up and running.
Git is a distributed version control system that enables you to track changes to your project over time. It allows multiple developers to collaborate on the same codebase seamlessly. With Git, you can easily revert to previous versions of your code, work on different features simultaneously, and merge changes from different sources efficiently.
Before you start using Git, you need to set it up on your machine. Here are the basic steps to get started:
-
Install Git: Download and install Git from the official website (https://git-scm.com). Follow the installation instructions specific to your operating system.
-
Configure Git: Open a terminal or command prompt and set your username and email using the following commands:
git config --global user.name "Your Name" git config --global user.email "your@email.com"
-
(Optional) Set up SSH: If you want to establish a secure connection between your machine and remote repositories, you can set up SSH keys. This eliminates the need for entering passwords repeatedly. Follow the instructions in the Git documentation for generating and adding SSH keys to your GitHub or GitLab account.
Here are some fundamental Git commands you'll frequently use:
git init
: Initialize a new Git repository in your project directory.git add <file>
: Stage changes for commit. Usegit add .
to add all files.git commit -m "Commit message"
: Create a new commit with the staged changes and a descriptive message.git status
: View the current status of your repository and any pending changes.git log
: Display a log of commits, including commit messages and metadata.git diff
: Show the differences between the current changes and the last commit.git restore <file>
: Discard changes in a file and restore it to the last committed state.git branch
: List all branches in the repository.git checkout <branch>
: Switch to a different branch.git merge <branch>
: Merge changes from a different branch into the current branch.
Git's branching and merging capabilities allow for parallel development and seamless collaboration. Here are some essential commands for branching and merging:
git branch <branch>
: Create a new branch.git branch -d <branch>
: Delete a branch.git merge <branch>
: Merge changes from a specific branch into the current branch.git rebase <branch>
: Incorporate changes from a specific branch onto the current branch.
Git facilitates working with remote repositories, such as those hosted on GitHub or GitLab. Here are common commands for remote repository management:
git clone <repository URL>
: Clone a remote repository to your local machine.git remote add <name> <repository URL>
: Add a remote repository to your local Git configuration.git push <remote> <branch>
: Push