What is Git and why do you need it
Imagine that you are writing code and suddenly realize that the previous version worked better. Or you are working with a team, and you need to combine the changes of several people. Git solves these problems by keeping a history of all changes and allowing you to safely experiment with the code.
Git stores snapshots of your project in a special repository. Each save of changes is called a commit, and you can always go back to any of them.
Git Installation
Before you start, you need to install Git on your computer. Download it from the official website git-scm.com and follow the installer instructions. After installation, open the terminal (command line on Windows or Terminal on Mac/Linux) and check the version with the command:
git --versionIf the installation is successful, you will see the Git version number.
Git Setup
The first thing you need to do is introduce yourself to Git, indicating your name and email. This information will be attached to each of your commits:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"The --global flag means that these settings will be applied to all your projects. You can check the current settings with the command:
git config --listCreating the first repository
Now let's create our first repository. Go to your project folder and run the command:
git initThis command creates a hidden folder .git, in which Git will store the entire history of changes. Your project is now under version control, but there is nothing in the repository yet.
Status check
The git status command shows the current state of the repository. It displays which files have been modified, which are ready to be committed, and which are not tracked at all:
git statusYou will use this command very often. It helps you understand what's going on in your repository right now.
Adding files
Create a file, for example index.html, and add some code to it. Git is not yet tracking this file. To add a file to the tracked files, use the command:
git add index.htmlIf you want to add all the modified files at once, use a dot:
git add .After executing git add, the files are moved to the staging area, from where they will be included in the next commit. This is convenient when you want to save only part of the changes.

Creating a commit
A commit is a snapshot of the state of your project at a certain point in time. Each commit must contain a clear description of what has changed:
git commit -m "Added index.html file with basic structure"The -m flag allows you to add a commit message directly in the command. Try to write meaningful messages that explain what you have changed and why. This is very helpful when you or your colleagues are reviewing the project history.
View history
The git log command shows the history of all commits in your repository:
git logYou will see a list of commits with their hashes (unique identifiers), authors, dates, and messages. For a more compact output, use:
git log --onelineThis command will show abbreviated hashes and commit messages in one line each.
View changes
If you have changed the files but have not yet added them to the staging area, the git diff command will show what exactly has changed:
git diffThis is useful when you want to check your edits before committing. To see changes in files that are already in the staging area, use:
git diff --stagedCancel changes
Sometimes you need to undo changes. If you have modified a file but have not yet added it to the staging area, you can revert it to the last committed version:
git checkout -- index.htmlIf the file is already in the staging area, first remove it from there:
git reset index.htmlThen, if necessary, roll back the changes with the checkout command.
Working with a remote repository
Often, the code is stored not only on your computer, but also on a remote server, for example, on GitHub or GitLab. To link a local repository to a remote one, use:
git remote add origin https://github.com/username/repository.gitHere origin is the standard name for the main remote repository. To send your commits to the server, run:
git push -u origin mainThe -u flag establishes a connection between the local and remote branch. In the future, you can simply use git push.
To get changes from a remote repository, use:
git pullThis command downloads the changes and immediately merges them with your local code.
Cloning a repository
If the project already exists on a remote server, you can copy it to your computer:
git clone https://github.com/username/repository.gitThis command will create a folder with the repository name and download all the code along with the commit history.
Basic workflow
A typical Git workflow looks like this:
Make changes to project files
Check the status with the
git statuscommandAdd changes to the staging area with the
git addcommandCreate a commit with a description of the changes using the
git commitcommandSend changes to the server with the
git pushcommand
Before you start, it's a good idea to update the code with the git pull command to get the latest changes from the team.
Useful tips
Always check the status of the repository before committing. This helps to avoid accidentally adding unnecessary files. Write clear commit messages, use verbs in the imperative mood, for example "Add", "Fix", "Update". Commit logically related changes together — do not put edits from different parts of the project into one commit.
Don't be afraid to experiment. Git always allows you to return to the previous state, so you will not break the project irrevocably. Regularly send code to a remote server — this will protect you from data loss in the event of a computer failure.
Conclusion
We have analyzed the basic Git commands, which are enough to start working with the version control system. As your experience grows, you will become familiar with branches, merges, conflict resolution, and other advanced Git features. But even these basic commands are enough to effectively manage the versions of your code and participate in team projects.
Practice, create repositories for training projects and don't be afraid to make mistakes. The ability to work with Git is a skill that will remain with you throughout your career as a developer and will open the door to participation in open source projects and team development.
Join the educational platform Code, where you will find structured courses in Python, JavaScript, HTML, CSS and other technologies for beginner developers.
Subscribe to our Telegram channelto receive useful articles, programming tips and keep up to date with new materials. Together we will make your path to development simple and clear!
