Explanation of basic Git terminology
Repository
The repository is the core concept of Git, referring to a database that contains all the historical records and metadata of a project. Each Git project has a hidden .git
directory, which is where the repository is located. There are two types of repositories:
- Bare Repository: A pure repository without a working directory, typically used for server-side sharing.
- Non-bare Repository: A development repository that includes a working directory.
Commands to create a new repository:
# Initialize a new repository
git init
# Clone an existing repository
git clone https://github.com/user/repo.git
Working Directory
The working directory is the directory where developers directly edit files, also known as the working tree. It contains the current state of the project but does not include Git's version control information. Files in the working directory have three states:
- Modified: Files have been changed but not staged.
- Staged: Changes have been marked as ready to commit.
- Committed: Changes have been safely saved in the local database.
Check the status of the working directory:
git status
Staging Area
The staging area is a unique concept in Git, also known as the index. It acts as a buffer, holding changes that are about to be committed. The staging area allows developers to selectively commit partial changes.
Common commands for the staging area:
# Add files to the staging area
git add file.js
# Add all changes
git add .
# Interactive add
git add -p
Commit
A commit is the basic unit of operation in Git, representing a complete snapshot of the project at a specific point in time. Each commit has a unique SHA-1 hash as its ID.
Example of a commit:
git commit -m "Fix login page styling issue"
A commit includes the following metadata:
- Author information
- Committer information
- Timestamp
- Commit message
- Parent commit pointer
Branch
A branch is a lightweight, movable pointer to a specific commit. Git's branching mechanism is highly efficient, as creating and switching branches consumes almost no additional resources.
Branch operations example:
# Create a new branch
git branch feature-auth
# Switch branches
git checkout feature-auth
# Create and switch to a branch
git checkout -b hotfix
Git has a default main branch (usually called master
or main
), from which all development branches are derived.
Tag
A tag is a static reference to a specific commit, often used to mark release versions. Unlike branches, tags generally do not move after creation.
Types of tags:
- Lightweight Tag: A simple pointer to a commit.
- Annotated Tag: A standalone object containing complete information.
Tag operations:
# Create a lightweight tag
git tag v1.0.0
# Create an annotated tag
git tag -a v1.1.0 -m "Official release version 1.1.0"
# Push tags to remote
git push origin v1.1.0
Remote
A remote is a reference to another repository, typically a central repository shared by a team. The most common remote name is origin
.
Remote operations example:
# View remotes
git remote -v
# Add a remote
git remote add upstream https://github.com/original/repo.git
# Fetch updates from remote
git fetch origin
# Push a branch
git push origin main
Merge
Merging is the operation of integrating changes from different branches. Git supports multiple merge strategies:
- Fast-forward Merge: When the target branch is a direct ancestor of the current branch.
- 3-way Merge: Creates a new merge commit when branches diverge.
Merge example:
# Switch to the main branch
git checkout main
# Merge a feature branch
git merge feature-branch
When resolving merge conflicts, Git marks the conflicting sections in the file:
<<<<<<< HEAD
const apiVersion = 'v2';
=======
const apiVersion = 'v3';
>>>>>>> feature-api
Rebase
Rebasing is another way to integrate branch changes by reapplying commits onto a new base commit, resulting in a more linear history.
Rebase operation:
git checkout feature
git rebase main
Interactive rebase allows modifying commit history:
git rebase -i HEAD~3
Checkout
The checkout operation is used to switch branches or restore working directory files. In Git 2.23+, some of its functionality has been replaced by the switch
and restore
commands.
Checkout example:
# Switch branches (old way)
git checkout develop
# Switch branches (new way)
git switch develop
# Restore a file (old way)
git checkout -- file.js
# Restore a file (new way)
git restore file.js
Ref
A ref is a pointer to a commit, including:
- Branch refs (
refs/heads/
) - Tag refs (
refs/tags/
) - Remote-tracking branches (
refs/remotes/
)
View the reflog:
git reflog
HEAD
HEAD is a special pointer that points to the current local branch or a specific commit (detached HEAD state). In the .git
directory, HEAD is usually a symbolic reference.
View where HEAD points:
cat .git/HEAD
Hooks
Hooks are scripts that Git automatically runs when specific events occur, stored in the .git/hooks
directory. Common hooks include:
pre-commit
: Runs before committing.post-merge
: Runs after merging.pre-push
: Runs before pushing.
Example pre-commit
hook:
#!/bin/sh
npm run lint
Submodule
Submodules allow one Git repository to be included as a subdirectory of another repository while maintaining independent version control.
Submodule operations:
# Add a submodule
git submodule add https://github.com/user/lib.git
# Initialize submodules
git submodule init
# Update submodules
git submodule update
Workflow
Git supports multiple collaboration workflows, including:
- Centralized Workflow: Similar to SVN, with a single central repository.
- Feature Branch Workflow: Each feature is developed in an independent branch.
- Git Flow: A strict branching model with
develop
,release
, and other branches. - GitHub Flow: A simplified workflow emphasizing continuous deployment.
Diff
Git provides multiple ways to view differences:
# Differences between the working directory and staging area
git diff
# Differences between the staging area and the last commit
git diff --cached
# Differences between two commits
git diff commit1 commit2
# Summary of changes
git diff --stat
Stash
Stashing temporarily saves changes in the working directory and staging area, making it easier to switch contexts:
# Stash current changes
git stash
# View the stash list
git stash list
# Restore the most recent stash
git stash pop
# Create a named stash
git stash push -m "WIP: auth feature"
Reset
The reset command is used to move the HEAD and current branch pointer:
# Soft reset (moves only the pointer)
git reset --soft HEAD~1
# Mixed reset (default; moves the pointer and resets the staging area)
git reset HEAD~1
# Hard reset (moves the pointer, resets the staging area, and working directory)
git reset --hard HEAD~1
Restore
A new command introduced in Git 2.23+ specifically for restoring files:
# Discard working directory changes
git restore file.js
# Restore a file from a specific commit
git restore --source=HEAD~2 file.js
# Unstage a file
git restore --staged file.js
Bisect
Used to quickly locate the commit that introduced a bug:
git bisect start
git bisect bad
git bisect good v1.0
# After testing, mark the current commit as good or bad
git bisect reset
Patch
Generate and apply patch files:
# Generate patches
git format-patch HEAD~2
# Apply patches
git am *.patch
Credential Storage
Git provides multiple credential storage methods:
# Cache credentials (default 15 minutes)
git config --global credential.helper cache
# Long-term storage
git config --global credential.helper store
# Use the system keychain
git config --global credential.helper osxkeychain
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn
上一篇:Git工作流程概述