技术交流GitGit常见用法
Mr.阿布白Git 常见用法速查手册
一、初始化项目
1 2
| git init git clone <url>
|
二、配置用户信息(首次使用)
1
| git config --global user.name "你的名字"git config --global user.email "你的邮箱"
|
三、文件操作
1 2 3 4
| git status git add <file> git add . git commit -m "说明"
|
四、查看记录与差异
1 2 3 4
| git log git log --oneline git diff git diff --staged
|
五、分支管理
1 2 3 4 5 6
| git branch # 查看本地分支 git branch <name> # 创建新分支 git checkout <name> # 切换分支 git checkout -b <name> # 创建并切换新分支 git merge <branch> # 合并指定分支到当前分支 git branch -d <branch> # 删除本地分支
|
六、远程操作
1 2 3 4 5 6
| git remote -v # 查看远程仓库地址 git remote add origin <url> # 添加远程仓库 git push -u origin master # 首次推送主分支 git push # 推送代码到远程仓库 git pull # 拉取远程更新并合并 git fetch # 拉取远程更新(不合并)
|
七、回退操作
1 2 3
| git reset --hard HEAD~1 git reset --soft HEAD~1 git revert <commit_id>
|
八、标签管理
1 2 3 4 5
| git tag git tag <v1.0> git push origin <v1.0> 九、忽略文件 创建 .gitignore 文件,例如:
|
九、创建 .gitignore
文件,忽略文件
1 2 3 4 5
| *.log *.pyc __pycache__/ .env node_modules/
|
云端拉取项目 → 修改 → 推送到远程仓库
基本流程
1 2 3 4 5 6 7 8 9 10 11 12 13
| git clone <远程仓库地址> cd <项目目录>
git add <文件名> git commit -m "修改说明" git push origin <当前分支名>
git clone https://github.com/username/repo.git cd repo
git add . git commit -m "修改说明" git push origin main
|
额外常用命令
拉取远程更新并自动合并:git pull
如果多人协作,建议先同步远程改动:git pull origin <分支名> --rebase