Short recap

  • Repositories are databases for content versions

  • Commits are snapshots of the content of a repository

  • Changes must first be added to the staging area before going into a commit


Config

Git is highly configurable.

Configurations can be applied on multiple levels.

More specific configurations overwrite more generic ones.

  • repository
  • user
  • system

View the current configuration:

# list config options specific to the current repo
git config list --local

# list user-level config options
git config list --global

# list the system-level config options
git config list --system

Modifying config options:

# tell git to use VSCode as the default editor
# globally
git config --global core.editor "code --wait"

# tell git to use a different email when committing
# for this project
git config --local user.email personal@mail.com

# unsetting a config option
git config --local --unset user.email

Init

Creating repositories is simple. It creates a .git directory. The current directory becomes the working directory

## inside a directory that you want to track
git init

Status

The status sub-command provides information about the state of the repository.

git status

# prints the following info:

# On branch main
# Changes to be committed:
# ...
# Changes not staged for commit:
# ...
# Untracked files:
# ...

Ignoring files

There might be files in the working directory that should NOT be part of the project’s history.

Such files can be added to the special .gitignore file:

# sample .gitignore file
.env
secrets/
tmp/

Gitignore syntax

# for comments
subdir/ # everything within subdir
/.secret # in repo-root directory only
private-* # wildcard
! # = exception (i.e. don't ignore)

# ignore all .md files, except readme
*.md
!README.md

Do NOT leak sensitive data

It is a common mistake for git-noobies to expose sensitive data when sharing their repositories online.

Make sure to add any passwords, api keys, environment details, and other valuable files to your .gitignore file.

Common mistake with .gitignore

“My .gitignore isn’t working! I ignored my secrets file, but it is still in git..”

Files already tracked aren’t ignored, use:

# makes the file untracked
git rm --cached

Add

Remember, add does NOT make changes part of the history. It only adds them to the staging area.

# add all files (including untracked)
git add -A

# add only changes to already tracked files
git add -u

# interactively add specific hunks
git add -p

# add specific file(s)
git add my-file.txt readme.md

Reset

Use reset to un-stage a change i.e. the change will no longer be in the next commit.

The changes are still present in the working directory.


# removes changes to the file from
# the staging area
git reset filename

# interactively remove hunks from
# the staging area
git reset -p

Reset is a very versatile command. Some options may lead to data loss


Diff

diff allows us to compare changes:

# view all changes in the staging area
git diff --cached

# view changes to a file since the last commit
git diff my-file.txt

Clean

clean deletes untracked files from the working directory:

# remove untracked files in the top level dir
git clean

# preview which files will be deleted
git clean -n

# remove all untracked files recursively
git clean -d

# remove untracked and ignored files
git clean -x

Warning: the files will be totally lost


Commit

Creates a commit with all changes in the staging area.

# opens the default editor for a message
git commit

# inline message
git commit -m "my awesome commit"

# add extra changes to the last commit
# (actually creates a new commit)
git commit --amend

Commit internals

Internally git stores everything as graph of object files.

Once created, commits can NOT be modified.

Commit internals


Writing good commit messages

Writing clear, short, consistent and meaningful commit messages is very important.

# Bad examples
git commit -m "fix"
git commit -m "changed stuff"
git commit -m "asdfgh"

# Good examples
git commit -m "fix: resolved login timeout issue"
git commit -m "feat: added dark mode toggle"
git commit -m "docs: updated install instructions"

In the links below, you will find a spec that can be useful to follow to ensure your commit messages are well structured.


Committing: Best Practices

  • DO: Commit often (multiple times per day)
  • DO: Each commit should represent one logical change
  • DON’T: Wait days/weeks between commits
  • DON’T: Commit broken/untested code

Show

Displays information and changes introduced by a commit.

# display info and all changes in the current commit
git show


# displays info and just the list of changed files
git show --stat

Restore

restore overwrites the contents of the file in the working tree and/or staging area.

# SAFE: removes changes from the staging area
# working dir is untouched
git restore --staged filename

# discard changes in the working directory
# changes will be lost!
git restore filename

Warning: the changes will be totally lost


Remove

git rm tells git to no longer track the given file

# stages a change to no longer track the file
# in the next commit
# DELETES THE file from the working directory
git rm filename

# only stages an instruction to git to
# no longer track the file in the next commit
git rm --cached filename

Log

Use the log sub-command to view the history of changes:

# displays a detailed view of all commits
git log

# displays a short view of all commits
git log --oneline

# displays a short view of all commits
# including file change stats
git log --oneline --stat

Visualization the commit lineage

Each commit holds a pointer to its parent commit.

line of commits

Which of the following is NOT a git command

  • git add
  • git commit
  • git restore
  • git ignore
  • git rm
  • git init

After running git add filename.txt, the file is

  • Committed to the repository
  • Still in the working directory only
  • Staged for the next commit
  • Uploaded to the remote repository

Practise to learn

Git is a very practical system. The aim of this course is to help you learn to use it daily and seamlessly. But for that to happen you need to practise on your own. Start using git to track your projects today!


Your first repository

  1. Create a directory in your home directory called hello-git
  2. Create a repository within the “hello-git” directory
  3. Create a couple of text files (with whatever content you want)
  4. Commit these files to git
  5. Modify the files (contents and rename)
  6. Examine the state of your repository
  7. Commit the newly made changes
  8. Examine the repositories history

Create notes repository

  • Create a repository called “daily-notes”
  • Add a README explaining the purpose
  • Create directories for different subjects/projects
  • Set up a .gitignore file
  • Make at least 5 meaningful commits over the next week
  • Practice unstaging changes with git restore --staged
  • Practice using git log and git diff