Git Fundamentals: Add, Commit, Status, Diff

LinuxLinuxBeginner
Practice Now

Introduction

This challenge will test your knowledge of basic Git commands, specifically git add, git commit, git status, and git diff. These commands are essential for version control and managing changes to your Git repository.

Achievements

  • git add: This command adds changes to the staging area, preparing them to be committed.
  • git commit: This command saves changes to the repository, creating a new commit with a unique ID.
  • git status: This command shows the current status of the repository, including which changes are staged and which are not.
  • git diff: This command shows the differences between two states of a file or between two different files.

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) git(("`Git`")) -.-> git/BasicOperationsGroup(["`Basic Operations`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/BasicSystemCommandsGroup -.-> linux/tree("`Directory Tree Display`") linux/VersionControlandTextEditorsGroup -.-> linux/diff("`File Comparing`") git/BasicOperationsGroup -.-> git/add("`Stage Files`") git/BasicOperationsGroup -.-> git/status("`Check Status`") git/BasicOperationsGroup -.-> git/diff("`Compare Changes`") git/BasicOperationsGroup -.-> git/commit("`Create Commit`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/ControlFlowGroup -.-> shell/for_loops("`For Loops`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_ops("`Arithmetic Operations`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/AdvancedScriptingConceptsGroup -.-> shell/adv_redirection("`Advanced Redirection`") subgraph Lab Skills linux/redirect -.-> lab-387715{{"`Git Fundamentals: Add, Commit, Status, Diff`"}} linux/tree -.-> lab-387715{{"`Git Fundamentals: Add, Commit, Status, Diff`"}} linux/diff -.-> lab-387715{{"`Git Fundamentals: Add, Commit, Status, Diff`"}} git/add -.-> lab-387715{{"`Git Fundamentals: Add, Commit, Status, Diff`"}} git/status -.-> lab-387715{{"`Git Fundamentals: Add, Commit, Status, Diff`"}} git/diff -.-> lab-387715{{"`Git Fundamentals: Add, Commit, Status, Diff`"}} git/commit -.-> lab-387715{{"`Git Fundamentals: Add, Commit, Status, Diff`"}} shell/quoting -.-> lab-387715{{"`Git Fundamentals: Add, Commit, Status, Diff`"}} shell/for_loops -.-> lab-387715{{"`Git Fundamentals: Add, Commit, Status, Diff`"}} shell/arith_ops -.-> lab-387715{{"`Git Fundamentals: Add, Commit, Status, Diff`"}} shell/subshells -.-> lab-387715{{"`Git Fundamentals: Add, Commit, Status, Diff`"}} shell/adv_redirection -.-> lab-387715{{"`Git Fundamentals: Add, Commit, Status, Diff`"}} end

Adding Changes to the Staging Area

In this step, you will learn how to use the git add command to stage changes to your repository.

Target

  • Go to the ~/myrepo directory for operations.
  • Stage changes to a file in your repository.
  • View the status of the repository to verify that the changes have been staged.

Result Example

Suppose you have a file called my_file.txt in your repository, the output of the git status command will show that the changes to my_file.txt have been staged:

Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: my_file.txt

Requirements

  • A Git repository with at least one file.
  • Make changes to the file.
  • Verify that the changes have not been staged.
  • Stage the changes.
  • Verify that the changes have been staged.

Committing Changes to the Repository

In this step, you will learn how to use the git commit command to save changes to your repository. The -m option allows you to add a commit message that describes the changes you have made. After running this command, you can use the git status command to verify that the changes have been committed:

Target

  • Commit the changes that you staged in the previous step.
  • View the status of the repository to verify that the changes have been committed.

Result Example

The output of the git status command will show that your repository is up to date:

On branch master
nothing to commit, working tree clean

Requirements

  • A Git repository with at least one file.
  • Make changes to the file.
  • Stage the changes.
  • Commit the changes.
  • Verify that the repository is up to date.

Checking the Status of Your Repository

In this step, you will learn how to use the git status command to check the current status of your repository.

Target

  • View the status of your repository before and after making changes.
  • Understand the output of the git status command.

Result Example

Suppose you have a file called my_file.txt in your repository, and you have made changes to it. Before staging or committing these changes, you can use the git status command to view the current status of your repository. The output of this command will show that there are changes to be committed:

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:   my_file.txt

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

After staging and committing the changes, you can use the git status command again to verify that your repository is up to date. The output of this command will show that your repository is up to date:

On branch master
nothing to commit, working tree clean

Requirements

  • A Git repository with at least one file.
  • Make changes to the file.
  • View the current status of your repository before and after staging and committing the changes.
  • Understand the output of the git status command.

Viewing Differences Between Two States

In this step, you will learn how to use the git diff command to view the differences between two states of a file or between two different files.

Target

  • Make changes to a file in your repository.
  • View the differences between the current state of the file and a previous state.

Result Example

Suppose you have a file called my_file.txt in your repository, and you have made changes to it. After committing the changes, you can use the git diff command to view the differences between the current state of the file and the previous state. The HEAD^ argument specifies the previous commit, and HEAD specifies the current commit. The output of this command will show the differences between the two commits:

diff --git a/my_file.txt b/my_file.txt
index 1234567..89abcdef 100644
--- a/my_file.txt
+++ b/my_file.txt
@@ -1,2 +1,3 @@
This is the first line.
This is the second line.
+This is the third line.

Requirements

  • A Git repository with at least one file.
  • Make changes to the file.
  • Commit the changes.
  • View the differences between the current state of the file and a previous state.

Summary

In this challenge, you learned how to use the git add, git commit, git status, and git diff commands to manage changes to your Git repository. You learned how to stage changes, commit changes, check the status of your repository, and view differences between two states of a file or between two different files.

Other Linux Tutorials you may like