Git and GitHub course menu
git init, add and commit: Your First Git Repository
Create a Git repository with git init, check its state with git status, stage files with git add, save a snapshot with git commit, and learn the basic Git workflow step by step.
On this page
In this lesson you will create a repository and make your first commits. The same small project is used in many lessons: a folder called notes.
Create a repository#
Make a folder and turn it into a repository:
mkdir notes
cd notes
git initInitialized empty Git repository in /home/you/notes/.git/git init creates a hidden .git folder. That folder is the repository: it stores the history, branches and settings. Deleting it removes the history but leaves your files.
ls -a.
..
.gitCheck the state with git status#
git status is the command you will run most. It changes nothing; it only reports.
git statusOn branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)Add a file and look again:
echo "Buy milk" > notes.txt
git statusOn branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
notes.txt
nothing added to commit but untracked files present (use "git add" to track)The file is untracked: it is in the folder, but Git is not recording it yet.
Stage the file#
git add puts a file on the list for the next commit. This list is called the staging area.
git add notes.txt
git statusOn branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: notes.txtCommit#
git commit saves everything on that list as a permanent snapshot. -m gives the message:
git commit -m "Add shopping note"[main (root-commit) 75e182b] Add shopping note
1 file changed, 1 insertion(+)
create mode 100644 notes.txtThe first line shows the branch, a short ID for this commit, and your message. root-commit means it is the very first commit in the repository.
The basic cycle#
Edit, stage, commit. Do it again with a change:
echo "Call the bank" >> notes.txt
git statusOn branch main
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: notes.txt
no changes added to commit (use "git add" and/or "git commit -a")Git sees that a tracked file was modified. Stage and commit it:
git add notes.txt
git commit -m "Add reminder to call the bank"
git status[main 6b74313] Add reminder to call the bank
1 file changed, 1 insertion(+)
On branch main
nothing to commit, working tree cleannothing to commit, working tree clean means every change is saved. Now see the history:
git log --oneline6b74313 Add reminder to call the bank
75e182b Add shopping noteTwo commits, newest first. You now know the basic loop you will use every day:
edit files --> git add --> git commitAdding many files#
Common mistakes#
Practice#
Practice exercise
Create a repository called recipes. Add a file tea.txt containing Boil water, and commit it. Then add a second line, Add tea leaves, commit that too, and print the history in one-line format.
Show hint
Use echo with > for the first line and >> to add the second.
Show solution
mkdir recipes
cd recipes
git init -q
echo "Boil water" > tea.txt
git add tea.txt
git commit -q -m "Add tea recipe"
echo "Add tea leaves" >> tea.txt
git add tea.txt
git commit -q -m "Add second step"
git log --onelinef7083ec Add second step
4ee4f8c Add tea recipe-q (quiet) hides the usual messages. It is used here only to keep the output short.
Key takeaway#
Next: The Staging Area.
Finished this lesson?