In this introductory chapter we will review:
- 010 - Git Configuration
- 011 - Repository Creation
- 012 - Understanding the Staging Lifecycle
- 013 - Git History
- 014 - File Operations
-
Three Git configuration files on your system (from lowest to highest priority):
- System's
/etc/gitconfig - User space:
$HOME/.gitconfigor$HOME/.config/git/config - Repository level:
.git/configwithin your repository
- System's
-
git configcommand let you modify the files above through options:- With
--systemoption, reads/writes from/etc/gitconfig - With
--globaloption, reads/writes from user space, aka$HOME/.gitconfig - With
--localoption, reads/writes from current repository, aka.git/config
- With
-
git config --listshows your current Git configuration.
Step 1: Identify Yourself
Git needs to know who is making the changes. Run these commands:
$ git config --global user.name "Your Name"
$ git config --global user.email "your-email@example.com"Step 2: Set the Default Editor
We will use nano editor in the terminal (use vim only if you are familiar with it):
$ git config --global core.editor nanoStep 3: Set the Default Branch
- Branching is more advanced topic that we will cover in a dedicated section of this tutorial
- Curious fact: in 2020, GitHub moved default branch term from
mastertomain- Reasoning: https://sfconservancy.org/news/2020/jun/23/gitbranchname/
- Versions lower to Git 3.0 still use
masterfor default naming:hint: Using 'master' as the name for the initial branch. This default branch name hint: will change to "main" in Git 3.0. To configure the initial branch name hint: to use in all of your new repositories, which will suppress this warning, hint: call: hint: hint: git config --global init.defaultBranch <name> hint: hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and hint: 'development'. The just-created branch can be renamed via this command: hint: hint: git branch -m <name> hint: hint: Disable this message with "git config set advice.defaultBranchName false"
Ensure that main is always being used as the default branch name:
$ git config --global init.defaultBranch mainIn practical terms, a repository in Git is represented by a folder in your file system. Create a fresh folder (mkdir shell command) for the tutorial and initialize Git:
$ mkdir git-datascience-master-practice
# Be sure to 'cd' into the repository folder
$ cd git-datascience-master-practice
$ git initThe importance of git status to understand each Git operation
git status is an informative command that shall be used anytime we feel we need more information about what's happening within our Git Workflow:
$ git status
On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)Although still working with an empty repository, we gather important pieces of information:
- Our current branch name: this should be
mainif the Git Setup steps above (done throughgit config) have been followed. - Status of the files within the repository: i.e. commits done or to-be-done, tracked/untracked files, ...
- Hints on how to proceed: next steps that are often done at the point where the current status is.
# Note: we use the overwriting sign '>'
$ echo "# My Learning Journey" > README.md
$ git status
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
nothing added to commit but untracked files present (use "git add" to track)What the status information tells us:
README.mdis untracked (under Untracked files)README.mdwas not in the previous snapshot (commit)- Hint:
use "git add" to track
git add marks the changes to the file to be added in the next commit. It does it by moving the change to the Staging Area:
$ git add README.md
$ git status
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.mdWhat the status information tells us:
README.mdis now staged (under "Changes to be committed")- Hint: the Git command to restore the
addcommand (move the staged change back to untracked)
Considerations about the git commit command:
- Marks the registry of a new version, adding the changes that currently exist in the Staging Area.
- Versions can be compared, reverted, etc.
- A commit implies 1-or-more modifications to 1-or-more files.
- Modifications shall be related, atomic and topical
“A commit should contain related changes and nothing but related changes” (codefoster.com)
- The commit message shall be descriptive in the long term: critical to know its purpose at a glance.
- The
git commitcommand alone (without the-moption), opens up the default editor (we configured withgit config)
- The
# Shortcut to provide a commit message through `-m` option
$ git commit -m "Initial commit: add README"
[main (root-commit) bbbaa9f] Initial commit: add README
1 file changed, 1 insertion(+)
create mode 100644 README.md
$ git status
On branch main
nothing to commit, working tree cleanWhat the commit information tells us:
- The identifier of the commit (
bbbaa9f) and the branch (main) where it has been done. - Number of insertions (lines), how many files changed, and file permisions.
What the status information tells us:
- Our working directory is free of untracked or staged changes:
"nothing to commit"
The Status of Files
- Untracked: git does not know about these files
- Tracked: git knows about
- Unmodified
- Modified
- Staged
The Git Workflow
- Edit files → Modified
- Stage the required files (
git add) → Staged - Commit them (
git commit) → Unmodified
- Let's modify again the previous
README.mdfile:
$ echo "Fix #1 added -- You can safely remove this line --" >> README.md
$ git status
On 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: README.md
no changes added to commit (use "git add" and/or "git commit -a")What the status information tells us:
README.mdis unstaged (under "Changes not staged for commit") → modified in the working directory but not staged.
- Add the modification to the Staging Area with
git add:
$ git add README.md
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: README.mdWhat the status information tells us:
README.mdis staged and will go into the next commit
- ..but before the commit, you've just realized that a last minute change is required..
$ echo "Fix #2: Very important fix added -- You can safely remove this line --" >> README.md
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: README.md
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: README.mdWhat the status information tells us:
README.mdis staged and unstaged at the same time!? → Git works with insertions/lines added/removed in files, not with the files themselves
At this point..:
- If you commit now, only the first change in
README.md(“Fix #1..”) will go through. - You need to
git add+git commitagain to stage the last modification.
$ git add README.md
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: README.md
$ git commit -m "fix: unnecessary messages added to the README.md file"Git doesn't just save copies; it records a semantic, searchable timeline of your project's evolution.
To view the commit history of our project we use git log command:
$ git log
commit ef889ad4ca267c17c332bae571bc122f17a9a5e7 (HEAD -> main)
Author: Pablo Orviz <orviz@ifca.es>
Date: Mon Oct 5 13:53:49 2026 +0200
fix: unnecessary messages added to the README.md file
commit bbbaa9f0cecd722a7f2de87efd8a17845609f41c
Author: pablo <pablo@trea.ifca.es>
Date: Mon Oct 5 13:00:08 2026 +0200
Initial commit: add READMEWhat the Git history information tells us:
- Ordered list of commit → Most recent commits show up first.
- Commit data:
- Identifier of the commit.
- Author’s name & email.
- Date
- Author’s commit message
Unlock the power of history inspection with the following flags:
git log --oneline: Condenses every commit into a single, clean line.git log --graph --all: Draws an ASCII visual map of all your branches and merges directly in the terminal.git log -n 1: Shows only the last commits to avoid clutter.git log --author="Pablo": Filters and displays commits matching the author name.git log -1 --patch: Shows modifications of last (-1) change. It usesgit diffoutput.
When you run git log --oneline, you will notice a 7-character code at the beginning of each line (e.g., a1b2c3d). This is the Commit ID (a short SHA-1 hash). Think of it as a unique fingerprint for that specific moment in time.
By capturing these IDs, you can perform surgical operations on your history using the CLI.
1. Inspecting a Specific Commit: git show
If you want to see exactly what changed inside a past commit (who wrote it, when, and the exact lines added or removed), use git show followed by the Commit ID:
git show <commit-id>2. Comparing Two Points in Time: git diff
Instead of guessing what changed between last Tuesday and today, you can use the CLI to compare any two commits instantly.
- Compare a past commit with your current working directory:
git diff <commit-id>
- Compare two specific past commits against each other:
git diff <old-commit-id> <new-commit-id>
Git stands for the development of a clean history. To this end, it provides ways to alter the commits already done at any point in time.
E.g. Fixing the Immediate Past with git commit --amend
- The
--amendflag allows us to modify the very last commit (first appearance in our history). - Useful when: amending a typo, the commit message or any last-minute change not being added.
Exercise:
- Modify the
README.mdfile. - Track the change, adding it to the Staging Area.
- Commit with the
--amendflag to append this change as part of the last commit.
If you want to rename a file or move it to a different folder, use git mv (Git Move).
- To rename a file:
git mv old-name.txt new-name.txt
- To move a file into a folder:
mkdir src git mv new-name.txt src/
Why is this better than associated Shell commands? Run git status right after. You will see that Git instantly recognizes the action as a clean renamed event and automatically adds it to the Staging Area. No extra git add required!
The same applies for deleting files with Git: if you use the Git CLI, you won't need to stage this change with git add.
- To delete a file from both your disk and Git tracking:
git rm src/new-name.txt
Exercise:
- Create a temporary file:
echo "temp" > trash.txt - Track it and save it:
git add trash.txt && git commit -m "Add temporary file" - Now, delete it completely using the proper CLI command.
- Run
git statusto confirm it is staged for deletion, then commit the change.
