Git Basic Operation Guide

I. What is Git?

1.1 The Birth and Concept of Git

Git is a distributed version control system (DVCS) developed by Linus Torvalds, the creator of Linux, in 2005, initially for managing the Linux kernel code. Unlike traditional centralized version control systems (such as SVN), Git uses a distributed architecture, where each developer has a complete local copy of the repository. This brings three revolutionary advantages: offline work, powerful branch management capabilities, and extremely fast operation speed.
Git’s core function is to record the change history of files, allowing developers to easily manage versions, collaborate as a team, and backtrack code. With Git, you can:

  • Track every modification to the code
  • Create parallel development branches
  • Easily roll back to any historical version
  • Collaborate effectively with team members
  • Complete project history

1.2 Basic Concepts of Git

Before delving into Git operations, it’s essential to understand a few core concepts:

conceptexplain
RepositoryA directory containing all code and version history, essentially a complete backup of the project.
Working DirectoryThe directory where the file currently being edited is located.
Staging area (Stage/Index)A temporary storage area for changes, ready to be submitted.
SubmitRecord each change to the code to create a version history.
BranchAn independent version of the code, allowing for parallel development.
MergeMerge changes from one branch into another branch.
CloningCopy code from the remote repository to your local machine.
PullGet the latest changes from the remote repository.
Push notificationsPush local changes to the remote repository

These concepts form the basis of Git operations, and understanding them will help you use Git more efficiently.

1.3 The Relationship Between Git and GitHub/GitLab/Gitee

Although Git and GitHub are often mentioned together, they are different concepts:

  • Git is an open-source version control system, a tool.
  • GitHub is a Git-based code hosting platform that provides a web interface and additional services.
  • GitLab and  Gitee  are code hosting platforms similar to GitHub, offering different features and services.

Git can be used independently of these platforms, but combining it with these platforms makes team collaboration, code review, and project management much easier.

II. Git Installation and Configuration

2.1 Installing Git on Windows

Installing Git on Windows is very simple; just follow these steps:
Download the installation package: Visit  the official Git website  to download the latest version of the Git installer.
Verify the installation: After installation, open Git Bash and enter the following command to check if the installation was successful:

git --version

If the version number is displayed, the installation was successful.
Insert image description here

2.2 Initial Git Configuration

After installation, some basic configuration is required:

  1. Set global username and email:
git config --global user.name "your name"
git config --global user.email "your email"

This step is very important because this information is used to identify the author every time code is committed.

  1. Check configuration:
git config --list

Confirm that the output contains the configurations for user.name and user.email.

  1. Set the default text editor (optional):
git config --global core.editor "code --wait"  # Using VS Code as an editor
  1. Set up a difference comparison tool (optional):
git config --global diff.tool vscode
git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"
  1. Configure automatic line wrapping (recommended setting for Windows users):
git config --global core.autocrlf true

2.3 SSH Key Configuration (Optional but Recommended)

If you plan to use the SSH protocol to connect to remote repositories (such as GitHub, GitLab, etc.), you need to configure an SSH key:

  1. Generate SSH key:
ssh-keygen -t rsa -C "your email"

Press Enter three times in a row to use the default settings.

  1. View public key:
cat ~/.ssh/id_rsa.pub
  1. Copy the output public key content.
  2. Add a public key to a remote platform:
    Add the copied public key to the SSH key settings of platforms such as GitHub, GitLab, or Gitee.

III. Getting Started with Git

3.1 Local warehouse operations

3.1.1 Creating a new repository

Initialize a new Git repository in the existing project directory:

cd /path/to/your/project  # Enter the project directory
git init  # Initialize Git repository

Insert image description here
This will create a hidden folder named .git in the project directory , containing all Git-related data. In my experience, the first time I typed `git init`, it only displayed the usage of the specific parameters. If you encounter this situation, just type `git init` again.

3.1.2 Cloning a remote repository

If you need to start from a remote repository, you can use the `git clone` command:

git clone https://github.com/user/repository.git  # Clone remote repository to current directory
git clone https://github.com/user/repository.git my-local-repo  # Clone to specified directory

3.2 Basic Workflow

Git’s basic workflow consists of three main steps:

  1. Edit files: Modify code within the workspace.
  2. Stage changes: Add changes that need to be committed to the staging area.
  3. Commit changes: Saves the changes in the staging area to the repository.

3.2.1 Check file status

It is important to understand the current state of the file before performing any operation:

git status  # View the status of the current workspace
git status -s  # View status in concise mode
Insert image description here

3.2.2 Add files to the staging area

Add the file from the working area to the staging area:

git add filename  # Add the specified file to the temporary storage area
git add .  # Add all files (including subdirectories) in the current directory to the temporary storage area
git add *.py  # Add all. py files in the current directory to the temporary storage area

3.2.3 Submit Changes

Commit the changes in the staging area to the local repository:

git commit -m "Common Text"  # Submit changes to the temporary storage area and add descriptive information

Commit message guidelines: Good commit messages help in understanding code changes. It is recommended to follow this format:
<type>(<scope>): <description>
For example…

feat(login): Add user login function
fix(profile): Fix personal information display issue
docs: Update the README document

3.2.4 View Submission History

View the project’s commit history:

git log  # View all submission history
git log --oneline  # View concise submission history
git log -n 5  # View the last 5 submissions
git log --graph  # View Branch Merge Diagram

3.3 Remote Warehouse Operations

3.3.1 Initialize the local repository

  1. Open a terminal (Windows users can use PowerShell or Command Prompt, Mac/Linux users use Terminal).
  2. Navigate to your project folder:
cd C:\Users\Your Name\Desktop\Backtesting
  1. Initialize the Git repository:
git init

3.3.2 Linking a remote GitHub repository

  1. Associate your local repository with your GitHub repository:
git remote add origin https://github.com/user/repository.git

Note: If you have previously associated with other remote repositories, you can delete the old associations first:

git remote remove origin

3.3.3 Add files and commit

  1. Add all files in the project to the staging area:
git add .

(The dot indicates that all files in the current directory should be added. If there are files that do not need to be uploaded, you can create a .gitignore file to exclude them.)

  1. Submit the files from the staging area to the local repository, and fill in the commit message (replace “initial commit” with a detailed description):
git commit -m "Initial submission"

3.3.4 Push to GitHub repository

Push local commits to a remote GitHub repository:

git push -u origin master

3.4 Branch Management

Branching is one of Git’s powerful features, allowing you to perform experimental development without affecting the main code:

3.4.1 Branch View

After changing your current directory using the `cd` command, you can use the corresponding command to view the branches.

git branch
Insert image description here

3.4.2 Creating and Switching Branches

git branch new-branch  # Create a new branch
git checkout new-branch  # Switch to new branch
git checkout -b new-branch  # Create and switch to a new branch

3.4.3 Merging Branches

Merge changes from one branch into the current branch:

git checkout main  # Switch to the target branch (e.g. main)
git merge feature-branch  # Merge feature-branch  to current branch

3.5 Cancel Operation

3.5.1 Undo workspace modifications

git checkout -- filename  # Revoke modifications that have not been temporarily stored

3.5.2 Cancel changes made in the temporary storage area

git reset HEAD filename  # Revoke modifications that have been temporarily stored but not submitted

3.5.3 Revert to a previous version

git log  # Find the commit hash value of the target version
git reset --hard commit-hash  # Fallback to the specified version

3.5.4 Deleting Created Branches

# Safely delete new branches
git branch -d new-branch

Note: `git reset –hard` will discard all subsequent changes, so use it with caution.

IV. Advanced Techniques

4.1 Ignore Files

Create a .gitignore file to specify the files and directories that Git should ignore:

#Ignore all. log files
*.log

#Ignore Node.js directory
node_modules/

#Ignore it DS_Store file (Mac system)
.DS_Store

#Ignore the. idea directory (IntelliJ IDEA)
.idea/

#But keep specific files
!important.txt

Add the .gitignore file to version control to ensure that team members use consistent ignore rules.

4.2 Tag Management

Tags are used to label specific commits in a repository, typically used to mark release points:

git tag v1.0  # Create a lightweight label
git tag -a v1.0 -m "Version 1.0 release"  # Create a footnote label
git push origin v1.0  # Push tags to remote warehouse

4.3 Temporary Storage Function

During development, you might need to temporarily switch branches without committing your changes. In this case, you can use `git stash`:

git stash  # Temporarily store changes in the current workspace
git stash list  # View all temporary storage
git stash apply  # Restore the latest temporary storage
git stash pop  # Restore and delete temporary storage records