Getting Started with Git
Git is a distributed version control and source code management tool. Git can save multiple snapshots of your project to manage the version of the entire project.
Version
What is version control?
A version control system is a system that records changes to one or more files over time.
Centralized Version Control VS Distributed Version Control
- The main function of centralized version control is synchronization, tracking, and backup of files.
- Distributed version control focuses more on sharing changes. Each change has a unique identifier.
- A distributed system has no predefined structure. You can also easily implement a SVN-style centralized system control with git.
Why use Git?
- Work offline
- Makes collaboration with others easier
- Branching is easy
- Merging is easy
- Git system is fast and flexible
Git Architecture
Repository
A series of files, directories, history records, commit records, and head pointers. It can be viewed as a data structure where each source code file has a history attribute.
A Git repository includes a .git directory and its working directory.
.git Directory (Part of the Repository)
The .git directory contains all configurations, logs, branch information, head pointers, etc. A detailed list.
Working Directory (Part of the Repository)
The directory and files in the repository can be seen as your working directory.
Index (in the .git directory)
The index is the staging area in git. It can be considered as a layer that separates your working directory from the Git repository, allowing developers to decide more flexibly what content to add to the repository.
Commit
A git commit is a set of changes or a snapshot of operations on the working directory. For example, if you added 5 files and deleted 2 files, these changes will be written into a commit, which can then be decided whether to push to another repository.
Branch
A branch is essentially a pointer pointing to your last commit. When you commit, this pointer automatically points to the latest commit.
Head Pointer and Head (.git folder's purpose)
A head pointer is a pointer pointing to the current branch. A repository has only one active head pointer at a time.
The head can point to any commit in the repository, and each repository can have multiple heads (HEAD).
Git Commands
Initialize
Create a new Git repository. The configuration and storage information of this repository will be saved in the .git folder.
$ git init
Configure
Change settings. It can be the repository settings, or system or global settings.
# 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 very detailed explanations for commands, which can be quickly referenced.
# Find available commands
$ git help
# Find all available commands
$ git help -a
# Search for a specific command in the documentation
# git help <command>
$ git help add
$ git help commit
$ git help init
Status
Shows the differences between the index file (i.e., current working space) and the commit pointed by the current head pointer.
# Show branches, untracked files, changes, and other differences
$ git status
# View other git status usages
$ git help status
Add
Add files to the current working space. If you don't use git add to add the files, they won't be included in subsequent commits.
# Add a file
$ git add HelloWorld.java
# Add a file in a subdirectory
$ git add /path/to/file/HelloWorld.c
# Supports regular expressions
$ git add ./*.java
Branch
Manage branches. You can use the following commands to add, delete, modify, and query branches.
# View all branches and remote branches
$ git branch -a
# Create a new branch
$ git branch myNewBranch
# Delete a branch
$ git branch -d myBranch
# Rename a branch
# git branch -m <old name> <new name>
$ git branch -m myBranchName myNewBranchName
# Edit the description of a branch
$ git branch myBranchName --edit-description
Checkout
Update the current working space to the one indicated by the index or a specific working space.
# Check out a repository, default updates to the master branch
$ git checkout
# Check out to a specific branch
$ git checkout branchName
# Create a new branch and switch to it, equivalent to "git branch <name>; git checkout <name>"
$ git checkout -b newBranch
Clone
This command copies a repository to another directory and also copies all branches to the new repository. This allows you to submit to a remote branch in the new repository.
# Clone learn-git
$ git clone https://gitcode.com/Gitcode-offical-team/learn-git.git
Commit
Save the changes in the current index as a new commit. This commit includes the user's changes and information.
# Submit with a commit message
$ git commit -m "Added multiplyNumbers() function to HelloWorld.c"
Diff
Display the differences between the current working space and the commit.
# Display differences between the working directory and the index
$ git diff
# Display differences between the index and the last commit
$ git diff --cached
# Display differences between the working directory and the last commit
$ git diff HEAD