Git 搭建总结
c10于2024.12.13编写,可能存在时效性,仅供参考
初始化仓库
在远程创建一个仓库:
- 登录 Git 平台(如 GitHub、GitLab),创建一个新的仓库。
在本地初始化仓库:
1
git init
- 初始化一个空的 Git 仓库。
关联远程仓库:
1
git remote add origin <远程仓库URL>
- 使用
origin标识远程仓库地址。
- 使用
添加
.gitignore文件:- 将不需要纳入版本控制的文件添加到
.gitignore中,比如编译文件:1
2
3*.o
*.class
*.log
- 将不需要纳入版本控制的文件添加到
提交更改:
1
2git add .
git commit -m "初始化仓库"推送到远程仓库:
1
git push -u origin main
常用命令
文件跟踪管理
- 取消跟踪某个后缀名文件:
1
git rm --cached *.xxx
- 作用:从版本控制中移除此类文件,但保留本地文件。
- 设置代理:
1
2
3
4
5
6
7
8
9
10
11
12
13git config --global http.proxy "http://127.0.0.1:8080"
```
## 分支操作
### 合并分支
```mermaid
graph TD;
A[Main: A---B---C---M]
subgraph Feature
D[D---E]
end
A -->|合并| M
- 合并命令:
1
2git checkout main
git merge feature
变基分支
1 | graph TD; |
- 变基命令:
1
2git checkout feature
git rebase main
分支管理
- 查看分支:
1
git branch
- 创建新分支:
1
git branch <branch-name>
- 切换分支:
1
git checkout <branch-name>
- 创建并切换分支:
1
git checkout -b <branch-name>
日志查看
- 查看提交日志:
1
git log
- 简洁模式:
1
git log --oneline
回滚操作
- 回退到上一提交:
1
git reset --hard HEAD~1
- 撤销文件修改:
1
git checkout -- <file-name>
远程仓库
- 查看远程仓库地址:
1
git remote -v
- 更新远程分支列表:
1
git fetch
小结
掌握 Git 的常见操作和命令可以极大提升团队协作和版本管理的效率。熟练使用分支操作、变基、合并,以及远程仓库管理是关键步骤。
All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.

