以下是 Git 项目创建、上传更新和下载更新的完整流程
一、项目创建与初始上传
1. 本地项目初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| mkdir project-name cd project-name
git init
git add .
git commit -m "Initial commit"
|
2. 关联远程仓库并首次推送
1 2 3 4 5 6 7 8
|
git remote add origin https://github.com/用户名/仓库名.git
git push -u origin main
git push -u origin master
|
二、日常上传更新(本地→远程)
完整上传流程
1 2 3 4 5 6 7 8 9 10 11 12 13
| git status
git add . git add 文件名
git commit -m "提交描述"
git push origin 分支名 git push
|
常用提交方式
1 2 3 4 5 6 7 8 9 10
| git commit -am "提交描述"
git add . git commit --amend
git log git log --oneline
|
三、下载更新(远程→本地)
拉取远程更新
1 2 3 4 5 6 7 8 9 10
| git pull origin 分支名
git pull
git fetch origin git diff origin/分支名 git merge origin/分支名
|
克隆已有项目
1 2 3 4 5 6 7 8
| git clone https://github.com/用户名/仓库名.git
git clone -b 分支名 https://github.com/用户名/仓库名.git
cd 仓库名
|
四、分支管理(推荐工作流)
基础分支操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| git branch git branch -r git branch -a
git branch 新分支名 git checkout 分支名 git checkout -b 新分支名
git push origin 分支名
git branch -d 分支名 git push origin --delete 分支名
|
协同开发工作流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| git pull origin main
git checkout -b feature/功能名称
git add . git commit -m "功能描述" git push origin feature/功能名称
git checkout main git pull origin main
|
五、常见问题解决
冲突解决
1 2 3 4 5 6
| git pull
git add 冲突文件 git commit -m "解决冲突" git push
|
撤销操作
1 2 3 4 5 6 7 8
| git checkout -- 文件名
git reset HEAD 文件名
git reset --hard commit_id
|
六、配置简化(可选)
设置别名
1 2 3 4 5 6 7
| git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit git config --global alias.st status
|
常用配置
1 2 3 4 5 6
| git config --global user.name "你的名字" git config --global user.email "你的邮箱"
git config --global credential.helper store
|
七、快速参考命令表
| 操作 |
命令 |
| 初始化 |
git init |
| 克隆 |
git clone <url> |
| 添加文件 |
git add <file> |
| 提交 |
git commit -m "msg" |
| 推送 |
git push origin <branch> |
| 拉取 |
git pull origin <branch> |
| 查看状态 |
git status |
| 查看历史 |
git log |
| 创建分支 |
git branch <name> |
| 切换分支 |
git checkout <name> |
这样你就掌握了Git的基本使用流程。建议在实际项目中多练习,熟悉后会越来越得心应手。