1. Repository Initialization and Configuration
# Initialize local repository
git init
# Clone remote repository
git clone <remote repository URL>
# Configure user information
git config --global user.name "your name"
git config --global user.email "your email"
2. Remote warehouse operations
# Add a remote repository:
`git remote add origin <remote repository URL>
` # View remote repositories:
`git remote -v`
# Modify the remote repository URL:
`git remote set-url origin <new remote repository URL>
` # Delete a remote repository:
`git remote remove origin`
3. Branch Management
# View all branches
git branch -a
# Create a new branch
git branch <branch name>
# Switch branches
git checkout <branch name>
# Or (for newer Git versions)
git switch <branch name>
# Create and switch to a new branch
git checkout -b <branch name>
git switch -c <branch name>
# Delete a branch
git branch -d <branch name> # Safely delete
git branch -D <branch name> # Force delete
# Rename a branch
git branch -m <new branch name>
4. Code Submission
# Check status
git status
# View changes
git diff
# Add a file to the staging area
git add <filename> # Add a specific file
git add . # Add all files
git add -A # Add all changes
# Commit to the local repository
git commit -m "commit message"
# Modify the last commit message
git commit --amend
5. Push and Pull
# Push to remote repository
git push origin <branch name>
# First push and establish association
git push -u origin <branch name>
# Pull remote updates
git pull origin <branch name>
# Fetch remote updates but do not merge
git fetch origin
6. Version rollback and undo
# View commit history
git log
git log --oneline # Concise version
# Revert to a specific commit
git reset --hard <commit_id>
# Revert to the previous commit
git reset --hard HEAD^
# Undo changes in the working directory
git checkout -- <filename>
# Undo files in the staging area
git reset HEAD <filename>
# Force push to remote (use with caution!)
git push -f origin <branch name>
7. Merging and Conflict Resolution
# Merge branches:
`git merge <branch name>
` # Rebase: `
git rebase <branch name>
` # Resolve conflicts and continue merging:
`git add .` `
git commit -m "Resolve conflicts"
` # Cancel merge
: `git merge --abort`
`git rebase --abort`
8. Storage and Labelling
# Stall the current work (
git stash)
# View the stash list
(git stash list)
# Restore a stash
(git stash pop)
# Create a tag
(git tag <tag name>)
# Push to remote
(git push origin --tags)
Submission and Push Explained
The essence of commit
Commit is the operation of saving to the local repository . It:
- Permanently save changes in the staging area to the local Git repository.
- Generate a unique commit ID (commit hash).
- It only affects the local machine and will not be uploaded to the remote server.
- You can submit multiple times and then push the results all at once.
The role of push
Push is the operation of uploading local commits to a remote repository . It:
- Synchronize commits from the local repository to the remote repository.
- Make sure your team members can see your changes.
- Network connection required
Regarding the issue of missing code
Key takeaway: Committing only locally ensures your code won’t be lost!
Detailed explanation:
After the first submission :
git add .
git commit -m "First commit"
# At this point, the code has been securely saved in the local repository
Continue modifying the code :
# Modify some files ..
git add .
git commit -m "Second submission"
Push all submissions at once
git push origin master
# This will push all submissions for the first and second time
Workflow Example
# Day 1 work:
git add .
git commit -m "Complete user login functionality"
# No push, go home immediately
# Day 2 work:
git add .
git commit -m "Complete user registration functionality"
# Now push both days' worth of work at once:
git push origin master
# The remote repository will receive two commits
Best Practice Recommendations
- Frequent submissions : It is recommended to submit after each small feature is completed.
- Meaningful commit information : Clearly describe the content of this commit.
- Regular push notifications : Although they won’t be lost, it’s recommended to send a push notification once a day before the end of the workday to prevent unexpected local data loss.
- Fetch before pushing : to avoid conflicts
git pull origin master
git push origin master
Special Circumstances Handling
If you just want a clean commit history, you can use:
# Merge multiple submissions into one
git reset --soft HEAD~2 # Roll back two submissions but keep the changes
git commit -m "Merge function development"
git push origin master
Git is a distributed version control system. The local repository is a complete copy, and code committed to the local repository is safe!
Where is the local repository?
The local repository is your project folder , specifically:
Visible portion:
你的项目文件夹/
├── src/ # Your source code
├── pom.xml # configuration file
├── README.md # Instruction Manual
└── ... # Other project documents
Hidden section (Git repository):
你的项目文件夹/
├── .git/ # Local Git repository
│ ├── objects/ # Store all file versions
│ ├── refs/ # Store branches and label references
│ └── config # repository configuration
└── Your project files ..
.git Folder visibility
We usually refer to the project folder as the working directory, while the .git folder is the Git repository, which is hidden by default.
In the file system, files and folders that begin with a dot are hidden by default. This is to prevent accidental user actions from corrupting the repository.
.git The folder is hidden by default , but it can be viewed in different ways:
Windows system:
- File Explorer : View → Check “Hidden items”
- Command line :
dir /aShow all files (including hidden files)
macOS/Linux systems:
- Finder : Cmd+Shift+. to show hidden files.
- Command line :
ls -laShow all files
View in IDEA:
- Project panel :
.gitFolders are not displayed by default. - To view: Settings → Editor → File Types → Ignored Files and Folders
.git(unchecked) .
.git Folder internal structure
.git/
├── HEAD # Current branch
├── config # Repository configuration (remote repository URL, etc.)
├── description # Repository description
├── hooks/ # Git hook scripts
├── info/ # Exclusion modes, etc.
├── objects/ # All file version data
├── pack/ # Compressed object package
└── ... # All committed file contents
├── refs/ # Branch and tag references
├── heads/ # Local branch
├── tags/ # Tags
└── remotes/ # Remote branch references
└── index # Staging area index
Complete .gitignore configuration of a Spring Boot project
# Files ignored by default:
/shelf/
/workspace.xml
# HTTP client requests based on the editor:
/httpRequests/
# Files ignored for local data source storage:
/dataSources/
/dataSources.local.xml
# ========== The following are Spring Boot project rules that need to be added ==========
# Compilation output:
/target/
*/target/
**/target/
!/target/.gitkeep # If you need to keep the .gitkeep file in an empty target directory
# Build output:
/build/
*/build/
**/build/
# Log files:
*.log
logs/
*.logs
# Temporary files:
*.tmp
*.temp
*.cache
# System files:
.DS_Store
Thumbs.db
*.swp
*.swo
# IDE files:
.idea/
*.iws
*.iml
*.ipr
.vscode/
*.swp
*.swo
# Maven wrapper
: !.mvn/wrapper/maven-wrapper.jar
# Application configuration files (optional, depending on the situation)
# /src/main/resources/application-*.properties
# /src/main/resources/application-*.yml
# Unit test reports
/test-output/
/reports/
*.eml
# Package files
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# Dependency downloads (if any)
.dependency/
# Database files (if a local database exists)
*.db
*.sqlite
# Environment variable files (usually contain sensitive information)
.env
.env.local
.env.production
# Key files
*.key
*.p12
*.keystore
Vue3 project .gitignore core configuration
# Dependency directory[citation:1][citation:5][citation:7]
node_modules/
.pnp/
.pnp.js
# Build output directory[citation:3][citation:5]
dist/
dist-ssr/
build/
out/
release/
deploy/
# Temporary files and cache directory
.cache/
.tmp
.temp
# Log Files[citation:1][citation:3][citation:7]
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
# Local environment variable file[citation:1][citation:3]
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
# IDE and Editor Configuration Files[citation:1][citation:3][citation:4]
.idea/
.vscode/
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
# Operating system generates files[citation:1][citation:3]
.DS_Store
Thumbs.db
# Test coverage report[citation:3]
coverage/
.nyc_output/
# Lockfiles(Choose whether to ignore according to team agreement)
# package-lock.json
# yarn.lock
# pnpm-lock.yaml