Git is useful for anyone who writes code or tracks changes to files, from web developers to app developers. So, what exactly is it, and why should you start using it?
What is Git?
Git is a version control system that tracks file changes. Using Git allows you to keep a record of all changes and return to specific versions as needed. It is simple to use, takes up little space, and is extremely productive. Its branching model distinguishes it from nearly every other SCM available. The ability to merge changes from multiple people into a single source is what makes Git so simple. You can use GitHub or other online hosts to store backups of your files as well as their revision history.
Git’s main components
To me, Git is a wonderful tool to use in team projects because it helps to avoid confusion in code and brings a simple yet effective system to work. Here, I’d like to cover up the main components of Git:
A Git repository (or simply repo) contains all of the project files as well as the entire revision history. You'll take an ordinary folder of files (such as the root folder of a website) and tell Git to turn it into a repository. This creates a .git subfolder in which all of the Git metadata for tracking changes is stored. Simply put, a repository is a place where you keep your code.
To add new code to the repository, you need to make acommit, which is a snapshot of your repository at a particular point in time. commits a specific change, or series of changes, to a file in the repository. Git's history is made up of successive commits.
A branch is used to store your changes until they are ready. While the main branch (master) remains stable, you can work on a branch. When you're finished, you can merge it with the master. The great advantage is that you can have a few branches in one repository and merge them whenever you need.
This is a technique used in Git for discussing changes before they are merged into your codebase. A pull request is more than just a notification; it's a dedicated discussion forum for the proposed feature. This is especially convenient when several people are working on the same code, allowing developers to check each other's work.
Now that we have briefly discussed the main theoretical Git components, I want to list 10 basic Git commands that every developer must know before starting to work with Git.
1. Starting a new repository
2. Setting the author name and email address respectively to be used with your commits
git config - - global user.name “[name]” git config - - global user.email “[email address]”
3. Downloading existing source code from a remote repository
git clone <https://name-of-the-repository-link>
4. Creating a new branch
git branch <branch-name>
5. Merging branch in master
git merge <branch-name>
6. Getting updates from the remote repository
git pull <remote>
7. Adding files into the staging area for Git
git add <file or directory name>
8. The current state of the repository
9. Sending the changes made on the master branch, to your remote repository
git push [variable name] master
10. Changing the head (records or snapshots the file permanently in the version history)
git commit -m " Commit Message"
So far, these are the main commands that everyone who works with Git must know. In fact, Git is extremely easy to use, and the number of commands is quite large. But to remember these commands is not a tough task—you simply need to start working with Git, and most of the commands will be remembered intuitively.
Top comments (13)
I believe you did an excellent job explaining Git and its main components. Their list of 10 essential Git commands is very valuable for beginner developers.
This is good article, but also beside this, I think it's important to understand and different things you need to do on local computer (own laptop) and GitHub.
On this link it's little bit explained that things: local computer vs GitHub
Completely agree! Thanks for valuable source👍🏻
Thank you so much, Sergey!
awesome article, but i would also add git fetch
I agree that with these commands you can live a whole life with git (if you are lucky, at least). I saw that someone else mentioned
git switch, but I'd like to chime in on that command. The purpose of
git switchis to switch between branches, or create new branches. All you can do with
git switchyou can do with
git checkout, but
git checkouthas a lot more use-cases so it is like using a sledgehammer for small nails! :)
A couple of commands I've been using a lot since they came out, is
git switch [-c] <branch>to switch to another branch (or create one with -c), and
git restore <files>for restoring working tree files. Adding
git restorewill just restore the index, i.e. un-stage files.
These two were introduced so there's a distinction between switching branches and restoring the index, which can be achieved by the
Hey ! I would replace
git reset(because you already have
git clone). Hard to live without it.
If you want to create an alias named "lg" that shows a history of all commits, you could use the following command:
Wish I could figure out how to use my VS Code interface to correctly use my repositories.