Linux git Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux git command with practical examples. You will start by initializing a new Git repository, then add and commit files to the repository. Finally, you will explore Git branch management. The lab covers essential Git commands and concepts, providing a hands-on experience for managing version control in your development projects.

The lab guides you through the process of setting up a Git repository, configuring your user information, and performing basic Git operations such as adding and committing files. These fundamental skills are crucial for effectively collaborating on code projects and tracking changes over time.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") subgraph Lab Skills linux/cd -.-> lab-422701{{"`Linux git Command with Practical Examples`"}} linux/mkdir -.-> lab-422701{{"`Linux git Command with Practical Examples`"}} linux/ls -.-> lab-422701{{"`Linux git Command with Practical Examples`"}} end

Initialize a Git Repository

In this step, you will learn how to initialize a new Git repository and set up your local development environment.

First, navigate to the project directory where you want to create the Git repository:

cd ~/project

Next, initialize a new Git repository using the git init command:

git init

Example output:

Initialized empty Git repository in /home/labex/project/.git/

The git init command creates a new .git directory in your project folder, which is where Git stores all the version control information.

Next, let's configure your Git user name and email address. These settings will be associated with any commits you make in this repository:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

This sets your user name and email globally for all Git repositories on your system. You can also set these values at the repository level if needed.

Now your Git repository is initialized and configured. In the next step, you will learn how to add and commit files to the repository.

Add and Commit Files to the Git Repository

In this step, you will learn how to add files to the Git repository and commit the changes.

First, let's create a new file in the project directory:

echo "This is a test file." > test.txt

Now, let's check the status of the Git repository:

git status

Example output:

On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        test.txt

nothing added to commit but untracked files present (use "git add" to track)

As you can see, the test.txt file is listed as an untracked file. To add it to the Git repository, use the git add command:

git add test.txt

Now, let's check the status again:

git status

Example output:

On branch master

No commits yet

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

The file is now staged and ready to be committed. To commit the changes, use the git commit command:

git commit -m "Add test.txt file"

Example output:

[master (root-commit) 1234567] Add test.txt file
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

The -m flag allows you to provide a commit message, which is a good practice to describe the changes you've made.

Now your file has been added and committed to the Git repository.

Explore Git Branch Management

In this step, you will learn how to work with Git branches, which are essential for managing different versions of your project.

First, let's check the current branch:

git branch

Example output:

* master

As you can see, you are currently on the master branch.

Now, let's create a new branch called feature/new-page:

git checkout -b feature/new-page

Example output:

Switched to a new branch 'feature/new-page'

The git checkout -b command creates a new branch and switches to it.

Let's make some changes to the test.txt file in the new branch:

echo "Adding a new line to the test file." >> test.txt

Now, let's check the status:

git status

Example output:

On branch feature/new-page
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:   test.txt

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

The changes are made in the feature/new-page branch, but not yet committed.

Let's commit the changes:

git add test.txt
git commit -m "Add new line to test.txt"

Example output:

[feature/new-page 7890abc] Add new line to test.txt
 1 file changed, 1 insertion(+)

Now, let's switch back to the master branch:

git checkout master

Example output:

Switched to branch 'master'

You can see that the changes made in the feature/new-page branch are not present in the master branch.

This is the basic workflow of working with Git branches. You can create, switch, and merge branches as needed to manage different versions of your project.

Summary

In this lab, you learned how to initialize a new Git repository, configure your Git user name and email, add files to the repository, and commit the changes. You started by navigating to the project directory and using the git init command to create a new Git repository. Then, you configured your Git user name and email using the git config command. Next, you created a new file, test.txt, and used the git add command to stage it for commit. Finally, you used the git status command to check the status of the repository and understand the changes that were ready to be committed.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like