📤 An in-depth introduction to Git command line operations
I. Introduction to Git 📚
GitHub is the world's largest code hosting platform. It provides code hosting services based on Git, a version control system.
Version control is a system that records changes in the content of one or several files so that you can check the revision of a specific version in the future.
1. History of Git 🕰️
The Linux kernel open source project has a large number of participants, but most of the Linux kernel maintenance work is spent on the tedious affairs of submitting patches and saving archives (1991-2002). By 2002, the entire project team began using a proprietary distributed version control system called BitKeeper to manage and maintain the code.
BitKeeper was a proprietary and paid-for tool at the time, but the Linux development crew were allowed to use it for free… until BitKeeper founder Larry McVoy took issue with one of the Linux developers over inappropriate use of BitKeeper.
By 2005, the partnership between the commercial companies that developed BitKeeper and the Linux kernel open source community ended, and they took back the Linux kernel community's right to use BitKeeper for free.
In 2005, Linus Torvalds urgently needed a new version control system to maintain the development of the Linux Kernel. So he went offline for a week, wrote a revolutionary new system from scratch, and called it Git.
Since its inception in 2005, Git has matured and become highly user-friendly 💻 while still maintaining the goals set in the beginning. It's fast ⏱️, perfect for managing large projects 🗂️, and has an incredibly non-linear branch management system 🌈.
2. Features of Git ✨
-
High Performance: Efficient compression algorithm, saving storage space 💾, fast branch operation ⚡, almost instantaneous branch switching ⏱️, excellent storage mechanism to avoid storing duplicate content 🚫, high network transmission efficiency, only transmit differential content 📡.
-
Distributed Version Control: Git is a distributed version control system, meaning every developer has a complete copy of the code repository 🗂️ and does not need to rely on a central server to work, supporting offline development 📵, improving the reliability of the system and avoiding a single point of failure ⚠️.
-
Data Integrity Assurance: The SHA-1 hashing algorithm is used to ensure data integrity 🔐, generating a unique hash value for each commit to ensure that the code history cannot be tampered with 📜, providing reliable code traceability 🔍, and any changes in the file will be accurately recorded. In fact, the information stored in the Git database is indexed as a hash of the file's contents, not the file name 🏷️.
-
Ideal for Collaborative Team Development: Complete remote repository support 🌍, convenient code push and pull mechanisms 🔄, and built-in conflict resolution tools 🛠️.
II. Installation and Configuration 💾
1. Installation Methods for Different Platforms 🖥️
2. Initial Configuration 🛠️
Git comes with a tool called git config to help set up Git configuration.
# View all configurations
git config --list
User Information Configuration
Mainly used to identify identity, each commit needs to identify the author, track code contributors, and distinguish between different developers' commits.
# Configure username (replace Github Username)
git config --global user.name "Github Username"
# Configure email (replace Github Email)
git config --global user.email "Github Email"
SSH Configuration
- Encrypt remote repository communication, secure authentication
- Avoid repeatedly entering passwords, password-free repository push and pull
# Generate SSH key (replace Github Email)
ssh-keygen -t rsa -b 4096 -C "Github Email"
# View public key, copy the content
cat ~/.ssh/id_rsa.pub
# Add SSH public key on platforms like GitHub (see image below ⬇️)
# Test SSH connection
ssh -T git@github.com
To resolve the issue "ssh: connect to host github.com port 22: Connection refused" 🤔
You can add the following content to the ~/.ssh/config file. This way, SSH will use port 443 when connecting to GitHub.
Host github.com
Hostname ssh.github.com
Port 443
Configuration Levels
Git has three configuration levels, with priority from high to low:
-
Local Level
- Only affects the current repository
- Configuration file location:
.git/config - Command:
git config --local
-
Global Level
- Affects all repositories for the current user
- Configuration file location:
~/.gitconfig - Command:
git config --global
-
System Level
- Affects all users on the system
- Configuration file location:
/etc/gitconfig - Command:
git config --system
3. Common Configuration Commands ⚙️
# View specific Git configuration (replace <key>)
git config <key>
# git config user.name
# Delete specific configuration (replace <key>)
git config --global --unset <key>
# git config --global --unset user.name
# Text editor: Git calls this when it needs you to input information. If not configured, Git uses system default text editor. (e.g., using nvim editor)
git config --global core.editor nvim
# Directly edit global configuration file
git config --global --edit
# Get help for specific Git commands or subcommands (replace <verb>)
git help <verb>
git <verb> --help
man git-<verb>
# git help config
Proxy settings (if needed, replace proxy.example.com and port)
# Set HTTP proxy
git config --global http.proxy http://proxy.example.com:port
# Set HTTPS proxy
git config --global https.proxy https://proxy.example.com:port
III. Git Basics 🧩
1. Git States 🚦
Files in Git can be in one of three states: committed, modified, and staged.
- Committed means the data is safely stored in your local database.
- Modified means you've changed the file but haven't committed it to your database yet.
- Staged means you've marked a modified file to go into your next commit snapshot.
2. Git Working Areas 🗂️
Working Directory
The actual location where project files are stored, where developers directly edit code, containing all source files and directories of the project.
Features 🎨:
- Untracked files exist here by default
- Can see actual project files
- Directly reflects the current state of code
# Check working directory status
git status
Staging Area
Temporarily stores modifications that will be committed, located in the index file under the .git directory, serving as an intermediate state between the working directory and local repository.
Features 🎨:
- Can precisely control what to commit
- Allows partial file commits
- Provides opportunity for code review
# Add files to staging area (replace <file>)
git add <file>
Local Repository
Stores complete project history, located in the .git directory in project root, contains all commit information and branches.
Features 🎨:
- Completely stores all version history of the project
- Supports version rollback and branch management
- Can work completely offline
# Commit to local repository (replace commit message)
git commit -m "commit message"
# View commit history
git log
Remote Repository
Code repository hosted on network servers like GitHub, GitLab, Gitee, used for code sharing and multi-person collaboration.
Features 🎨:
- Provides code backup
- Supports multi-person collaborative development
- Can host open source or private projects
# Clone remote repository (replace <repository-url>)
git clone <repository-url>
# Push code to remote repository (replace <branch>)
git push origin <branch>
# Pull code from remote repository (replace <branch>)
git pull origin <branch>
3. Workflow Example 🔄
Working Directory → Staging Area → Local Repository → Remote Repository
# 1. Modify file in working directory
vim README.md
# 2. Add modifications to staging area
git add README.md
# 3. Commit to local repository
git commit -m "Update README"
# 4. Push to remote repository
git push origin main
These four areas 🔄 form the core mechanism of Git version control, providing a flexible and efficient way of 📂 code management. Understanding these areas and their interactions is crucial for proficient Git usage.
IV. Basic Git Operations 🖱️
1. Getting a Git Repository 📦
There are two ways to get a Git repository: first is initializing a repository locally (git init); second is cloning an existing Git repository from a server (git clone).
| Feature | Initialize (git init) | Clone (git clone) |
|---|---|---|
| Use Case | New project | Existing project |
| Project History | Start from scratch | Get complete history |
| Remote Repository | Need manual addition | Automatically linked |
Initializing a Repository
git init
Can be done in an empty directory or in a folder with existing files. This command will create a subdirectory named .git to track versions - this is the core of Git repository, recording all history and state information.
Cloning a Repository
# Basic clone command (replace <repository-url>)
git clone <repository-url>
# Clone and specify local directory name (replace <repository-url> <local directory name>)
git clone <repository-url> <local directory name>
# Clone only the most recent commit (shallow clone) (replace <repository-url>)
git clone --depth 1 <repository-url>
# Clone last n commits
git clone --depth n <repository-url>
# Clone specific branch (replace <branch> <repository-url>)
git clone -b <branch> <repository-url>
Two main protocols are used: HTTPS is preferred for public projects, SSH is recommended for development projects.
| Default Port | 443 | 22 |
|---|---|---|
| Security | Medium | High |
| Firewall Passage | Very Good | May be Limited |
| Authentication | Username/Password | Key Pair |
| Initial Setup | Simple | Requires key generation and configuration |
| Each Operation | Requires credentials | Password-free |
| Use Case | Public networks, temporary use | Private environments, frequent operations |
git clone https://github.com/username/repository.git
git clone git@github.com:username/repository.git
2. Adding Files (git add) ➕
# Add single file to staging area (replace <file>)
git add <file>
# Add multiple specified files (replace <filen>)
git add <file1> <file2> <file3>
# Add all new files and modifications in current directory
git add .
# Add all changes including new, modified, and deleted files
git add -A
# Equivalent to
git add --all
# Add only modifications to tracked files
git add -u
# Interactive selection
git add -p
.gitignore Syntax
.gitignore is a text file that tells Git which files or directories should not be version controlled. Proper configuration can greatly simplify Git usage and keep the repository clean. It only affects untracked files.
Ignoring Files/Directories
# Ignore specific files
secret.txt
passwords.json
# Ignore specific directories
logs/
temp/
# Ignore all .log files
*.log
# Ignore all files in specific directory
build/*
Wildcard Usage
# * matches multiple characters
*.txt # all .txt files
*.log # all log files
# ? matches single character
test?.txt # matches test1.txt, testa.txt, but not test10.txt
# ** matches multiple directory levels
**/logs # logs directory at any level
Negation Rules
# Ignore all .log files except important.log
*.log
!important.log
3. Committing Changes (git commit) ✅
# Basic commit (replace commit message)
git commit -m "commit message"
# Multi-line commit message (replace "...")
git commit -m "feat: Add user authentication module
- Implement user registration
- Add login verification logic
- Integrate third-party authentication"
# Directly commit all tracked modified files (replace commit message)
git commit -a -m "commit message"
# Modify the most recent commit message (replace commit message)
git commit --amend -m "commit message"
# Merge new changes into the last commit without modifying commit message
git commit --amend --no-edit
# Open editor to write more detailed commit message
git commit
4. Checking Status (git status) 👀
# Concise output
git status -s
git status --short
# Show branch information
git status -b
git status --branch
Output Information Types
# Unmodified status
nothing to commit, working tree clean
# Untracked files
Untracked files:
(use "git add <file>..." to include in what will be committed)
new_file.txt
# Staged files
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: staged_new_file.txt
modified: staged_modified_file.txt
5. Viewing Differences (git diff) 🔍
# Compare working directory and staging area, show unstaged file modifications
git diff
# Compare staging area and most recent commit
git diff --staged
git diff --cached
# Compare two commits (replace <commit1> <commit2>)
git diff <commit1> <commit2>
# View differences for specific file (replace <file>)
git diff <file>
# Show brief statistics
git diff --stat
6. Viewing Logs (git log) 📝
Helps you track project changes and development trajectory.
Standard log view git log
- Shows all commits in reverse chronological order
- Includes complete commit hash, author, date, and commit message
Concise log git log --oneline
- Each commit shown on one line
- Shows short hash and commit message
# Show last 5 commits
git log -n 5
# Filter by author (replace username)
git log --author="username"
# Filter by date range (replace "date")
git log --since="2023-01-01" --until="2023-12-31"
# View commit history
git log --graph --oneline --all
git log --graph --oneline develop
# View commit and author of last modification for each line in file (replace <file>)
git blame <file>
7. Undoing Changes 🔙
git checkout
Use
--to separate filenames to avoid conflicts with branch names
# Discard changes in working directory for a file (replace <file>)
git checkout -- <file>
# Discard all unstaged changes, use with caution
git checkout -- .
# Switch branches (can also discard changes) (replace <branch>)
git checkout <branch>