Easy steps to start using Git
How to use git for beginners.
Sun Apr 16 2023 - 4 min read
Git is a source control system very popular and adopted by many in the industry, is a distributed system and is open source and free to use, you can work disconnected, and it is powerful and easy to use, here we are going to see an overview of how to start using Git.
The most powerful way to use Git is using the Command Line although there are GUI interfaces like Git Gui, GitHub Desktop, Source Tree, Git Kraken, and many others, Git shines using the command line, so it is important to get familiar with the CLI (Command Line Interface).
Installation
Git works on Linux, Mac, and Windows.
Check if you have git installed in your machine running this command.
foo@pop-os:~$ git --version
If you don’t have it, download Git, also use an editor of your choice, it can be Visual Studio Code, or any other of your preference.
I will be using a Linux OS distribution called Pop!_OS that is build on Ubuntu, so let’s dive in.
Configuration
The first thing to do after having installed Git is to configure it, use your name, and email, and check that the changes were made.
foo@pop-os:~$ git config --global user.name "Your name"
foo@pop-os:~$ git config --global user.email "youremail@host.com"
foo@pop-os:~$ git config --list
Fundamentals
In a distributed source control system, everyone would have the entire project and full history on their machine, this makes it possible to work offline, and no dependence on a central repository, is normal to have GitHub as a central repository for our projects, so everyone can push and pull from there.
The 3 states of Git
- Committed - The data is stored locally.
- Modified - The file has a change.
- Staged - The modified file is part of the next commit snapshot.
The 3 areas of Git
- Working directory - Content is created, modified, and deleted.
- Staging area - Where changes from the local directory will be staged before being committed.
- Git repository - Local git directory.
The is a 4th area to have in a central place that we can call the Remote repository, which could be GitHub.
Git flow
All interactions with git from the command line start with git.
For configuration as we did before.
foo@pop-os:~$ git config
Initialize a git local repository.
foo@pop-os:~$ git init
or
foo@pop-os:~$ git init [ project name ]
Download a repository from remote.
foo@pop-os:~$ git clone [ repository ]
Prepare a file or files in the staging area.
foo@pop-os:~$ git add .
or
foo@pop-os:~$ git add [ file or files ]
Commit a file to the local repository.
foo@pop-os:~$ git commit -m "Message to go with the commit here"
Example
- Create a new folder, use
mkdir
for creating a new folder where you want to create your repository. - Move to the directory, use
cd
to move to the newly created folder. - Use
git init
to initialize an empty git repository, by default, you are going to be in the master branch or the newly called main branch. - Check files, use
ls -la
to see the hidden files and you will see a.git
directory created by theinit
command, which is the directory where git keeps track of all files in the directory. - Use
git status
to check the status of the current repository. - Open your editor in this location and create a new file, you can name it
readme.md
that is used to describe your repository. - Use
git status
, now it shows an untracked file, the readme file is in color red. - Use
git add readme.md
, this will add the file,git add .
is in case you have many different files changed, but be careful, it is used only if you are sure what files have been changed and you want to commit. - Use
git status
again, now the file is tracked, and the readme file is in green color. - Use
git commit -m "[DOCS] initial commit"
to commit the changes. - Use
git status
and now there is nothing new to commit. - Use
git log
to show your commits.
And that’s it, now you know the basics of working with Git, keep practicing and play with this concepts.