Getting Started With Git for Beginners (Part 1)

Photo by Yancy Min on Unsplash

Getting Started With Git for Beginners (Part 1)

... by a beginner for beginners ๐Ÿ˜€

ยท

7 min read

Introduction

Git is a free and open-source version control system that helps programmers track the changes in their code and collaborate with other developers remotely.

Git is a distributed version control system which makes it easy to manage and speed up development operations for a project.

In this blog post, I will be giving guides to getting your hands dirty with the basic operations of version control with Git.

Installing Git

For Linux users ๐Ÿ˜Ž run the following command in your terminal according to the flavour of the distro you are using.

# for Debian/Ubuntu-based distros
sudo apt update && sudo apt install git
# for Arch-based distros
sudo pacman -Syu && sudo pacman -S git
# for Fedora 
sudo dnf install git

For Windows users, if you have the winget package manager run the following command to install Git directly from the command line

winget install --id Git.Git -e --source winget

Or you can just download the .exe installer here. macOS users can also get Git by just running the following command in the terminal.

brew install git

Most of the commands that will be used throughout this blog are bash-shell commands, so if you are using Windows, Git comes with a CLI application called Git Bash mainly for Windows users.

Configuring Git

Now that we have Git installed on our computer, let us make the necessary configs so that we can start making use of the amazing features that come with it.

Firstly, we need to let Git know our name and email, to do this run the following command

git config --global user.name "<Your Name>"
git config --global user.email "<Your email address>"

Then we now set up our line-ending preferences with the following

# For Linux and macOS users
git config --global core.autocrlf input
git config --global core.safecrlf true
# For Windows users
git config --global core.autocrlf true
git config --global core.safecrlf true

Also, you can set up your text editor of choice for editing your commit messages

# for vim text editor
git config --global core.editor "vim"
# for visual studio code
git config --global core.editor "code"

Note you can use any text editor you like (Just make sure you don't use nano๐Ÿ˜… )

Now that is enough for the basic configs we need to get started for further configuration, git-config man page is your friend ๐Ÿ˜€.

You can change all the configs we have created by editing your .gitconfig file.

Git init

Now let's get started with the main point of discussion.

Create an empty directory with the name hello-git

mkdir hello-git && cd hello-git

To create a git repository for the just created folder, run the following command

git init

A git repository helps keep track and saves the history of file changes we are going to be making in our project

git-init-meme.jpg Create a file named hello.txt

touch hello.txt

Since we are using git to keep track of file changes being made in our project, let us check the status of our project as related to our git repository

git status

then you should have an output similar to this

Untitled design.png

From the output of the above command, you will see that hello.txt is being marked as an Untracked file which means that we are yet to add hello.txt into our git repo.

We were able to see the status of hello.txt because we have already initialised the folder containing it (i.e hello-git) as a git repository

Before we add our file to the git repo, you might be wondering how Git gets to know about the changes you make in your project.

This is because upon creation of a Git repository a hidden directory is added to your project which is responsible for source control of our project.

To view this directory, run the following command

# command
ls -a
# output
.git  hello.txt

the folder is the .git folder you can run the following command to see the content of the folder

# command
cd .git && ls
# output
branches  config  description  HEAD  hooks  info  objects  refs

Each of the files and directories inside the .git directory has its function which would be discussed in a future article.

Now, all that apart, let us stage the file changes in our project.

# To stage hello.txt
git add hello.txt

run git status again to see the current state of our file.

# command
git status
# output
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
    new file:   hello.txt

From the output of the above command, we have that hello.txt staged file in our repository. Now let us add our staged file to the repository by committing it.

git commit -m "initial commit"

That's it you just created your first Git commit ๐Ÿ‘๐Ÿ‘

celebrate.gif

In the git commit -m "initial commit", you can replace initial commit with suitable sentences that best describe the changes you have made to your file or projects in entirety.

Run git status again to see the state of our working directory.

git status

Your output should look like this.

On branch master
nothing to commit, working tree clean

Whenever we have this kind of output from git status it simply means that our working directory is clean and there is no change being made yet to the working directory.

Now that we already have the snapshot of hello.txt in our git repo, it's high time we added some texts to our file.

# add a line of text to hello.txt
echo >> hello.txt "Hola Amigos, I am going to commit this line very soon"
# to view the contents of hello.txt run this
cat hello.txt

Check the state of the file by running git status

# command
git status
# output
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")

From the output of the command above it is stated that our file changes is not staged for commit, simply because we have not added it with the git add command. Also, we have hello.txt being labelled as modified compare to the Untracked files we had when we first created the file, this is because we already have a snapshot of our file (i.e hello.txt) in the git repository.

You can now stage the changes and commit.

# stage changes
git add
# commit
git commit -m "yet another commit"

That's quite some good kinds of stuff we have committed to our project ๐Ÿ˜€. Now let's see the log messages of all our commits.

git log

You should have a result similar to this

commit 2c8c27216ec1f81d9452b62808aae32d48fdf603 (HEAD -> master)
Author: Abdulrasheed Fawole <fawomath@gmail.com>
Date:   Tue Sep 6 07:53:38 2022 +0100

    yet another commit

commit 45cf283e315a71663cd1f62e0cd575e3da4e4c45
Author: Abdulrasheed Fawole <fawomath@gmail.com>
Date:   Tue Sep 6 07:36:58 2022 +0100

    initial commit
  • The hash after the commit is the unique identity for every commit you make.
  • The author is the name you input during the configuration of git.
  • And the date marks the exact time and date you made each commit.

The log messages might look overwhelming but in the sequel articles, we'll look at how we can make it look fancier than what we have right now ๐Ÿ˜Œ.

As an exercise try, creating new files in the present working directory, adding some text to them, staging them and committing them. Also, you can try creating a new directory and create a git repo for it, create new files stage them and make many commits as many as you can ๐Ÿ˜€.

Summary

So far we have:

  1. Learnt how to set up Git for different platforms (Linux, Windows and Mac).
  2. Set up our own config file for Git.
  3. Used the git init command to create a new repository for our sample project.
  4. Used git status in checking the status of our working directory with respect to the git repository.
  5. Learned how to stage and commit our file changes with the git add and git commit commands.
  6. Log our commit details using the git log command. That's it for now ๐Ÿคž๐Ÿคž.
ย