Challenge¶
- Run this code. It initializes a new git repo inside a directory named
yolo/
mkdir yolo
cd yolo
git init
Don't forget to cd
to the parent directory where you want the yolo/
directory to reside, before running the
above code.
-
Insert a text file named
foo.txt
insideyolo/
-
Commit your changes with the message "first commit"
How do I create a text file from the command line?
Create an empty file using touch
touch foo.txt
Create a file with some text using echo
echo "Hello World" > foo.txt
Solution¶
git add .
git commit -m "first commit"
Explanation¶
Working Directory
The explanation below assumes that your working directory is the root of the repo. In other words, if you run
bill@gates:~$ pwd
it should return
bill@gates:~$ pwd
/path/to/yolo
Otherwise, you need to
bill@gates:~$ cd /path/to/yolo
Status¶
Before we stage and commit the changes, let's review the state of our git repo using git status
.
bill@gates:~$ git status
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
foo.txt
nothing added to commit but untracked files present (use "git add" to track)
git status
tells us that we have an untracked file - foo.txt
.
Untracked Files
Untracked files are files that have been created within your repo's working directory but have not yet been added to therepository's tracking index using git add
Stage¶
Before we can commit our changes, we need to stage them with git add
.
bill@gates:~$ git add foo.txt
bill@gates:~$ git add .
Let's run git status
again..
bill@gates:~$ git status
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: foo.txt
Now, instead of "Untracked files:"
we see "Changes to be committed:"
Commit¶
To commit our staged changes, we can use git commit
.
bill@gates:~$ git commit -m "first commit" # (1)!
[main (root-commit) 88d8887] first commit
1 file changed, 1 insertion(+)
create mode 100644 foo.txt
-m
is an alias for the--message
argument togit commit
. It should be a meaningful message regarding your changes in the commit.
What if I don't want to specify a message?
Shame! 🔔 You should always provide a commit message!
Log¶
Finally, let's check our commit history with git log
.
bill@gates:~$ git log
commit 88d8887beaad2cfd37d04fb00f38c05077581057 (HEAD -> main)
Author: bill123 <[email protected]>
Date: Sun Sep 11 13:09:50 2022 -0500
first commit