Git Command Reference

Basic operations

# Initialize repository
git init

# Clone remote repository
git clone <repo_url>

# View current status
git status

# Add all modifications to the temporary storage area
git add .

# Submit to local warehouse
git commit -m "submission common"

# View Submission History
git log --oneline

# View file modification differences
git diff

Branch Management

# Create a new branch
git branch <branch_name>

# switch branch
git checkout <branch_name>

# Create and switch branches
git checkout -b <new_branch>

# Merge branches into the current branch
git merge <branch_name>

# Delete local branch
git branch -d <branch_name>

# Force deletion of unconsolidated branches
git branch -D <branch_name>

# View all branches (including remote)
git branch -a

Remote warehouse operations

# Add remote warehouse
git remote add <remote_name> <repo_url>

#Push local branch to remote
git push -u <remote_name> <branch_name>

#Mandatory push (use with caution!)
git push -f

#Pull remote updates
git pull <remote_name> <branch_name>

#Obtain remote branches but do not merge them
git fetch

#Delete remote branch
git push <remote_name> --delete <branch_name>

Cancel operation

#Revoke workspace modifications
git checkout -- <file>

#Revoke the temporary storage file
git reset HEAD <file>

#Revise the last submission
git commit --amend

#Fallback to specified submission (keep modifications)
git reset --soft <commit_id>

#Completely revert to the specified submission (use with caution!)
git reset --hard <commit_id>

#Restore mistakenly deleted files
git checkout <commit_id> -- <file_path>

Logs and Queries

#Graphic submission history
git log --graph --all

#Submit by Author Search
git log --author="name"

#Search for submitted content
git log -S "keyword"

#Display the modification history of a certain file
git blame <file>

Tag Management

#Create tags
git tag <tag_name>

#Create annotated labels
Git tag-a v1.0-m "Version Description"

#Push tags to remote location
git push --tags

#Delete local tags
git tag -d <tag_name>

#Delete remote tag
git push origin :refs/tags/<tag_name>

Advanced Operations

#Store current modifications
git stash

#Apply recent storage
git stash pop

#Interactive transformation (modified last 3 submissions)
git rebase -i HEAD~3

#Binary search for problem submission
git bisect start
Git bisect bad # marks the current submission as an error
Git bisect good<id># tag is known to be submitted normally

#Clean up untracked files
git clean -fd

Configuration related

#Global username configuration
git config --global user.name "Your Name"

#Global email configuration
git config --global user.email " [email protected] "

#View all configurations
git config --list

#Set aliases (such as simplifying logs)
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'"

Submodule management

#Add submodule
git submodule add <repo_url> <path>

#Initialize submodule
git submodule init

#Update submodule
git submodule update

Practical Tips

#Ignore file permission changes
git config core.fileMode false

#Generate. gitignore template
curl https://gitignore.io/api/ <Language/Tools>

#View warehouse size
git count-objects -vH

#Clone specified branch (shallow clone)
git clone --branch <branch_name> --depth 1 <repo_url>

Precautions:

  1. Before performing a force operation, be sure to confirm the scope of impact.
  2. Avoid directly modifying important branches (such as main/master).
  3. We recommend using the SSH protocol for remote operation.
  4. Regularly perform git garbage collection to optimize the repository.