环境

  • Windows 10
  • git bash

git配置

1
2
3
4
5
6
7
8
# 查询git版本
git --version
# 查看当前电脑的git用户配置
git config --list
# 设置git全局用户名
git config --global user.name "DyedBamboo"
# 配置邮箱
git config --global user.email "xxxxxxx@qq.com"

实例操作

本地仓库操作

随便找个位置新建文件夹,命名GitLearing,进入GitLearning右键"Git Bash Here"

https://cdn.jsdelivr.net/gh/DyedBamboo/DyedBamboo.github.io-blog-img/git_learning/20220729205258.png

把这个文件夹作为工作区,就可以在这个文件夹下放自己的代码了

例如我这里新建了两个文件:helloworld.cpp,helloworld.py

运行 git status查看当前工作区状态,显示有两个文件helloworld.cpp和helloworld.py未提交到缓存区。然后运行git add .把当前目录下的所有文件添加到缓存区

https://cdn.jsdelivr.net/gh/DyedBamboo/DyedBamboo.github.io-blog-img/git_learning/20220729210844.png

接着运行git commit -m "添加了Cpp和Python的HelloWord"提交到仓库

https://cdn.jsdelivr.net/gh/DyedBamboo/DyedBamboo.github.io-blog-img/git_learning/20220729211236.png

更改代码文件内容后重新提交git add ., git commit -m "修改了两个文件",然后运行git log查看提交历史

https://cdn.jsdelivr.net/gh/DyedBamboo/DyedBamboo.github.io-blog-img/git_learning/20220729214918.png

尝试提交历史回退,运行git reset --hard 5683fa

https://cdn.jsdelivr.net/gh/DyedBamboo/DyedBamboo.github.io-blog-img/git_learning/20220729220610.png

分支操作

尝试分支操作:

  • 查看当前的分支:git branch
  • 创建新的分支dev:git branch dev
  • 切换到新的分支dev:git check out dev

https://cdn.jsdelivr.net/gh/DyedBamboo/DyedBamboo.github.io-blog-img/git_learning/20220729221216.png

  • 合并dev分支到master里:git merge dev

https://cdn.jsdelivr.net/gh/DyedBamboo/DyedBamboo.github.io-blog-img/git_learning/20220729222153.png

  • 删除dev分支:git branch -d dev

  • 创建并切换main分支:git checkout -b main

总结

https://cdn.jsdelivr.net/gh/DyedBamboo/DyedBamboo.github.io-blog-img/git_learning/20220729230500.png

视频教程