Everyday Git: init, clone, status, add, commit, log, diff and .gitignore
In this lesson
The handful of Git commands you will use dozens of times a day, how to read their output, and how to keep secrets and build files out of your repository.
On this page
You now know where changes live. This lesson turns that into a daily routine: start or clone a project, see what changed, commit it in sensible pieces, read the history and keep junk and secrets out. These commands cover most of what a developer does in Git.
Why this matters#
Good everyday habits matter more than rare tricks. A repository with small, clear commits is easy to review and easy to roll back. A repository with one giant "update" commit, or one that has a password buried in its history, is a problem for every teammate and for the server you deploy to. We will also look at what to do when you commit a file you should not have.
All output below comes from a real repository, using Git 2.43. A bare repository on the same machine played the part of the remote server, so we had something to clone.
Starting: init and clone#
You get a repository in one of two ways. git init turns the current folder into a repository. git clone copies an existing repository, with its whole history, from another place.
git clone origin.git api
cd api
git statusCloning into 'api'...
warning: You appear to have cloned an empty repository.
done.
On branch main
No commits yetThe remote in this example was empty on purpose, hence the warning. With a real URL, such as git clone https://example.com/team/api.git, you get all the files. A clone remembers where it came from under the name origin, which lesson 17 covers.
The daily loop#
Most of the time you repeat four steps: change files, check, stage, commit. git status is the one you will type most. Start with a few new files in the working tree:
git status -s?? .env
?? debug.log
?? node_modules/
?? package.json
?? src/.env holds a secret, debug.log is a log file and node_modules/ holds downloaded dependencies. None of those belong in the repository. That is what .gitignore is for.
.gitignore: files Git should leave alone#
.gitignore is a text file in your project that lists patterns for files Git should ignore. Commit it, so everyone shares the rules.
node_modules/
.env
*.log- A name such as
.envmatches that file anywhere in the project. - A trailing slash, as in
node_modules/, matches a folder. *is a wildcard, so*.logmatches every log file.- A leading
!re-includes something. A#starts a comment.
After creating the file, status no longer lists the ignored files:
git status -s
git check-ignore -v .env debug.log node_modules/dep/index.js?? .gitignore
?? package.json
?? src/
.gitignore:2:.env .env
.gitignore:3:*.log debug.log
.gitignore:1:node_modules/ node_modules/dep/index.jsgit check-ignore -v is useful when a file is being ignored and you do not know why: it names the rule and the line. Commit a file such as .env.example with fake values so a new teammate knows which variables to set.
Ignoring only affects files that are not tracked yet. A file already in the history stays tracked even if you add it to .gitignore later.
Staging and committing#
Stage what you want in the next commit, check, and commit with a message.
git add .
git status -s
git commit -m "Add API skeleton"
git log --onelineA .gitignore
A package.json
A src/server.js
f1144da Add API skeletongit add . stages everything in the current folder, so read git status first to be sure you are not staging something unintended. You can stage a single file with git add src/server.js, or go piece by piece with git add -p, which asks you about each changed chunk.
For files Git already tracks, git commit -am "message" stages all modified tracked files and commits in one step. It does not add new files.
echo 'app.listen(port);' >> src/server.js
git diff --stat
git commit -am "Start listening on the port" src/server.js | 1 +
1 file changed, 1 insertion(+)
[main 9fc5d7b] Start listening on the port
1 file changed, 1 insertion(+)If you run git commit with no staged changes, Git says "nothing to commit, working tree clean" and does nothing. If you run it without -m, Git opens your editor for the message. Lesson 21 covers how to write good messages.
Commit one logical change at a time: a bug fix, a feature step, a rename. If you cannot describe a commit in one short sentence, it is probably two commits.
Reading history#
git log shows commits, newest first. The plain form is long, so these shorter forms are the ones people use:
git log --oneline
git log -2 --format='%h %s'
git log --oneline -- src/server.js9fc5d7b Start listening on the port
f1144da Add API skeleton
9fc5d7b Start listening on the port
f1144da Add API skeleton
9fc5d7b Start listening on the port
f1144da Add API skeletonThe third form lists only the commits that touched a file. Other useful options are --stat (files changed per commit), -p (the full patch), --author=name and --since='2 weeks ago'.
git show displays one commit with its changes, and git show HEAD:package.json prints a file as it was in a given commit. HEAD means "the commit you are on now", and HEAD~1 means its parent.
git show --stat --format='%h %s' HEAD
git diff HEAD~1 --stat9fc5d7b Start listening on the port
src/server.js | 1 +
1 file changed, 1 insertion(+)
src/server.js | 1 +
1 file changed, 1 insertion(+)Use git diff in these forms:
git diffcompares the working tree with the staging area (changes not yet staged).git diff --stagedcompares staging with the last commit (what you are about to commit).git diff HEADcompares the working tree with the last commit (everything uncommitted).git diff A Bcompares two commits.
When you commit something you should not have#
It happens to everyone. We accidentally added .env to a commit. The first step is to stop tracking it and ignore it from now on:
git rm --cached .env
echo '.env' >> .gitignore
git add .gitignore
git commit -m "Stop tracking .env"
git ls-files.gitignore
package.json
src/server.jsgit rm --cached removes the file from Git's tracking but leaves it on your disk. The file is gone from the latest snapshot. It is not gone from history. The earlier commit still contains it, and anyone who clones can read it with git show. If the commit was pushed, treat the secret as exposed: rotate it (create a new password or key and revoke the old one). Rewriting history to hide it is a bigger operation, and still does not help if someone has already cloned. The safe habit is to set up .gitignore before the first commit and always read git status before committing.
Fixing the last commit#
If you made a typo in the message or forgot to stage a file, you can replace the last commit before you share it:
git commit --amend -m "Add hello file"This creates a new commit with a new hash in place of the old one. Only amend commits that you have not pushed. Once other people have the commit, replacing it causes trouble, a problem lesson 18 explains.
Other small things you will need#
git mv old newrenames a tracked file, andgit rm filedeletes one. Doing it by hand and then runninggit add -Arecords the same thing.git clean -npreviews which untracked filesgit clean -fwould delete. Always run the preview first.git blame fileshows who last changed each line and in which commit, which helps you find the reason behind a line of code.
Try it#
- Create a new folder with
package.json, asrc/folder, a.envand adebug.log. Rungit initandgit status -s. Create a.gitignorefor the last two, then checkgit status -sagain. - Commit the files with a clear message. Then change one file and compare
git diffandgit diff --stagedbefore and after staging it. - Run
git log --onelineandgit show --stat HEAD. Then view an old version of a file withgit show HEAD~1:path. - Commit a file called
secret.txt, then stop tracking it withgit rm --cachedand ignore it. Usegit log --statto confirm it is still in the older commit. - Make a commit with a typo in its message and fix it with
git commit --amend. Check that the hash changed.
Sign in to keep track of the lessons you finish.