安装git

windows下安装git,直接下载安装即可。
macos下安装git,需要打开终端,输入以下命令:

1
xcode-select --install

命令输入完成后会跳出对话框,点击安装,安装完成后,打开终端,输入以下命令:

1
git --version

配置git

配置git需要输入以下命令:

1
2
3
4
git config --global user.name "你的用户名"
git config --global user.email "你的邮箱"

clear & git init #初始化git仓库

新建几个档案,保存好。

1
2
3
4
5
6
7
8
9
10
11
12
git status #查看状态
git add . #添加所有档案
git commit -m "你的提交信息" #提交到本地仓库

git log --oneline #查看提交记录

git diff #查看修改

git checkout -b 分支名 #创建分支

git reset --hard HEAD^ #回退到上一次提交

github

github是一个代码托管平台,可以托管git仓库。

1
2
3
4
git remote add origin https://github.com/你的用户名/你的仓库名.git #添加远程仓库
git branch -M main #设置主分支
git push -u origin main #推送到远程仓库

协同工作:

1
2
git clone https://github.com/你的用户名/你的仓库名.git #克隆远程仓库

后面如果有新增文档只需要在编辑好文档后,输入以下命令即可:

1
2
3
git add . #添加所有档案
git commit -m "你的提交信息" #提交到本地仓库
git push #推送到远程仓库

协同工作的只需要输入

1
git pull #拉取远程仓库

如果我需要创建一个分支

1
git checkout -b 分支名

创建一个分支后,我需要切换到这个分支

1
git checkout 分支名