about git
git是版本管理软件,在这个软件的数据库(仓库)中会记录代码随时间的变化,我们可以在仓库中看见谁在什么时间对代码做了哪些修改,也可以方便地回退到某个代码状态。
git可以在命令行使用
IDE中也有继承git插件
GUI git:gitkraken/sourcetree
安装git
- 下载地址:Git - Downloads
按照网页提示安装即可
- 查看当前环境中是否有git
git --version
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
解决方法:xcode-select --install
配置git
要设置的内容:name/email/default editor/line ending
可三个层次上设置以上内容:
system:针对所有使用该电脑的用户
global:针对当前用户的所有仓库
local:我文件夹中的仓库
针对不同的层次有不同的设置。
git config --global user.name "huxixi"
git config --global user.email hytuaing@163.com
git config --global core.editor "code --wait" 设置vscode为默认编辑器(不设置的话默认是vim)
【设置在命令行输入code时打开vscode:打开vscode--》command+shift+p--》输入shell command--》选择install到path选项】
刚才的所有配置都保存在配置文件中,后续可以修改配置文件,打开配置文件使用:
git config --global -e
不同平台的行尾结束符会有不同,为避免错误,需要设置行尾结束符
git config --global core.autocrlf input
(windows 设置成true,mac/linux设置成input)
git config --help
可以看到所有可配置的参数
使用git
git init
创建一个文件夹,在该目录下初始化本地git仓库,生成.git
git项目组成
- 工作目录,你可以在本地的工作目录增删改你的文件
- 提交区:这里保存你修改的内容清单
- 仓库:永久存储你的修改及各个版本
git工作流
查看当前工作目录和staging area的状态 【git status】
git status
未提交到staging area的文件会被标红
提交本地修改到staging area 【git add 文件列表】
git add *.txt
提交staging area到仓库 【git commit [-m comment]】
直接git commit会打开默认编辑器,编辑文件的第一行为简短提交注释,第三行为长注释,保存关闭即可完成带注释的提交。
如果足够自信,也可以不做git add,直接git commit -am comment到仓库。
查看staging area中的文件
git ls-files
删除本地和staging area的文件
git rm filenames
重命名或移动文件
- 方式一需要分两步,分别提交旧名文件和新命名的文件
git add ori-filename
git add new-filename
- 方式二同步操作本地文件和staging area
git mv ori-filename new filename
设置不跟踪变化提交的文件或目录
echo untracked-file >.gitignore
git add .gitignore
git commit -m "ignore files"
前提是被设置的文件或目录之前没有提交过,否则设置无效,如果之前提交过,现在向忽略,需要将该文件仅从staging area中删除
git rm --cached filename
git commit -m "delete file"
查看staging are中跟踪的文件和本地文件的区别
git diff
设置查看diff的工具:
git config --global diff.tool vscode
git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"
git config --global -e
修改cmd为code --wait --diff $LOCAL $REMOTE
使用git difftool(用法和参数和git diff一样)就可以在vscode中查看区别
查看提交历史
git log
git log --oneline
git log --online --reverse
查看提交历史记录的具体内容
git show HEAD~number 查看距离head number距离的提交版本信息
git show HEAD~number:filename 查看该提交版本中指定文件的信息
git ls-tree HEAD~number 查看指定版本的文件结构
回滚修改过的内容
git restore --staged filename 将未提交的staging area中的变化回滚,本地文件不变
git restore . 将未提交的staging area和本地本文件的变化都回滚到上次提交的状态
清楚所有没有被追踪不在staging area中的文件
git clean
确定要清除:git clean -f
从仓库恢复之前的删除的文件
git restore --source=HEAD~1 filename