Getting Started with Git and Version Control

Chapter 3
Git Installation, Configuration and Tooling

Diploma in Data science
Getting Started with Git and Version Control   •   Diploma

Git Installation, Configuration and Tooling

Introduction

Git is a distributed version control system used to record, inspect, compare, and coordinate changes to files. In data science, Git supports reproducible analysis by preserving code, documentation, configuration files, notebooks, and selected project metadata. It also provides a professional workflow for collaborating with other analysts, software developers, and stakeholders.

This chapter focuses on the practical foundations required before meaningful version control work can begin: installing Git, establishing a user identity, configuring sensible defaults, selecting command-line or graphical tools, obtaining help, and creating or cloning repositories. These activities are simple technically, but errors in configuration can cause confusing commit histories, incorrect author information, unsafe collaboration practices, or difficulty reproducing work.

Git is different from a cloud storage service. A cloud drive primarily synchronizes files, whereas Git records a sequence of intentional changes. Git is also different from GitHub, GitLab, and Bitbucket. Git is the version control software; these services provide hosting, collaboration, access control, issue tracking, and related features.

Learning Objectives

By the end of this chapter, you should be able to:

  1. Explain the purpose and architecture of Git.
  2. Install Git on Windows, macOS, and Linux.
  3. Configure user identity, editor preferences, default branch names, and useful safety settings.
  4. Distinguish local repositories, working trees, staging areas, and remote repositories.
  5. Use essential command-line and graphical Git tools.
  6. Obtain help from Git documentation and diagnostic commands.
  7. Create a repository and clone an existing repository.
  8. Apply professional practices to repository setup, credentials, and reproducible data-science work.

1. Git as a Version-Control System

Version control is a systematic method for managing successive versions of digital artefacts. A version-control system allows a user to answer questions such as:

  • What changed between two versions of a script?
  • Who made a change and when?
  • Which version produced a reported result?
  • Can an earlier working state be restored?
  • How can two people work on related files without silently overwriting one another?

Git uses snapshots. A commit represents a recorded project state, together with metadata such as the author, message, parent commit, and time. Git does not normally store a complete independent copy of every file for every commit; internally, it identifies content efficiently. Users should nevertheless think of a commit as a durable snapshot of the project at a particular point in its history.

A Git repository contains the history and supporting metadata. The working tree is the set of files currently checked out for editing. Between them is the staging area, also called the index. The staging area allows a user to select precisely which changes will enter the next commit.

The basic Git data flow

The central workflow is:

  1. Edit files in the working tree.
  2. Inspect changes.
  3. Place selected changes in the staging area using git add.
  4. Record the staged snapshot using git commit.
  5. Exchange commits with another repository using git fetch, git pull, or git push.

The staging area is important because a working directory may contain several unrelated changes. For example, an analyst might have corrected a data-cleaning function and also modified a report draft. These changes can be reviewed and committed separately rather than combined into one unclear commit.

Local and remote repositories

A local repository is stored on the user’s computer. A remote repository is another repository, often hosted on a server. A remote may be located on an institutional GitLab server, a cloud platform, or another controlled system. Git itself does not require a remote connection for committing, inspecting history, or creating branches.

This distributed design has two advantages. First, many operations remain available without network access. Second, the local repository contains a substantial copy of the project history, making ordinary operations fast and reducing dependence on a single central server.

2. Installing Git

Installation should be performed from a trusted operating-system package manager or the official Git distribution. Institutions may require a specific version or managed installation. In professional settings, do not install software from an unknown download site and do not bypass organisational security controls.

Windows

On Windows, Git for Windows is commonly installed using the official installer. The installer provides Git Bash, a shell that supports common Unix-like commands, as well as integration with Windows tools. During installation, the default choices are generally suitable for beginners, but users should understand the important options:

  • the default text editor;
  • whether Git is available from Git Bash only or from other command prompts;
  • line-ending conversion behaviour;
  • the terminal emulator;
  • credential-management integration.

A cautious default is to make Git available from Git Bash and supported command prompts, use a familiar editor, and retain the recommended line-ending setting unless a project has a documented alternative. Line endings differ between operating systems, so teams should agree on repository conventions rather than changing settings arbitrarily.

macOS

On macOS, Git may be installed through Apple’s command-line developer tools, Homebrew, or an approved institutional package. Opening Terminal and entering git --version may prompt installation of the developer tools if Git is not already available. Homebrew users commonly install Git with brew install git, subject to local policy.

Linux

Most Linux distributions provide Git through their package manager. For example, Debian or Ubuntu systems commonly use:

sudo apt update
sudo apt install git

The exact command differs across distributions. Fedora, Arch, and enterprise Linux systems use different package-management commands. Administrative privileges may be required.

Verify the installation

After installation, open a new terminal and run:

git --version

A successful installation prints a version such as git version 2.x.y. The exact version is not fixed in this textbook and will change as Git develops. If the command is not found, check that Git is installed, that the terminal was restarted, and that Git’s executable directory is on the system PATH.

Two further diagnostic commands are useful:

which git       # macOS and Linux
where git       # Windows

These commands identify which executable the operating system will use. This matters when multiple Git installations exist.

3. Configuration Levels and User Identity

Git configuration can be set at several scopes:

ScopeTypical commandEffect
Systemgit config --systemApplies to all users on the computer; usually administrative
Globalgit config --globalApplies to the current user across repositories
Localgit config --localApplies only to the current repository
Worktreegit config --worktreeApplies to a particular linked worktree when enabled

A more specific setting normally overrides a less specific one. The local repository configuration therefore takes precedence over the global configuration.

Establishing identity

Every commit records an author name and email address. Configure them explicitly:

git config --global user.name "Amina Khan"
git config --global user.email "[email protected]"

The email should follow institutional or hosting-service policy. Some services provide a privacy-preserving address, and some universities require an institutional address. The identity recorded in a commit is metadata; it is not the same as authentication. A name and email do not grant permission to push to a remote repository.

Check the settings with:

git config --global user.name
git config --global user.email
git config --list --show-origin

--show-origin helps identify whether a value came from a system, global, local, or other configuration file.

Authentication and author identity are different

Git operations involving a remote may require authentication. Modern hosting services commonly use SSH keys, personal access tokens, credential managers, or institutional single sign-on rather than an ordinary account password. Authentication proves access to a remote service; commit identity describes who is recorded as the author of a change. A user can authenticate successfully while having a poorly configured commit identity, and the reverse is also possible.

Do not place passwords, access tokens, private keys, or other secrets in a repository. If a secret is accidentally committed, deleting the file in a later commit may not be sufficient because the secret remains in history. The credential should be revoked or rotated immediately, followed by an approved history-cleaning process.

4. Sensible Defaults and Professional Configuration

Git has many options. A small, deliberate configuration is preferable to copying an unexplained configuration file from the internet.

Default branch name

Many organisations use main as the initial branch name. A user can set this default for newly initialised repositories:

git config --global init.defaultBranch main

This setting affects new repositories created with git init; it does not rename existing branches.

Editor selection

Git sometimes opens a text editor for commit messages or other operations. Configure an editor that you can use confidently. Examples include:

git config --global core.editor "code --wait"

or, on systems where Nano is installed:

git config --global core.editor "nano"

The --wait option is important for editors that otherwise return immediately while Git is still waiting for the message.

Line endings

Text files represent the end of a line differently on different operating systems. Inconsistent handling can create large, misleading diffs. Git’s line-ending behaviour should be coordinated with the project. A repository-level .gitattributes file is often more reliable than relying only on personal settings. A basic example is:

* text=auto
*.sh text eol=lf

This tells Git to detect text files and to use LF endings for shell scripts. Teams should test such rules before applying them to an established repository.

Useful inspection and safety settings

The following commands can improve visibility:

git config --global color.ui auto
git config --global fetch.prune true

fetch.prune true asks Git to remove local references to remote branches that no longer exist when fetching. This changes references, not committed project content, but users should understand the setting before enabling it.

A useful alias can reduce typing, although aliases should remain transparent to collaborators:

git config --global alias.st status

A learner should first understand git status before relying on git st.

5. Creating a Repository

A repository can be created from an existing project directory or obtained by cloning an existing remote repository.

Initialising an existing directory

Suppose a data-science project already contains a Python script, a notebook, and a README file. From the project directory, run:

git init
git status

git init creates a hidden .git directory containing repository metadata. Do not edit files inside .git manually. The command does not automatically add or commit existing files.

A suitable first sequence is:

git add README.md src/ analysis/
git status
git commit -m "Create initial project structure"

The paths are examples. A project should include only appropriate files. Large raw datasets, generated model files, credentials, temporary outputs, and environment-specific caches generally should not be committed unless there is a clear reason.

The .gitignore file

A .gitignore file specifies untracked files that Git should normally ignore. For a data-science project, it may include items such as:

__pycache__/
.ipynb_checkpoints/
.env
.venv/
*.log
.DS_Store

The exact rules depend on the language, operating system, tools, and project requirements. A .gitignore rule does not remove a file that is already tracked. If a sensitive or generated file has already been committed, it must be removed from tracking and handled according to the project’s security process.

A strong initial repository often contains:

  • README.md, explaining purpose and setup;
  • source-code directories;
  • tests;
  • a reproducible environment specification such as requirements.txt or environment.yml;
  • documentation of data sources and licensing;
  • .gitignore;
  • a licence where appropriate.

Cloning a repository

To obtain an existing repository, use git clone:

git clone https://example.org/team/customer-churn.git
cd customer-churn
git status

The clone normally creates a directory, copies accessible history, checks out the default branch, and records the original repository as a remote named origin. Inspect the remote with:

git remote -v

SSH cloning has a form such as:

git clone [email protected]:team/customer-churn.git

The choice between HTTPS and SSH depends on institutional policy, credential management, and the hosting service. Cloning a repository does not automatically grant permission to push changes.

6. Command-Line Tooling

The command line is precise, scriptable, and available on servers and remote computing environments. The following commands form a practical foundation.

CommandPurpose
git statusShows branch, staged changes, and unstaged changes
git logDisplays commit history
git diffShows unstaged differences
git diff --stagedShows staged differences
git addStages selected content
git commitRecords staged content
git branchLists or manages branches
git switchChanges branches or creates one
git fetchDownloads remote references and objects without merging
git pullFetches and integrates remote changes according to configuration
git pushSends local commits to a remote

The most important diagnostic habit is to run git status frequently. It tells the user which branch is active, whether files are untracked, which changes are staged, and which changes remain only in the working tree.

Selecting content carefully

git add file.py stages one file. git add . stages changes beneath the current directory, which can be convenient but risky if the directory contains generated or sensitive files. Review the result with:

git diff --staged

A commit should represent a coherent, reviewable change. Commit messages should describe the purpose of the change, for example:

git commit -m "Add validation for missing age values"

Help and documentation

Git includes several help mechanisms:

git help
 git help status
git status --help
git status -h
g

The first form provides general help; the others provide a manual page or concise option summary. A user should consult the documentation for the installed Git version because options and defaults can vary. Online documentation from the official Git project and the hosting provider should be preferred over unverified snippets.

Useful diagnostic commands include:

git version
git config --show-origin --show-scope --list
git rev-parse --show-toplevel
git remote show origin

git rev-parse --show-toplevel identifies the root directory of the current repository, which is useful when commands are run from a nested folder.

7. Graphical and Integrated Tools

Graphical interfaces can make history, branches, and file differences easier to inspect. Common choices include Git GUI, GitKraken, Sourcetree, command-line interfaces embedded in code editors, and IDE integrations. Their capabilities vary, but most provide views for:

  • changed files;
  • staged and unstaged content;
  • commit history;
  • branches and tags;
  • remotes;
  • merge or conflict resolution.

Graphical tools do not replace Git concepts. A button labelled “Commit” still records staged content, and a button labelled “Sync” may perform several operations whose exact meaning must be checked. Learners should be able to inspect the equivalent command or confirm the resulting state with git status, git log, and git diff.

An integrated development environment may be particularly useful for data-science students because code, notebooks, terminal commands, and version-control information appear in one workspace. However, notebook diffs can be difficult to read because notebooks are structured documents containing code, outputs, metadata, and execution state. Clear notebook practices—such as removing unnecessary outputs, using meaningful cell structure, and separating reusable code into scripts or modules—make version control more effective.

8. Applied Case: Setting Up a Student Data-Science Project

Background

This illustrative case concerns a three-person diploma project analysing public transport delays. The team must preserve code, document data preparation, and allow members to work on separate tasks.

Problem

One student creates a folder containing a notebook, downloaded data, temporary logs, and a file containing an API token. Without an organised repository, team members risk sharing secrets, duplicating files, and losing the exact code used for a report.

Analysis

The team identifies four categories:

  1. Source code and documentation that should be versioned.
  2. Small configuration files that may be versioned if they contain no secrets.
  3. Raw data whose licence and size must be checked.
  4. Secrets, caches, temporary files, and generated outputs that must not be committed.

They agree that the repository will contain a README, a src directory, a notebooks directory, tests, and an environment specification. The raw data will be documented by source and retrieval date; it will not be committed until licensing and size have been assessed.

Approach and solution

One member performs the following setup:

cd transport-delay
printf "# Transport Delay Analysis\n" > README.md
git init
git config user.name "Project Team"
git config user.email "[email protected]"

The team creates .gitignore entries for .env, virtual environments, notebook checkpoints, logs, and operating-system files. They inspect the repository:

git status
git add README.md .gitignore src notebooks tests requirements.txt
git diff --staged
git commit -m "Create reproducible project structure"

Each member clones the repository rather than copying the folder manually. Before making changes, members use git status and create focused branches according to the team’s agreed workflow. The API token is stored in a local environment mechanism and is never written into tracked files.

Outcome

The project has an auditable initial state, a documented setup procedure, and a lower risk of exposing credentials. Team members can identify which changes belong to which task, and the final report can reference a commit associated with the analysis version.

Learning points

  • Repository setup is partly a technical task and partly a governance task.
  • .gitignore reduces accidental tracking but is not a substitute for reviewing staged content.
  • A README and environment specification improve reproducibility.
  • Authentication, author identity, and authorisation must be treated as separate concepts.
  • Data licensing and privacy should be considered before data enters a repository.

9. Challenges, Limitations, and Governance

Git records files effectively, but it is not a complete data-management system. Large binary files and frequently changing datasets can make repositories slow and difficult to maintain. Git Large File Storage or a dedicated data-versioning system may be more suitable, subject to institutional policy.

Git also records that a file changed, not necessarily whether the analytical result is scientifically valid. Reproducibility requires suitable environments, documented dependencies, stable data references, random-seed practices where appropriate, and clear explanations of preprocessing. A commit hash identifies a repository state; it does not guarantee that external data, software libraries, hardware, or execution conditions remain identical.

Data protection is especially important in data science. Personal data, confidential business data, protected health information, and restricted research data should not be uploaded to a public or unauthorised remote. Before creating or cloning a repository, determine its visibility, access controls, retention rules, and backup arrangements. Follow relevant institutional policy and applicable privacy or research regulations.

Commit history may also contain personal information such as names and email addresses. Users should understand the organisation’s retention and disclosure policies. Although Git permits rewriting history, rewriting a shared repository can disrupt collaborators and does not guarantee that previously copied data has disappeared.

10. Contemporary Practice and Best Practices

Modern Git workflows increasingly use hosted services, automated testing, continuous integration, code review, protected branches, and dependency scanning. These tools are valuable only when the underlying repository is understandable and secure. Automation should not be treated as proof that an analysis is correct; it provides repeatable checks against defined conditions.

Recommended practices for a new repository include:

  1. Install Git from an approved source and verify the executable.
  2. Configure an accurate name and approved email address.
  3. Set a familiar editor and an agreed default branch name.
  4. Add .gitignore before staging project files.
  5. Review both unstaged and staged differences.
  6. Make small, coherent commits with informative messages.
  7. Document setup, dependencies, data provenance, and licensing.
  8. Never commit secrets or confidential data.
  9. Use a remote with appropriate visibility and access controls.
  10. Learn both command-line concepts and the behaviour of any graphical client.
  11. Keep generated files and large data objects under a deliberate management strategy.
  12. Use help and diagnostic commands rather than guessing.

Chapter Summary

Git is a distributed version-control system that records project history as commits and supports both local work and collaboration through remotes. Installation should be verified with git --version, and configuration should establish a suitable user identity, editor, default branch name, and platform-aware file-handling behaviour. Git configuration operates at system, global, local, and sometimes worktree scopes, with more specific settings overriding broader ones.

A repository includes a working tree, staging area, and history. The commands git init and git clone create or obtain repositories, while git status, git diff, git add, and git commit support a disciplined local workflow. Graphical tools can improve visibility, but users must understand the Git operations represented by interface buttons. Help commands and diagnostic commands are essential professional tools.

For data-science projects, repository setup must include more than code: documentation, environment information, data provenance, licensing, privacy protection, and secret management are also important. Git improves traceability and collaboration, but it does not by itself guarantee analytical validity or complete reproducibility.

Key Terms

Git: A distributed version-control system.

Repository: A directory containing project history and Git metadata.

Working tree: The checked-out files on which a user works.

Staging area: The selected content prepared for the next commit.

Commit: A recorded project snapshot with history and metadata.

Remote: Another repository used for exchange or collaboration.

Clone: A local copy of an existing repository and its accessible history.

Initialize: To create Git metadata in an existing directory using git init.

Branch: A movable reference to a line of development.

Author identity: The name and email recorded in commit metadata.

Authentication: Verification that a user or process may access a service.

Authorisation: Permission to perform a particular operation.

.gitignore: A file describing normally untracked paths that Git should ignore.

Index: Another name for Git’s staging area.

Diff: A representation of changes between file or repository states.

PATH: An operating-system variable used to locate executable programs.

Credential manager: A tool that stores or supplies authentication credentials.

Repository scope: The configuration level at which a Git setting applies.

Reproducibility: The ability to recreate an analysis using documented inputs, code, and conditions.

Review Questions

5 Short Answer Questions

  1. What is the difference between Git and a Git hosting service?
  2. Why should a user run git status frequently?
  3. What information is recorded by user.name and user.email?
  4. What is the purpose of a .gitignore file?
  5. Name two commands that can be used to obtain Git help.

5 Long Answer Questions

  1. Explain the relationship between the working tree, staging area, repository history, and remote repository.
  2. Describe a reliable procedure for installing and verifying Git on a new computer.
  3. Compare command-line Git tools with graphical Git tools, including advantages and limitations of each.
  4. Explain why authentication and commit author identity should not be treated as the same concept.
  5. Discuss why Git alone cannot guarantee reproducibility in a data-science project.

3 Analytical / Application Questions

  1. A student stages an entire project directory and discovers that it contains a private API token, generated model files, and notebook checkpoints. Analyse the risks and propose a corrected repository setup.
  2. A team uses different operating systems and reports large diffs in files that appear unchanged. Identify a likely technical cause and recommend configuration or repository-level controls.
  3. Design an initial Git setup for a small data-science project containing Python code, notebooks, tests, public data, and confidential local credentials. Specify the files to include, files to ignore, configuration steps, and verification commands.

Multiple Choice Questions with Answers

Question 1

Which Git component contains changes selected for the next commit?

A. The remote repository

B. The staging area

C. The system PATH

D. The working-tree editor

Correct Answer: B. The staging area

Question 2

Which command verifies that Git is installed and reports its version?

A. git verify

B. git status --install

C. git --version

D. git check-version

Correct Answer: C. git --version

Question 3

What is the primary purpose of git init?

A. To upload files to a hosting service

B. To create Git metadata in an existing directory

C. To delete a repository history

D. To authenticate a user

Correct Answer: B. To create Git metadata in an existing directory

Question 4

Which command obtains an existing repository as a local copy?

A. git clone

B. git copy-history

C. git obtain

D. git remote-init

Correct Answer: A. git clone

Question 5

What does git diff --staged display?

A. Differences between remote servers

B. Changes selected for the next commit

C. Only deleted branches

D. The Git installation directory

Correct Answer: B. Changes selected for the next commit

Question 6

Which statement correctly distinguishes authentication from author identity?

A. They are always identical settings

B. Authentication proves access, while author identity is commit metadata

C. Author identity grants permission to push

D. Authentication is stored only in commit messages

Correct Answer: B. Authentication proves access, while author identity is commit metadata

Question 7

Which setting establishes main as the default initial branch name for newly initialised repositories?

A. git config --global branch.main true

B. git config --global init.defaultBranch main

C. git branch --global default main

D. git init --branch-default main

Correct Answer: B. git config --global init.defaultBranch main

Question 8

A .gitignore rule normally affects which files?

A. Already committed files only

B. Normally untracked files matching the rule

C. All files on the remote server

D. The contents of previous commits automatically

Correct Answer: B. Normally untracked files matching the rule

Question 9

Which command shows where a Git configuration value originated?

A. git config --show-origin --list

B. git origin --config

C. git source --settings

D. git inspect-config-location

Correct Answer: A. git config --show-origin --list

Question 10

Why should users inspect staged changes before committing?

A. To make Git install faster

B. To confirm that the intended content, and no secrets or unrelated files, will be committed

C. To convert every file to binary format

D. To create a remote automatically

Correct Answer: B. To confirm that the intended content, and no secrets or unrelated files, will be committed

Getting Started with Git and Version Control
Chapter 3 of 14