Git is a version control system that helps track changes in files, mainly used for coding projects. It allows multiple people to work on the same project without overwriting each other’s work. With Git, you can save different versions of your project, go back to previous versions if needed, and collaborate easily. It works by storing changes in a repository, which can be kept on your computer or shared online using platforms like GitHub. It was created by Linus Torvalds, the creator of the Linux Kernel.
Windows
There are a few options:
winget
, i.e. open your terminal and execute the following commandwinget install --id Git.Git -e --source winget
MacOS
Open a terminal and try to run the git
command. MacOS will prompt you to install the XCode command line tool which will install git
and other developer tools as well.
Linux
Install it with your package manager, on Arch Linux
sudo pacman -S git
In this section, you will learn the typical Git workflow with the commands git init
, git add
, and git commit
for a local repository. Then we will briefly mention how to git push
to a remote repository that could be hosted on an online platform like GitHub or GitLab.
First, to work with Git, you need a Git repository, which is a directory for your project that will be managed with Git. To create one, move to your project’s directory and run the command:
git init
When you run git init
, it creates a new subdirectory named .git
in your current working directory. This directory contains all the necessary Git metadata for the new repository, including subdirectories for objects, references, and template files. It is a special directory that Git and other programs use to recognize the directory as a Git repository.
Alternatively, if the repository already exists somewhere, you can instead clone it by providing the URL
git clone <repository-url>
Note:
If the repository has submodules, you can add the flaggit clone <repository-url> --recurse-submodules
After creating a repository you can keep working as usual. Add, modify and remove files from your code as you wish, until you want to commit your changes, that is, create a snapshot of the current state of the project to the project’s history so that you can check it out later.
To create a commit, you need to first track and stage your changes. That is, Git needs to know which files it needs to look for changes before making a commit. You can know which files are being tracked and which are not, as well as other information, by running the command:
git status
From here, you can decide which files to track and stage for your upcoming commit. As a simple example, you can add all of the modified files with the following command:
git add .
Warning:
Adding all of the files is commonly unwanted and not recommended, as it can unintentionally include temporary files, logs, or other files that should not be committed. Most of the time, it is preferable to manually add specific files and bundle them under a single commit:git add <filename>
You can repeat this command as many times as needed to bundle all of your desired files under a single commit. You can also make separate commits for individual files when necessary. Alternatively, if you want to interactively choose files, you can use:
git add -p
This allows you to review changes before staging them, which can help maintain a cleaner commit history.
After staging the files, you need to save the changes to the repository with a commit:
git commit -m "Your commit message"
The commit message should describe the changes you made, making it easier to track the project’s history.
If you want to back up your project or collaborate with others, you can push your changes to a remote repository, i.e. a repository that is hosted in some online/cloud platform like Github. First, you need to add a remote repository URL:
git remote add origin <repository-url>
Note:
If your repository was cloned directly from the cloud platform. It will already have this information added. You can skip this step.
Then, you can push your changes using:
git push -u origin <branch>
Here <branch>
is typically main
.
Note:
If your repository was cloned directly from the cloud platform. Just rungit push origin <branch>
This command uploads your commits to the remote repository and sets up tracking so future pushes can be done with git push
.
The following steps summarize what you will be doint 90% of the times with git.
git init
or git clone
git add <filename>
or git add .
git commit -m "message"
git push origin main
There is a lot more to know about git, but you can learn the specific features as you need them.
Branches allow parallel development without affecting the main code.
Create a new branch:
git branch <branch-name>
Switch to a branch:
git checkout <branch-name>
Create and switch to a new branch:
git checkout -b <branch-name>
List branches:
git branch
Merge a branch into the current one:
git merge <branch-name>
git branch -d <branch-name> # Safe delete (only if merged)
git branch -D <branch-name> # Force delete
git checkout <commit-hash>
To prevent certain files from being tracked by Git, create a .gitignore
file in the repository root and list the files or directories to ignore.
# Ignore compiled files
*.o
*.exe
# Ignore logs
logs/
*.log
# Ignore environment files
.env
To apply changes after modifying .gitignore
, use:
git rm --cached <file>