初识git
安装git
sudo scoop install git -g
初始化仓库
git init
添加文件
git add readme.txt #将文件添加到暂存区
提交到仓库
git commit -m "commit message" #进行提交
版本控制
查看日志
git log
注:
如果嫌输出的信息太多,可以添加
git log --pretty=oneline
版本回退
git reset --hard HEAD #回退到当前版本
git reset --hard HEAD^^ #回退到两个前版本(一个^代表上一次版本)
git reset --hard HEAD~100 #回退到100个前版本
git reset --hard 版本id #回退到id对应版本,在没有歧义的前提下版本号只需要写前几位
版本回退日志
git reflog
查看差异
显示暂存区和上一次提交(commit)的差异:
$ git diff --cached [file]
git diff --cached .\test.txt
# 或
$ git diff --staged [file]
git diff --staged .\test.txt
显示两次提交之间的差异:
git diff [first-branch]...[second-branch]
git diff HEAD^ HEAD^^
撤销修改
在添加到暂存区之前
git checkout -- file
git checkout -- .\test.txt
添加到暂存区之后
git reset HEAD [file]
git reset HEAD test.txt
删除文件
shell 中删除文件,删除后可以在git status中看到
rm [file]
git 中删除,删除后即在版本库中看不到,即删除git status中相关信息
git rm [file]
分支管理
创建分支
git branch dev
git checkout dev
#或是一条命令
git checkout -b dev
分支管理指令
git branch #查看分支
git branch <name> #创建分支
git checkout <name> #切换分支
git switch <name> #切换分支
git checkout -b <name> #创建+切换分支
git switch -c <name> #创建+切换分支
git merge <name> #合并某分支到当前分支
git branch -d <name> #删除分支
分支管理日志
git log --graph #打印合并图
bug分支
将当前分支暂存起来,等以后恢复工作
git stash
commit 合并
git rebase -i [startpoint] [endpoint]
标签管理
- 添加标签:
git tag <name>
- 查看所有标签
git tag
- 为标签添加说明
git tag -a [name] -m [message]
- 查看指定标签说明:
git show <tagname>
- 删除标签:
git tag -d [tagname]
- 将标签添加到远程仓库
- 推动一个标签:
git push origin <tagname>
- 推送所有标签:
git push origin --tags
- 推动一个标签: