Getting Started with Git
Git is a distributed version control and source code management tool that can save several snapshots of your project for version control.
Versions
What is Version Control?
Version control systems are systems that record changes to one or more files over time.
Centralized Version Control vs Distributed Version Control
- The main functions of centralized version control are synchronization, tracking, and backing up files.
- Distributed version control emphasizes sharing changes. Each change has a unique identifier.
- Distributed systems do not have a predefined structure. You can easily implement a centralized system like SVN using Git.
Why Use Git?
- You can work offline.
- Collaborating with others becomes easier.
- Branching is straightforward.
- Merging is simple.
- Git is fast and flexible.
Git Architecture
Repository
A series of files, directories, history records, commit logs, and head pointers. You can think of it as each source code file having historical attribute data structures.
A Git repository consists of a .git
directory and its working directory.
.git Directory (Part of the Repository)
The .git
directory contains all configurations, logs, branch information, and head pointers in detail.
Working Directory (Part of the Repository)
Directories and files in the repository can be considered as the directories you work with.
Index (.git Directory)
The index is the staging area in Git. It acts as an additional layer between your working directory and the Git repository, allowing developers to more flexibly decide what content to add to the repository.
Commit
A Git commit is a snapshot of changes or operations performed on the working directory. For example, if you add five files and delete two files, these changes will be written into a commit, which can later be decided whether to be pushed to another repository.
Branch
A branch is actually a pointer to your last commit. When you make a commit, this pointer automatically points to the latest commit.
Head Pointer and Head (Role of the .git Directory)
A head pointer is a pointer to the current branch, and a repository only has one active head pointer at a time.
A head can point to any commit within the repository, and each repository can have multiple heads (HEAD).
Git Commands
Initialization
Create a new Git repository. Configuration, storage, etc., will be saved in the .git
folder.
$ git init
Configuration
Change settings. These can be repository-specific, system-wide, or global.
# Output and set basic global variables
$ git config --global user.email
$ git config --global user.name
$ git config --global user.email "MyEmail@Zoho.com"
$ git config --global user.name "My Name"
Help
Git has detailed explanations of commands built-in for quick reference.
# Find available commands
$ git help
# List all available commands
$ git help -a
# Search for specific commands in the documentation
# git help <command>
$ git help add
$ git help commit
$ git help init
Status
Show differences between the index file (current workspace) and the commit pointed to by the current head pointer.
# Show branches, untracked files, changes, and other differences
$ git status
# View other uses of git status
$ git help status
Add
Add files to the current workspace. If you do not use git add
to add the files, they will not be included in subsequent commits.
# Add a single file
$ git add HelloWorld.java
# Add a file from a subdirectory
$ git add /path/to/file/HelloWorld.c
# Support regular expressions
$ git add ./*.java