Git

Git is a distributed version control system for systematically managing the change history of files such as source code and documentation.

It allows you to systematically record and manage changes to code, and helps multiple people work independently on the same project and collaborate efficiently.

This tutorial does not cover everything about Git. Instead, it focuses on the features needed for the BlueBase project.

For a broader understanding of Git, refer to the following resources:

Key Features

  • Version control: Systematically records the history of code changes.

  • Collaboration: Allows multiple people to work independently on the same project.

  • Merging changes: Combines work performed on different branches.

  • Remote repositories: Stores and shares code on servers such as GitHub and GitLab.

Core Concepts

  • repository: The main storage space of a project that maintains the history of file changes.

  • commit: A unit that records changes at a specific point in time. Every commit has a unique ID that can be used to track the change history.

  • branch: An independent line of development within a repository. Development typically branches off from the main branch.

  • merge: The process of combining work from different branches.

  • remote: A remote server location connected to a local repository for storing, managing, and sharing it.

You will gradually learn about another important concept, rebase, later.

Usage Examples

1. Initialize a repository

Initialize a project directory so that it is managed as a repository.

Example: a project named my_repository

# Create a directory
mkdir my_repository

# Move into the directory
cd my_repository

# Initialize the repository
git init

Alternatively, clone a remote repository to your local environment.

Example: my_repository under the your_github_id account on GitHub

# Clone the repository
git clone https://github.com/your_github_id/my_repository.git

# Move into the directory (git is already initialized)
cd my_repository

A directory managed as a repository contains a .git directory. Various information about the repository can be found there:

# View the contents
cat .git/config

2. Switch to a development branch

The default branch is main. During development, it is common to create a new branch, such as dev, work on it, and merge it into main when a unit of work is complete.

If dev exists in the remote repository:

# Create the `dev` branch while tracking `origin/dev`
git checkout -b dev origin/dev

If it does not exist remotely and you want to create a new local branch:

# Create the `dev` branch
git branch dev

# Switch the current working branch to `dev`
git switch dev

3. Modify files

Now modify, add, or delete files as needed.

For example, create a file named temp.txt:

# Create a new file
touch temp.txt

4. Add changes

Add the changes made above so that Git can track them. This is called “staging” the changes.

# Add only this change
git add temp.txt

To add all changes at once:

# Add all changes
git add .

This also stages deleted files as changes, although git rm is commonly used when explicitly removing a file.

5. Commit changes

Create a commit from the changes staged above. This is called “committing” the changes. You can also include a message, which should clearly describe the changes.

# Commit with a message
git commit -m "Add temp.txt"

The dev branch now contains a new commit. You can inspect the commit history using log:

# View the commit history
git log

6. Merge branches

Now merge the work from the dev branch into the main branch. This is called “merging”.

# Switch the current working branch to `main`
git switch main

# Merge the `dev` branch into the current branch
git merge dev

7. Push to the remote repository

Once the local work is complete, upload the commits to the remote repository. This is called “pushing”.

# Push commits to the `main` branch of remote `origin`
git push origin main

8. Summary

# Clone the repository
git clone https://github.com/your_github_id/my_repository.git

# Move into the directory (Git is already initialized)
cd my_repository

# Create the `dev` branch while tracking `origin/dev`
git checkout -b dev origin/dev

# Create the `dev` branch
git branch dev

# Switch the current working branch to `dev`
git switch dev

# Create a new file
touch temp.txt

# Add only this change
git add temp.txt

# Commit with a message
git commit -m "Add temp.txt"

# Switch the current working branch to `main`
git switch main

# Merge the `dev` branch into the current branch
git merge dev

# Push commits to the `main` branch of remote `origin`
git push origin main