Chuck's Academy

GIT

Basic Workflow in Git

Now that we have learned how to create and clone repositories, it's time to explore the basic workflow in Git. The workflow in Git generally follows a simple cycle: make changes, add them to the staging area, and then commit those changes. We will also learn how to view file differences and how to delete or move files within the repository.

Adding Changes to the Staging Area

The staging area in Git is an intermediate step where you select which changes you want to include in the next commit. This gives you control over which exact modifications will be saved in the history.

To add a file or set of files to the staging area, use the git add command.

bash
"With the command 'git add' followed by the file name, you add that file to the staging area. You can use wildcards like asterisks to add multiple files at once."

If you want to add all modified files to the staging area, you can use:

bash
"The command 'git add dot' adds all modified files within the current directory to the staging area."

It's important to note that until you make a commit, changes will not be permanently saved in the repository history.

Committing Changes

Once the changes are in the staging area, you can commit them to save them permanently in the repository history. A commit can be seen as a "checkpoint" in the project's development.

bash
"With 'git commit -m' followed by a message in quotes, you save the changes in the repository. The commit message should be a clear description of the changes made."

Git recommends writing commit messages that explain what was done in the change concisely but meaningfully. Avoid generic messages like "changes" or "adjustments."

Viewing Changes Made

Before committing, it is useful to review what exact changes have been made to the files. To see the differences between the current version and the last committed version, use the git diff command.

bash
"The command 'git diff' shows the differences between the modified files in the working area and the most recent version in the repository. This allows you to verify what changes you've made before committing."

If you've already added the files to the staging area and want to see the staged file differences, use:

bash
"The command 'git diff --staged' allows you to see the differences of files that are already in the staging area. This helps you verify the changes you're about to commit."

Deleting or Moving Files in the Repository

Sometimes, you will need to delete or move files within the repository. To delete a file and record it in the next commit, you can use the git rm command.

bash
"The command 'git rm' removes a file from the working directory and also from the staging area, so the file will be deleted in the next commit."

To move or rename a file, use git mv:

bash
"With 'git mv', you can move or rename a file in the repository. This is useful when reorganizing your files and you want Git to track those changes correctly."

Viewing the Change History

The commit history in Git allows you to see how a project has evolved over time. We've already seen how to use git log to view this history.

If you want to see the commit history with a summary of the differences in the files, you can use:

bash
"The command 'git log --stat' shows the commit history along with a summary of the changes made in each file. This helps you understand which files were modified in each commit."

Conclusion

In this chapter, we have learned how to follow the basic workflow in Git: add files to the staging area, make commits, and view the changes. We also saw how to delete or move files within a repository. These are the essential steps you will follow in your day-to-day work with Git. In the next chapter, we will explore how to work with branches in Git, which will allow you to handle different lines of development in a project.


Ask me anything