Welcome to our comprehensive guide on using git status! In this tutorial, we'll explore how to track changes in your projects using Git, a powerful version control system. By the end of this lesson, you'll be able to confidently navigate your way through the command line and understand the status of your project at any given moment. 📝
Git is an open-source distributed version control system used for tracking changes in projects. It allows multiple people to work on the same project without overwriting each other's changes, and it makes it easy to collaborate, fix bugs, and manage multiple versions of a project.
Using Git is essential for any developer, as it provides a way to manage and track changes to your codebase. It's an indispensable tool for collaborative projects and for maintaining the integrity of your codebase over time.
Before we dive into git status, let's make sure you have Git installed on your machine. You can download Git from the official Git website (https://git-scm.com/) and follow the installation instructions for your operating system.
Once Git is installed, open a terminal or command prompt and navigate to your project directory. If you don't have a project yet, create a new one for this tutorial:
mkdir git-tutorial
cd git-tutorial
touch readme.mdNow that we have a project and a file, let's initialize Git and create a new repository:
git initNow that we have a Git repository, we can start using the git status command. This command tells us the current status of our repository, including which files have been modified, which have been added, and which have been deleted.
When you run git status, Git will display a message that looks something like this:
On branch master
No commits yet
Nothing to commit (create/copy files and use "git add" to track)Let's break down this message:
On branch master: This tells us which branch we're currently on (in this case, the master branch).No commits yet: This indicates that we haven't committed any changes yet.Nothing to commit (create/copy files and use "git add" to track): Git is letting us know that we can create or copy files, and then use git add to start tracking them.Let's add our readme.md file to the Git repository using the git add command:
git add readme.mdNow if we run git status, we'll see a different message:
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: readme.mdThis message tells us that we have a new file in the repository (readme.md) that's ready to be committed.
Now that we've added our file, let's commit our changes with the git commit command:
git commit -m "Initial commit"In this example, we're creating a commit with the message "Initial commit". This message describes the changes we've made in this commit.
After running this command, Git will display a message confirming the commit:
[master (root-commit) 08a8e0d] Initial commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 readme.mdThis message tells us that we've committed one file (readme.md) and that no lines were added or deleted.
Now that we've committed our changes, let's make some changes to our readme.md file and see how git status responds:
nano readme.md(Edit the readme.md file and save it.)
If we run git status now, we'll see a message like this:
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: readme.md
No commits yetThis message tells us that we've modified our readme.md file, but we haven't yet added those changes to the Git repository.
To stage our changes, we use the git add command again:
git add readme.mdNow if we run git status, we'll see a message like this:
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: readme.mdThis message tells us that our changes to readme.md are staged and ready to be committed.
To commit our staged changes, we use the git commit command again:
git commit -m "Modified readme.md"After running this command, Git will display a message confirming the commit:
[master 4973456] Modified readme.md
1 file changed, 1 insertion(+), 1 deletion(-)This message tells us that we've committed one file (readme.md) and that one line was added and one line was deleted.
Here's a quick reference for some common Git status commands:
git status: Shows the current status of the repositorygit add <file>...: Stages changes to be committedgit commit -m "<message>": Commits staged changes with a messagegit restore <file>...: Discards changes in the working directoryWhat does the `git status` command show us about our Git repository?
That's it for our introductory lesson on git status! We've covered the basics of using Git to track changes in our projects, and we've seen how git status helps us keep track of our repository's current state. In future lessons, we'll explore more advanced Git concepts and learn how to collaborate with other developers using Git. Happy coding! 🎉