Skip to content

A Beginner’s Guide to Git for Everyday Use

Published: at 10:00 AM (8 min read)

Watching a full video on Git can feel like a tough task, and it’s not always the most productive way to learn. That’s why I’m writing this blog, to save you time and help you focus on the core concepts.

Caution: This guide covers 80% of what you’ll need in your daily use of Git. If you’re already familiar with >the basics and want to dive into more advanced topics, this might not be the blog for you.

Note: Before diving into Git, think about how you would create a system that:

You might have thought of a folder system(Please at least think for a minute😅), saving different versions of a project in different folders. But this can become tedious and space-consuming. Now that you have a solid understanding of the problem, let’s dive into Git.

Introduction to Git

Git is a version control system (VCS) that stores different versions of your project at various points in time. Each version represents how your code or file was at a specific moment. For example:

Version 1:

// written something

Version 2:

// written something
// written something else

Git tracks these different versions. Although Git is much more powerful, we’ll start with the essentials.

Setting Up Git Config

Open your terminal and type the following commands to set up Git with your information:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --global core.editor "code --wait"

For Windows users:

git config core.autocrlf true

For MacOS/Linux users:

git config core.autocrlf input

Core.autocrlf ensures compatibility between operating systems. Windows uses CRLF (Carriage Return + Line Feed) for end-of-line markers, while MacOS/Linux uses LF (Line Feed). These commands help Git manage line endings properly across different OS.

Installing and Initializing Git

You can install Git from the official Git website, depending on your operating system.

Once installed, initialize Git in your project directory by running:

git init

This command creates a hidden .git subdirectory in your project, where Git stores all snapshots and project history.

Git Workflow

git workflow image

When your project reaches a point where you want to save it, you “commit” the changes, which is like taking a snapshot of your project at that point in time. Before committing, Git uses a staging area to prepare what should be included in the snapshot. In this we propose or put what we want to commit.

Example Workflow:

Create two files in a folder of your choice.

create 2 files

Initialize Git in the project directory:

initialize git repository

git init

It creates a hidden folder named .git which contains all the snapshots and your configuration for the project. You can view it using the following command in linux/macos:

ls -al
ls -Force  /*and for windows you can do this:/*

Add files to the staging area:

stage file

git add file1.txt file2.txt

Check the status(current state of your project) of your project:

git status

Commit your changes with a message:

git commit

It will open your default text editor(vscode(what I use), vim, neovim or any other according to your configuration). Here, you can type your commit message: image creating first commit

Commit guidelines/ best practices:

  1. Not too short not too large but meaningful and represent the state of the code/project at that point.
  2. Not too often and not frequently(after some minor file updates). As you reach a state you want to record then you commit.
  3. Each commit should represent a logically separate change set.
  4. Usually present tense is used in commit messages { fix the bug than fixed the bug}

You can also create commit directly using following command(do this only if you are sure of what you are commiting):

git commit -m "Initial commit"

You can view the commit history:

git log

git log --oneline /* this shows shorter(oneline) version of commit */
git log image

Useful Git Commands

Here are some key Git commands to help you get started:

To explore other commands first of all let’s write something to file1.txt: append something to a file

git diff

It shows the changes in your working directory that are not yet staged. It means you have changed some file but you haven’t added them in staging area. git diff view in terminal

Here’s whats happening here:

View changes in the staging area:

git diff --staged
shows git staged in terminal

Shows the differences between the staged changes (the changes that are ready to be committed) and the last commit. This command helps you review what changes are currently staged for the next commit.

Show changes from a specific commit:

git show HEAD~1
git show head

Here it shows nothing because: HEAD is a pointer that points to the commit you currently are in. And ~1 indicates “one commit before HEAD.”So HEAD~1 points to the commit that was the last before the current commit.

Note: Use HEAD~n to go back n commits.

We have a single commit currently in our repository hence, the commit before the current commit doesn’t exists.

So, you can also use the hash of a commit instead of HEAD~1 to show the information about commit git show b95e47e1eaf3 git show using hash

Restore changes: If you want to undo changes in the staging area:

git restore --staged file1.txt

You can see how to restore, when using command git status. This command will be useful if you accidentally added something in the staging area using git add file...

To undo local changes:

git restore file1.txt

The command git restore file1.txt is used to discard changes in the file file1.txt and restore it to the state it was in at the last commit.

Say you have made some changes in your file after commiting(like we have added in file1.txt) and you haven’t yet added them in staging area or commited. Now how do you remove the changes you made to the local file?? Here’s where you use this command.

By using previous command we removed our file1.txt from staging area but it still exists in our local file. Let’s remove it. image removing file1.txt from local file

Note: Untracked files are not removed by restore(it means that files that are not yet tracked by Git (i.e., files that have never been added to the staging area with git add) are unaffected by the git restore command.). To remove them we use(This will remove all untracked files.):

git clean -f

Reset your project to a previous state:

git reset <commit-id>

Say you accidently commited something and you want to undo it. We use this command to do so. You can also use HEAD~n instead of commit-id to go to previous commit(determined by n).

This will reset your project to the specified commit(change the state of your project to what it was when you commited the commit commit-id)

image of adding something to file1.txt and commiting it

Now let’s reset our repository to before we accidently commited just now.

image reseting to first commit

Conclusion:

Git is a powerful tool that allows you to track and manage the different versions of your project, making collaboration and project management easier.

In this blog, we’ve covered the basics you’ll use every day. As you grow more comfortable with Git, you’ll discover its advanced features, but mastering the essentials is a great first step.

And we use Github to store our project with git repository. I’ll be creating separate blog for that. So stay tuned for that.

If you still have confusion regarding Git then I also have a video explaining it. Git Tutorial for Beginners


Previous Post
Khalti Payment Gateway Integration Guide