Staging code changes – Source Code Management with Git and GitOps

Git allows developers to stage their changes before they commit them. This helps you prepare what you want to commit to the repository. The staging area is a temporary holding area for your changes, and you can add and remove files from the staging area by using the git add and git restore commands.

Let’s create our first file within the local Git repository and stage the changes:

$ touch file1

Alternatively, you can create a blank file in the first-git-repo directory.

Now, we will check if Git can detect the new file that we’ve created. To do so, we need to run the following command:

$ git status

On branch master

No commits yet

Untracked files: (use “git add <file>…” to include in what will be committed)

file1

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

So, as we can see, Git has detected file1 and is telling us that it is not tracking the file currently. To allow Git to track the file, let’s run the following command:

$ git add file1

Now, let’s run git status again to see what has changed:

$ git status

On branch master

No commits yet

Changes to be committed: (use “git rm –cached <file>…” to unstage)

new file:   file1

As we can see, Git now shows file1 as a new file in the staging area. You can continue making changes, and when you are done, you can commit the changes using the following command:

$ git commit -m “My first commit”

[master (root-commit) cecfb61] My first commit

1 file changed, 0 insertions(+), 0 deletions(-)

create mode 100644 file1

Git has now recorded a commit with your changes. Now, let’s look at its status again using the following command:

$ git status

On branch master

nothing to commit, working tree clean

Git is now reporting that the working tree is clean, and there is nothing to commit. It also shows that there are no untracked files. Now, let’s change file1 and add some text to it:

$ echo “This is first line” >> file1

$ cat file1

This is first line

file1 now contains the first line. Let’s go ahead and commit this change:

$ git add file1

$ git commit -m “My second commit”

[master 4c55cf5] My second commit

1 file changed, 1 insertion(+)

As we can see, Git is now reporting that one file has changed, and there is one insertion. Remember when we discussed that Git only tracks the delta changes between commits? That is what is happening here.

In the introduction, we mentioned that Git provides a history of all commits. Let’s look at how we can display this history.

About the Author

Leave a Reply

Your email address will not be published. Required fields are marked *

You may also like these