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
Grep
Quickly search in the repository.
Optional configuration:
# Thanks to Travis Jeffery for the following usage:
# Display line numbers in search results
$ git config --global grep.lineNumber true
# Make search results more readable
$ git config --global alias.g "grep --break --heading --line-number"
# Search for variableName in all java files
$ git grep 'variableName' -- '*.java'
# Search for all lines containing "arrayListName" and "add" or "remove"
$ git grep -e 'arrayListName' --and \( -e add -e remove \)
Log
Display all commits in this repository.
# Display all commits
$ git log
# Display several commit messages
$ git log -n 10
# Display only merge commits
$ git log --merges
Merge
Merging means combining external commits into your branch.
# Merge another branch into the current branch
$ git merge branchName
# Create a new merged commit during merging
$ git merge --no-ff branchName
Mv
Rename or move a file.
# Rename
$ git mv HelloWorld.c HelloNewWorld.c
# Move
$ git mv HelloWorld.c ./new/path/HelloWorld.c
# Force rename or move
# This file already exists, and will overwrite it
$ git mv -f myFile existingFile
Pull
Merge from a remote repository into the current branch.
# Update the repository from the master branch of the remote origin
# git pull <remote> <branch>
$ git pull origin master
Push
Update the remote repository.
# Update the local branch to the master branch of the remote origin
# git push <remote> <branch>
# git push is equivalent to git push origin master
$ git push origin master
Rebase (Use with caution)
Apply all commit histories of one branch onto another branch. Do not use rebase on an already public remote branch.
# Apply experimentBranch onto master
# git rebase <basebranch> <topicbranch>
$ git rebase master experimentBranch
Reset (Use with caution)
Reset the current head pointer to a specific state. This allows you to undo merges, pulls, commits, adds, etc. This is a powerful command, but you must be clear about its consequences when using it.
# Restore the staging area to the state of the last commit, without changing the current working directory
$ git reset
# Restore the staging area to the state of the last commit, overwriting the current working directory
$ git reset --hard
# Restore the current branch to a certain commit, without changing the current working directory
# All changes in the working directory still exist
$ git reset 31f2bb1
# Restore the current branch to a certain commit, overwriting the current working directory
# And delete all uncommitted changes and all commits after the specified commit
$ git reset --hard 31f2bb1
Rm
Opposite of add, removes a file from the working space.
# Remove HelloWorld.c
$ git rm HelloWorld.c
# Remove a file in a subdirectory
$ git rm /pather/to/the/file/HelloWorld.c
For more Git manual learning, welcome to visit: How to start practicing Git commands on GitCode
Thanks to the open source project learnxinyminutes-docs