跳到主要内容

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"

For more Git settings

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

Branch

Manage branches. You can perform CRUD operations on branches using the following commands.

# List 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 workspace to match the index or a specific workspace.

# Check out the repository, defaulting to updating 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, including copying all branches to the new repository. This allows you to commit to remote branches from the new repository.

# Clone learn-git
$ git clone https://gitcode.com/Gitcode-official-team/learn-git.git

Commit

Save the current index changes as a new commit, which includes the user's changes and information.

# Commit with a message
$ git commit -m "Added multiplyNumbers() function to HelloWorld.c"

Diff

Show differences between the current workspace and the commit.

# Show differences between the working directory and the index
$ git diff

# Show differences between the index and the most recent commit
$ git diff --cached

# Show differences between the working directory and the most recent commit
$ git diff HEAD

Grep

Quickly search within the repository.

Optional configurations:

# 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 lines containing "arrayListName" and either "add" or "remove"
$ git grep -e 'arrayListName' --and \( -e add -e remove \)

Log

Display all commits in this repository.

# Show all commits
$ git log

# Show the last 10 commits
$ git log -n 10

# Show only merge commits
$ git log --merges

Merge

Merging involves integrating external commits into your own 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 be overwritten
$ git mv -f myFile existingFile

Pull

Merge from a remote repository into the current branch.

# Update the repository from the remote origin's master branch
# git pull <remote> <branch>
$ git pull origin master

Push

Push updates to the remote repository.

# Push the local branch to the remote origin's master branch
# 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 from one branch onto another branch. Do not use rebase on a publicly exposed remote branch.

# 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 can undo merges, pulls, commits, adds, etc. This is a powerful command, but be aware of its consequences when using it.

# Restore the staging area to the state before the last commit without changing the working directory
$ git reset

# Restore the staging area to the state before the last commit and overwrite the working directory
$ git reset --hard

# Restore the current branch to a specific commit without changing the working directory
# All changes in the working directory still exist
$ git reset 31f2bb1

# Restore the current branch to a specific commit and overwrite the working directory
# Also delete all unstaged changes and all commits after the specified commit
$ git reset --hard 31f2bb1

Rm

Opposite of add, remove a file from the workspace.

# Remove HelloWorld.c
$ git rm HelloWorld.c

# Remove a file from a subdirectory
$ git rm /path/to/the/file/HelloWorld.c

For more Git manual learning, visit: xjh22222228/git-manual

Thanks to the open-source project learnxinyminutes-docs