Git is a distributed version control system designed to handle everything from small to very large projects with speed and efficiency. It allows multiple developers to work on a project simultaneously without interfering with each other’s work. This guide will walk you through the basics of Git, from installation to advanced features.
Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. There are two main types of version control systems:
Git is a DVCS, which means every developer has a complete history of the project on their local machine.
You can install Git using Homebrew:
brew install git
Alternatively, you can install it as part of Xcode Command Line Tools:
xcode-select --install
For Debian/Ubuntu-based distributions:
sudo apt-get update
sudo apt-get install git
For Fedora:
sudo dnf install git
To verify the installation, run:
git --version
After installing Git, the first thing you should do is set up your username and email address. Git uses this information to label the commits you make.
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
A Git repository is a directory that contains your project files and the history of all changes made to those files.
To create a new repository:
mkdir my_project
cd my_project
git init
This command creates a .git
directory inside my_project
that tracks changes.
git init
This command initializes a new Git repository.
git init
git clone
To clone an existing repository:
git clone https://github.com/user/repo.git
This command copies the repository from GitHub (or any other remote) to your local machine.
git status
To check the status of your files in the working directory and staging area:
git status
git add
To add a file to the staging area (preparing it for commit):
git add file_name
To add all changes:
git add .
git commit
To commit the changes in the staging area:
git commit -m "Commit message"
git push
To push your changes to a remote repository:
git push origin branch_name
git pull
To fetch and merge changes from the remote repository:
git pull origin branch_name
To create a new branch:
git branch branch_name
To switch to a different branch:
git checkout branch_name
To merge a branch into your current branch:
git merge branch_name
When working with others, it’s common to create branches for new features or bug fixes. Here’s how you can collaborate:
git checkout -b feature_branch
git push origin feature_branch
Rebasing is the process of moving or combining a sequence of commits to a new base commit. This can be useful for keeping a clean project history.
git rebase branch_name
To undo the last commit (but keep the changes):
git reset --soft HEAD~1
To undo the last commit and discard the changes:
git reset --hard HEAD~1
To discard changes in a specific file:
git checkout -- file_name
This tutorial has covered the basics of Git, from installation to more advanced topics like branching, merging, and rebasing. By mastering these commands and concepts, you’ll be well on your way to managing your projects efficiently with Git.
For more detailed information, consult the official Git documentation.