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
If you want to add all modified files to the staging area, you can use:
bash
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
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
If you've already added the files to the staging area and want to see the staged file differences, use:
bash
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
To move or rename a file, use git mv
:
bash
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
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.
- Introduction to Git and Version Control
- Installation of Git and Initial Setup
- Understanding Repositories
- Basic Workflow in Git
- Working with Branches in Git
- Collaborating with Other Developers
- Undoing Changes in Git
- Working with Tags in Git
- Rebase and Squash in Git
- Stashing and Cleaning in Git
- Advanced Git Commands
- Hooks and Automation in Git
- GitHub and Repository Management
- Best Practices in Git
- Conclusion and Final Tips