How Do Git Repositories Work?

How Do Git Repositories Work?

Written by Dev Yoda on Aug 16th, 2021 Views Report Post

Introduction

Git is an open source version control system used to take snapshots of changes in files over time and store those changes in a .git folder. The folder is called a git repository. Tracking changes is essential to fixing mistakes that may be made while updating the code in a project.

Today, you will learn how Git repository works and performs its actions so that you can further integrate it into your project. You will also learn how to use git repositories in your project and deal with errors you might come across when using them.

Initial Configuration of Git

First, you need to install Git on your local machine. You can check their website on how to do that.

After installation, open your terminal/cmd:

  • For Windows: Search for "cmd".
  • For Linux: On your keyboard, press ctrl + alt + t.
  • For iOS: On your keyboard, press command + space and type "terminal".

To check if Git has been installed successfully on your system, run $ git --version. The version of Git you are currently using will be displayed.

Now let's set up our configuration settings. We will initialize name and email. Run this to save your name: $ git config --global user.name "Enter Your Name"

Then run this to save your email: $ git config --global user.email [email protected]

Application of Git Repositories

To begin, let's create a folder for our project. Open your terminal/BASH and navigate to the path of the folder you just created.

Next, run: $ git init. This is used to initialize an empty repository for your project. Run $ ls -a and you should see a new folder created in that directory titled ".git". That hidden folder is your git repository. If it is deleted, your project history goes along with it, so don't touch it.

Essential Git Commands

git add  

This command is used to put your changes in the staging area. Before your changes are stored in the repository, they are placed in the staging area. The staging area is where you add a version of a file that you want to save in your next commit.To see this in action, let's create a file first.txt in the folder we created above and initialized it with Git.

Now run $ git status to show if there are any changes to our project.

The "untracked files" show that you have not put the changes in the staging area yet.

To do that, run $ git add first.txt. You'll see that your changes have been put in the staging area, ready to be committed into the repository.

An alternative command to stage changes is $git add. This will stage all the files changed or deleted instead of staging just one file.

When a file is placed in the staging area, it can be removed with:

$ git reset HEAD -- <name of file>

Just like $ git add, you can unstage files recursively by directory, and you can also unstage everything at once. Run this from the root directory of your repository:

git reset HEAD -- .

git commit

$ git commit is used to save a snapshot of the changes in the staging area and save it in your repository.

To do that, run: $ git commit -m "initial commit"

The -m flag shows that you want to add a message to the commit: "initial commit" is the commit message. This message is important because it helps describe the changes made in that commit.

You can also commit without adding your changes to the staging area with: $ git commit -am "initial commit"

git log

Running $ git log will display all the commits made to the repository. 

git clone

If there is an existing repository that you would like to contribute to, use $ git clone <url> to get a replica on your local device. You can get an existing repo from either GitHub or GitLab.

Run the following: $ git clone https://github.com/khabdrick/gistmedia.git

Running that initializes a .git directory inside it, and pulls down all the data for that repository.

.gitignore

When working on a project, there are some files you wouldn't like to put in your GitHub repo. To make it work, you have to create a file .gitignore at the root of your project.

name_of_folder # Ignore a particular folder
*.<file_extention> #  is used to ignore all the files with that extension
env # Ignore a paticular file

git push and git pull

$ git push 

This command is used to push code from your git repository into a remote repository.

$ git pull

Use this command to pull the changes made in your remote repository into your local git repository.

What is a Remote Repository?

Source

All git repository functions work on your local machine, and all the data for the git repos are stored on your local machine. Thus, your repo is not easily accessible to other people who aren't near you, making collaboration hard.

Remote repositories solve this problem. In remote repositories, your repo is put somewhere on the web, making it accessible to everyone around the world.

$ git remote add <name> <url_of_repo>

The command above is used to attach a remote to your repository. 

Some Common Errors When Using Git Repositories

Alert fatigue is a common problem for many developers --- you just get bombarded with so many alerts all the time that you sometimes ignore the ones that are detrimental. Many Git errors, thankfully, need to be addressed immediately otherwise you cannot proceed with deployment. Here are the most common errors with Git.

fatal: remote origin already exists

This error can occur when you try to add a name "origin" to a remote repository after a name "origin" has already been added to that same remote repo.

 Imagine you just cloned a remote repo.

$ git clone https://github.com/khabdrick/mk.git

Add a name "origin" to the remote repo with:

$ git remote add origin https://github.com/khabdrick/mk.git

If you forget you added the name "origin" to the remote repo and try to add it again,  the fatal: remote origin already exists is displayed.

fatal: not a git repository(or any of the parent directories)

This error occurs when you try to use a Git command in a project path that Git is not initialized in. To replicate this error, navigate to a random folder on your computer and run $ git add ..

To fix this, try navigating to the correct directory. Also, run $ ls -a on the root of the project to see if there is a .gitfolder. If Git is not initialized, just run $git init.

fatal: refusing to merge unrelated histories

This error occurs if you have a Git repo with its commits and you are trying to pull a remote repository that already exists and already has commits in it. This error will occur because the .git folders in both repos will be conflicting, and Git cannot tell how the two repos are related.

To fix it, use the following: git pull origin main --allow-unrelated-histories

Conclusion

Git repositories are a very crucial part of software development. Even though there are other version management systems, Git has stood the test of time and is used by at least 99% of software companies globally. Therefore, it is always a good idea to learn how to use it.

Comments (0)