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 backing up 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 Subversion (SVN)-style centralized control system with Git.
Why Use Git?
- Work offline
- Makes collaboration with others easier
- Branching is easy
- Merging is easy
- Git is fast and flexible
Git Architecture
Repository
A collection of files, directories, history, commit logs, and head pointers. It can be considered 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 the configuration, logs, branch information, head pointers, etc.
Working Directory (Part of the Repository)
The directory and files in the repository can be seen as your working directory.
Index (.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 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. This commit can then be decided whether to push to another repository.
Branch
A branch is essentially a pointer to your last commit. When you commit, this pointer automatically points to the latest commit.
Head Pointer and HEAD (the role of the .git folder)
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 a repository can have multiple heads (HEAD).
Git Commands
Initialize
Create a new Git repository. The configuration and storage information of this repository are saved in the .git folder.
$ git init
Configure
Change settings. It can be repository settings, or system-wide 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 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
Displays the differences between the index file (i.e., the current working space) and the commit pointed to by the current head.
# 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 working space. If you do not use git add to add the files, they will not 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 perform add, delete, modify, and query operations on branches using the following commands.
# 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 identified by the index or a specific working space.
# Check out a repository, by default it 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 current index changes 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 the difference between the working directory and the index
$ git diff
# Display the difference between the index and the latest commit
$ git diff --cached
# Display the difference between the working directory and the latest commit
$ git diff HEAD
Grep
Quickly search within the repository.
Optional configuration:
# Thanks to Travis Jeffery for the following usage:
# Display line numbers in search results
$ git config --global grep.lineNumber true
# Improve readability of search results
$ 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 the repository.
# Display all commits
$ git log
# Display several commit messages
$ git log -n 10
# Only display merge commits
$ git log --merges
Merge
Merging is the process of combining external commits into your branch.
# Merge another branch into the current branch
$ git merge branchName
# Create a new merge commit when 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
$ 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 the commit history of one branch to another branch. Do not use rebase on a branch that has been publicly shared.
# Apply experimentBranch to 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 understand its consequences before 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, remove 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, please visit: How to start practicing Git commands on GitCode
Thank you for the open source project learnxinyminutes-docs