Git Mastery: A Comprehensive Guide to Version Control with Git : Part-2

Table of contents

From the previous blogs, we have learnt (hopefully :)) that Git is a powerful and popular version control system that allows developers to manage their codebase efficiently. One of the key features of Git is its ability to track changes made to files, from their creation to their final state. In Git, files go through different stages before they are committed and become a permanent part of the repository.

In this blog, we will discuss the different stages of files in Git, how they travel through these stages and also look into some commands while we dive deep into these stages.

I will be using a bunch of files to demonstrate the workflow of these files in git.

The Stages

The different stages of files in Git are:

  1. Untracked files.

  2. Staged files.

  3. Committed files.

Let's explore each stage in detail:

  1. Untracked files:

    When a file is created or modified in a Git repository, it is initially in an untracked state. This means that Git is not yet aware of the file's existence, and it will not be included in the next commit unless it is explicitly added.

    The files shown in red color below are untracked files.

    To add a file to the staging area, you can use the following command:

     git add file_name     # to add a single file.
    
     git add .             # to add all the files in that directory.
    

    This is the confirmation that our files are now staged and Git will be able to track our files.

    This will move our files to the next stage, which is the staging area.

    Note: There is a command in Git called git status. The git status command is a very useful Git command that provides a detailed report of the current status of the repository. It gives information about any changes that have been made to the files in the repository and their current state.

    When you run the git status command, Git will display the following information:

    1. The branch name: Git will show you the name of the current branch you are working on.

    2. The status of each file: Git will display information about each file in the repository, including whether it has been modified, whether it is staged for the next commit, or whether it is untracked.

    3. Untracked files: Git will also show you any files that are present in the working directory but have not yet been added to the repository.

  2. Staged files:

    The files which are green in color are staged files. When a file is in the staged state, Git is aware of its existence, and it is ready to be included in the next commit. The staged state or the staging area is a temporary state, and files can be removed from the staging area at any time.

    To remove a file from the staging area, you can use the following command:

     git reset file_name
    

    Now, if we have staged a file by mistake or we no longer require that file to be in staged state we can use the above command to move the file from staged state back to untracked state.

    As you can see, the file "sampleFile3.txt" has been moved back to untracked state.

    This will move the file back to the untracked state.

  3. Committed files: When a file is committed, it becomes a permanent part of the Git repository. The commit contains a snapshot of the entire project, including all the changes made to the files since the last commit.

To commit changes to the repository, you can use the following command:

git commit -m "message_to_others"

The files are now in the committed state and are a permanent part of the repository.

We can also "uncommit" files that have been committed earlier. However, it's important to note that once you uncommit a file, any changes made to that file will be lost, and you won't be able to recover them.

We can uncommit files using git revert. The git revert command can be used to uncommit files in Git. This command will remove the most recent commit, including all the changes made to the files in that commit. To use this command to uncommit files, follow these steps:

Step 1: Use the following command to list the previous commits:

git log

Step 2: Copy the commit hash for the commit that you want to uncommit.

Note: A commit hash is a unique identifier for a commit in a Git repository. It is a 40-character string consisting of alphanumeric characters that can be used as a reference to a specific commit in Git history.

The commit hash is used by Git to track changes and maintain a history of the repository. It allows developers to refer to a specific commit in the repository and to retrieve the contents of that commit, including the files and changes made.

The string that is next to the word "commit" is called a commit hash.

Step 3:

git revert <commit hash> #to uncommit the files.

The above window will be displayed confirming that we have uncommitted the previous commit. This will create a new commit that reverses the changes made in the previous commit.

Observation: As you can see, the files which I was using earlier were deleted when we used git revert. So, it is worth noting that any changes made to the files in the uncommitted commit will be lost. Therefore, it's always recommended to create a backup or stash the changes before uncommitting files to avoid data loss.

Conclusion

In conclusion, understanding the different stages of files in Git and how they move through these stages is critical for effective version control and collaboration in software development. By using Git commands, developers can manage changes to files, track the history of the repository, and collaborate with others.

I hope this blog has helped you understand the various stages that files go through in Git and how to use Git commands to work with files in each stage. As always, feel free to reach out if you have any questions or need further assistance.

In the next blog we will discuss branching and other essential commands in git. Until then, bye.

Thank you for reading!