MCP Git Server
A Model Context Protocol (MCP) server for executing git push operations with flexible branch mapping and pending changes management.
đĄ Best Practices
Configure tool prompts to regulate AI's git operation behavior:
- Register an account:
- đ¨đŗ China: https://openai.hlwidc.com:28080/user
- đ Global: https://www.zhineng.dev/user/
- Add your own provider and model configuration
- Generate an API key
- Add to the key's tool prompts:
Tool: get_push
Prompt: Unless I explicitly confirm, do not use native git tools for any operations. Only when I explicitly say to use push pr should you use the git_push and pr tools from MCP tools. At any other time, git-related operations are not allowed.
Tool: save_changes
Prompt: There is a save_changes tool in the tools. You should call this tool to save modification records after completing modifications.
Effect: AI only executes git operations when you explicitly request it, while automatically saving records after each modification.
Features
- Execute
git pushwith configurable remote, source branch, and target branch - Push history tracking and duplicate push prevention
- Operation logging for debugging and monitoring
- Pending changes management and review functionality
- Multi-language support (English, Chinese)
- Environment variable configuration for flexibility
Installation
npm install -g @liangshanli/mcp-server-git
Environment Variables
Required Variables
PROJECT_PATH: Absolute path to the git repositoryLOCAL_BRANCH: Local branch name to pushREMOTE_BRANCH: Remote branch name to push to
Optional Variables
REMOTE_NAME: Remote repository name (default: "origin")PULL_SOURCE_BRANCH: Source branch forgit_pull(default: same asREMOTE_BRANCH)GIT_PUSH_FLAGS: Additional git push flags (default: "--progress")TOOL_PREFIX: MCP tool name prefix (default: "")REPO_NAME: Repository identifier for logging and identificationLANGUAGE: Message language ("en", "zh", "zh-CN", "zh-TW") (default: "en")MCP_LOG_DIR: Log file directoryMCP_PUSH_HISTORY_FILE: Push history filename (default: "push-history.json")HTTP_PROXY/HTTPS_PROXY/SOCKS_PROXY: Proxy settings
Usage
1. Set Environment Variables
export PROJECT_PATH="/path/to/your/git/repository"
export LOCAL_BRANCH="main"
export REMOTE_BRANCH="main"
export REMOTE_NAME="origin"
export REPO_NAME="my-project"
export TOOL_PREFIX="myproject"
2. Editor Configuration
Create or update .cursor/mcp.json or .vscode/mcp.json in project root:
{
"mcpServers": {
"git": {
"command": "npx",
"args": ["@liangshanli/mcp-server-git"],
"env": {
"PROJECT_PATH": "/path/to/your/git/repository",
"LOCAL_BRANCH": "main",
"REMOTE_BRANCH": "main",
"REMOTE_NAME": "origin",
"REPO_NAME": "my-project",
"TOOL_PREFIX": ""
}
}
}
}
3. Start Server
npm start
# or
mcp-server-git
# or (with verification)
npm run start-managed
Multi-Project Instance Support
You can configure multiple Git MCP server instances in your editor to manage different repositories simultaneously.
Cursor Editor Configuration
{
"mcpServers": {
"git-web-app": {
"command": "npx",
"args": ["@liangshanli/mcp-server-git"],
"env": {
"PROJECT_PATH": "D:/projects/web-app",
"LOCAL_BRANCH": "main",
"REMOTE_BRANCH": "main",
"REPO_NAME": "web-app",
"TOOL_PREFIX": "web"
}
},
"git-api-service": {
"command": "npx",
"args": ["@liangshanli/mcp-server-git"],
"env": {
"PROJECT_PATH": "D:/projects/api-service",
"LOCAL_BRANCH": "develop",
"REMOTE_BRANCH": "develop",
"REPO_NAME": "api-service",
"TOOL_PREFIX": "api"
}
}
}
}
VS Code Editor Configuration
{
"servers": {
"git-project-a": {
"command": "npx",
"args": ["@liangshanli/mcp-server-git"],
"env": {
"PROJECT_PATH": "/path/to/project-a",
"LOCAL_BRANCH": "main",
"REMOTE_BRANCH": "main",
"REPO_NAME": "project-a",
"TOOL_PREFIX": "a"
}
},
"git-project-b": {
"command": "npx",
"args": ["@liangshanli/mcp-server-git"],
"env": {
"PROJECT_PATH": "/path/to/project-b",
"LOCAL_BRANCH": "main",
"REMOTE_BRANCH": "main",
"REPO_NAME": "project-b",
"TOOL_PREFIX": "b"
}
}
}
}
Benefits of Multi-Instance Integration:
- Tool Isolation: Each instance has independent prefixed tools (e.g.,
web_git_push,api_git_push) - Log Isolation: Logs stored in different directories
- Independent Config: Each repository can configure different branch mappings and project paths
MCP Tools
git_push
Execute git push with a commit message. Automatically adds and commits changes before pushing.
Important: You must call get_pending_changes first to review changes before using this tool.
Operations performed:
- Auto-execute
git add .to stage all changes - Auto-execute
git commit -m "message"to commit changes - Execute
git pushto push to remote - Clear all pending changes and reset review status
{
"name": "git_push",
"arguments": {
"message": "Update project files"
}
}
get_pending_changes
Get and review pending changes before push. Must be called before git_push to enable pushing.
{
"name": "get_pending_changes",
"arguments": {
"limit": 1000
}
}
save_changes
Save pending changes before push. Records modified files and change content for pre-push review.
{
"name": "save_changes",
"arguments": {
"files": ["src/main.js", "src/utils.js"],
"content": "Fix bug in user authentication"
}
}
get_push_history
Get the last 5 push history records to check for duplicates.
get_operation_logs
Get operation logs for debugging.
{
"name": "get_operation_logs",
"arguments": {
"limit": 50
}
}
git_status
Show status of working directory and staging area.
git_diff
Show differences between working directory and HEAD or staging area.
{
"name": "git_diff",
"arguments": {
"staged": true
}
}
git_add
Add file contents to staging area.
{
"name": "git_add",
"arguments": {
"files": ["src/main.js", "src/utils.js"]
}
}
git_log
Show commit history.
{
"name": "git_log",
"arguments": {
"limit": 5,
"oneline": true
}
}
git_pull
Execute git pull from configured remote and source branch.
đĄ Best Practices and Usage Suggestions
To fully leverage MCP Git Server, follow these "strong constraints" instructions when collaborating with AI:
1. Atomic Recording (save_changes)
Instruction suggestion: "Please call save_changes tool immediately after completing each independent small feature or fixing a bug. You need to explicitly list modified files and briefly describe your modification logic in one or two sentences. Do not accumulate large changes without recording."
Value: This ensures AI's memory fragments are solidified in real-time, preventing loss of initial intent during complex refactoring.
2. Modular Push (git_push)
Instruction suggestion: "When we complete all development and self-testing for the current feature module, please push by calling git_push. Before pushing, you must use get_pending_changes to fully read and summarize all saved records from our conversation, generating a structured Commit Message covering all changes."
Value: Making "summarizing history records" a mandatory prerequisite for push completely eliminates "goldfish brain" commits.
3. Periodic Review
If the session is very long (e.g., lasting several hours), occasionally ask AI to call get_pending_changes for a mid-session summary to ensure stored records perfectly match the current actual code state.
Required Workflow
- Make code modifications
- Call
save_changesto record modification content - Call
get_pending_changesto review and mark modifications as reviewed - Call
git_pushto auto-add, commit and push changes - For subsequent pushes, repeat steps 3-4 (review status resets after each push attempt)
Validation
The server performs the following validations on startup:
- Check required environment variables
- Verify
PROJECT_PATHpath exists - Ensure
PROJECT_PATHis a valid git repository (contains.gitdirectory)