DEV Community

Cover image for How to Use Git: A Simple Tutorial for Beginners
Mennatallah Ashraf
Mennatallah Ashraf

Posted on

How to Use Git: A Simple Tutorial for Beginners

Why should every software engineer learn Git?

Every software engineer should learn Git because it is essential for modern software development. Git tracks and manages code changes, allows multiple developers to collaborate without conflicts, and serves as a backup for the code.

For open-source contributions, Git is essential as many projects use it for version control. Proficiency in Git is often required for software engineering jobs, making it important for career growth.


Setting up Git

Before learning how to use Git, you should make sure that you have Git on your machine. To verify that you have Git installed, type the following command at your command line:

git --version
Enter fullscreen mode Exit fullscreen mode

If this command returns the version of Git on your machine, it means you have Git installed.

However, if nothing is returned or you get an output like "git is not recognized", that means you have to install Git on your system. Don't worry, installing Git is easy, and I will show you how to do it whether you are using Linux, Windows, or Mac.

Git Installation

On Linux

Installing Git in Linux is easy all you have to do is to run the following commands.

If you are using a Debian-based distribution like Ubuntu, First, update your local package index:

sudo apt update
Enter fullscreen mode Exit fullscreen mode

then type this command to install Git:

sudo apt install git
Enter fullscreen mode Exit fullscreen mode

But if you’re on Red Hat-based Distributions (e.g., Red Hat Enterprise Linux, CentOS, Fedora), you can use yum:

sudo yum install git
Enter fullscreen mode Exit fullscreen mode

Or you can use dnf:

sudo dnf install git
Enter fullscreen mode Exit fullscreen mode

On Windows

First, navigate to the latest Git for Windows installer and download the most recent version.

Once the installer starts, follow the instructions provided in the Git Setup wizard until the installation is complete.

On Mac

You can follow the instructions provided in this link to install Git on Mac.

Whether you are using Linux, Windows, or Mac, once you've finished installing Git, run git --version to verify that Git is installed.

Configuring Git

Configuring Git is important because it ensures that your commits and interactions with the repository are properly attributed to you and managed according to your preferences.

Setting up username

To set up your username, type this command:

 git config --global user.name "your-name"
Enter fullscreen mode Exit fullscreen mode

Make sure to replace "your-name" with your own username.

Setting up email

Then, set up your email using this command:

git config --global user.email "your-email@example.com"
Enter fullscreen mode Exit fullscreen mode

Make sure to replace "your-email@example.com" with your actual email address.

Initializing a Git repository

To initialize a Git repository, first navigate to the directory where you want to create your repository using this command:

cd /path/to/your/project
Enter fullscreen mode Exit fullscreen mode

Then, you can initialize a repository in this directory by running:

git init
Enter fullscreen mode Exit fullscreen mode

This command creates a new subdirectory named .git in your project directory, which contains all the necessary metadata for the repository.

Congrats, you have initialized your first Git repository! 🥳


Basic Git Commands

After you have initialized your Git repository, you need to know the essential Git commands to manage it, and they are:

git status
Enter fullscreen mode Exit fullscreen mode

The git status command checks what files Git will add to your new repository.

git add .
Enter fullscreen mode Exit fullscreen mode

The command git add . stages all the changes in the current directory and its subdirectories for the next commit.

If you want to stage specific file you can use the following command instead:

git add path/to/your/file/example1.txt
Enter fullscreen mode Exit fullscreen mode

Next you will need to commit the files you have just staged and you can do that by running:

git commit -m "write the commit comment here"
Enter fullscreen mode Exit fullscreen mode

Then if you need to see the history of all the files you have committed use the following command:

git log
Enter fullscreen mode Exit fullscreen mode

It will display all your commits with the author and hash.

But if you prefer to show each commit in a single line per commit, add the --oneline option as follows:

git log --oneline
Enter fullscreen mode Exit fullscreen mode

That is it with basic Git commands. Next, we will learn about branches and how to manage them.


Branching and Merging

Understanding branches in Git

In Git, branches are basically references to a certain commit. They are used to develop features, fix bugs, or experiment in isolation from the main codebase.

Git creates a branch named main (or master in older versions) by default. This is the main line of development.

Creating a new branch and switching branches

If you want to create a new branch but stay on the current branch, use:

git branch branch_name
Enter fullscreen mode Exit fullscreen mode

To create a new branch and switch to it:

git checkout -b branch_name
Enter fullscreen mode Exit fullscreen mode

You may easily return to the previous branch by using:

git checkout -
Enter fullscreen mode Exit fullscreen mode

Renaming a branch

To rename the current branch, use:

git branch -m new_branch_name
Enter fullscreen mode Exit fullscreen mode

If you want to rename another branch:

git branch -m branch_you_want_to_rename new_branch_name
Enter fullscreen mode Exit fullscreen mode

Merging branches

If you want to merge the changes you made from a branch called new_branch to the current branch:

git merge new_branch
Enter fullscreen mode Exit fullscreen mode

After initiating a merging, you may want to pause it and restore everything to its pre-merge condition. Use --abort:

git merge --abort
Enter fullscreen mode Exit fullscreen mode

Deleting branches

To delete a branch locally:

git branch -d branch_name
Enter fullscreen mode Exit fullscreen mode


Be aware that the previous command will fail (produce an error) if the branch branch_name has unmerged changes that would be lost.

In that case, if you really want to delete that branch and don't care about losing the changes that have not been merged, you can force delete the branch (and lose any unmerged changes in that branch) by using the -D flag:

git branch -D branch_name
Enter fullscreen mode Exit fullscreen mode

Conclusion

Learning Git is a must for every software engineer. It helps you track code changes, collaborate smoothly with others, and is often a job requirement. With this guide, you now know how to set up Git, use basic commands, and manage branches.

Get comfortable with Git, and you'll see your development skills and career opportunities grow. Happy learning! 😊

Top comments (0)