Use git status to Check Ahead Commits
In this step, we'll learn how to use the git status
command to see if our local branch has commits that are ahead of the remote branch. This is a common scenario when you've made changes locally and haven't pushed them to a remote repository yet.
First, let's make sure we are in our project directory. Open your terminal and navigate to the my-time-machine
directory:
cd ~/project/my-time-machine
Now, let's create a new file and add some content to it. We'll call this file future_plans.txt
:
echo "Plan for world domination." > future_plans.txt
Next, we need to stage this new file to prepare it for a commit:
git add future_plans.txt
Now, let's create a commit with a message describing our change:
git commit -m "Add future plans"
You should see output similar to this, indicating that a new commit has been created:
[master abcdefg] Add future plans
1 file changed, 1 insertion(+)
create mode 100644 future_plans.txt
We have now created a new commit on our local master
branch. However, this commit only exists locally and has not been sent to any remote repository.
Let's use git status
to see the current state of our repository:
git status
The output should now show that your local branch is ahead of the remote branch (if you had one configured, which we don't in this basic example, but Git still gives you the hint):
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: future_plans.txt
nothing to commit, working tree clean
The important line here is Your branch is ahead of 'origin/master' by 1 commit.
. This tells us that our local master
branch has one commit that is not present on the origin/master
branch (which is the default name for the remote branch Git expects).
This is a very useful piece of information. It lets you know that you have local changes that haven't been shared with others yet. In a real-world scenario with a remote repository, this would indicate that you need to git push
your changes.