10. Working with Local Repositories: Add, Commit, Status, Log

Git is a powerful tool for controlling code versions that facilitates collaboration between developers and the management of changes over time. One of the fundamental concepts of Git is the local repository, which is a copy of your project that you can work on in isolation. Let's explore some of the most important commands for working with local repositories in Git: add, commit, status, and log.

Git Add

The git add command is the first step in the lifecycle of a commit. It is used to add changes to files in your working directory to the Git index, which is a staging area where changes are grouped before being committed to the repository.

git add <file-name>
git add .

Using git add <file-name>, you can add a specific file, while git add . adds all changes to all files in the directory current and subdirectories. It is important to note that git add does not affect the repository until changes are committed.

Git Commit

After adding your changes to the index with git add, the next step is to create a commit with the git commit command. A commit is like a snapshot of your project at a specific point in time, containing a message that describes what was changed.

git commit -m "Explanatory message about the commit"

It's good practice to write clear and descriptive commit messages, as they help other developers (and yourself in the future) understand the history of the project. If you just type git commit, Git will open the default text editor for you to write a longer commit message.

Git Status

The git status command provides information about the current state of the repository. It shows which files have been modified, added or removed since the last commit, which are ready for the next commit, and which are in the working directory but not yet added to the index.

git status

This command is extremely useful for keeping track of the changes you have made and ensuring that all desired changes are included in the next commit. git status does not make changes to the repository; it just provides information.

Git Log

git log is a command that shows the repository's commit history. It lists commits in reverse chronological order (newest first) and includes the commit ID (hash), author, date, and commit message.

git log
git log --oneline
git log --graph

There are several options you can use with git log to customize the output. For example, git log --oneline shows each commit on a single line, making it easier to view many commits at once. git log --graph adds a graphical representation of commit history, useful for visualizing branches and merges.

Conclusion

Working with local repositories in Git involves a cycle of adding changes with git add, committing them with git commit, checking the repository status with git status and review commit history with git log. Mastering these commands is essential for any developer who wants to maintain an organized code history and collaborate effectively on software projects.

It's important to remember that while the commands discussed here are crucial for managing local repositories, Git also offers a variety of advanced tools and techniques for managing branches, resolving merge conflicts, and working with remote repositories like GitHub. Delving deeper into these advanced areas is a natural next step after familiarizing yourself with the fundamentals discussed in this chapter.

With practice and experience, you will become increasingly comfortable with Git, and it will become an indispensable tool for your software development workflow.

Now answer the exercise about the content:

Which of the following Git commands is used to add changes from all files in the current directory and subdirectories to the index, preparing them for the next commit?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Using .gitignore to ignore files

Next page of the Free Ebook:

11Using .gitignore to ignore files

5 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text