Git Add and Commit
Level 1 · Your first commits
How a change goes from your files into Git's history. What the staging area is for, what git status tells you, and how .gitignore keeps files out.
Course contentsLesson 2 of 13
Level 1 · Your first commits
Level 2 · Branches and merges
Level 3 · Undo and set aside
Level 4 · Remotes and GitHub
Level 5 · Working as a team
After this lesson you can
- Stage a change and commit it
- Read the output of git status
- Keep secret files out with .gitignore
12 mincovers 5 glossary entries
Worth reading first: Version Control and Git
How a change becomes a commit
A commit is a saved version of your project, with a note saying what changed. Making one takes two commands. First, git add picks the changes to save. Then git commit saves them with a message. Each commit gets its own ID, like a205dfe, so you can find it again years later.
Your files live in three places in Git. The working directory is the folder you see and edit. The staging area is a waiting room for the changes you picked. The last commit is what Git has already saved. A change moves from one to the next, one command at a time.
Saving a file in your editor is not a commit. Git does not count it as part of the history yet. If you delete the file or change it again, the old version is gone for good. Only a commit puts a change into the history.
The staging area is there so you can split your work. Say you fixed a bug and changed a colour in the same hour. You can add the bug fix, commit it with its own message, then add the colour and commit again. Each commit then does one clear thing.
Git status tells you where every file sits right now. It lists files you changed but did not stage, files staged for the next commit, and new files Git is not tracking yet. It changes nothing, so you can run it as often as you like. Many people run it before every add.
The short form, git status --short, puts two letters in front of each file. The left letter is the staging area and the right one is your folder. A means added, and M means changed. Two question marks mean untracked: a new file Git has never saved.
Some files should never be saved at all, like passwords, keys and big folders a tool can rebuild. A file called .gitignore lists them, one name or pattern per line. Files that match disappear from git status and cannot be added by mistake. It only works on files Git is not tracking yet.
The most common slip is git add with a dot, which stages every change in the folder. That is how a password file ends up in a shared repo. Run git status first, read the list, then add files by name. Once a secret is shared, change the secret, because deleting the file later does not erase it from the history.
$ git status --short
?? index.html
?? style.css
$ git add index.html
$ git status --short
A index.html
?? style.css
$ git commit -m "Add home page"
[main (root-commit) a205dfe] Add home page
1 file changed, 1 insertion(+)
create mode 100644 index.html# .gitignore
node_modules/
.env
*.logWorked example
Jack splits a messy afternoon
Jack changes 3 files in one afternoon. Two of them fix a broken login form, and one makes the page header green.
He adds the two login files and commits them as Fix the login form. Then he adds the third as Make the header green. When the team dislikes the green, that one commit can go without touching the fix.
The words for this lesson5
Check yourself
Is a file I saved in my editor already safe in Git?
Finished this lesson?
Marking it complete also marks its 5 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.