本文主要记录 git 使用。submodule
正常使用
origin 为代号,可以随意取
1 2 3 4
| # https 方式 git remote add origin https://github.com/love1angel/testgit.git # ssh 方式 git remote add origin git@github.com:love1angel/testgit.git
|
submodule 使用
创建
1 2 3
| mkdir 3rdparty cd 3rdparty git submodule add https://github.com/google/googletest.git
|
下载
1 2
| git submodule init git submodule update --recursive
|
查看状态
1 2 3 4 5 6 7 8 9 10 11
| # 正常下载好的状态 git submodule status 33af80a883ddc33d9c0fac0a5b4578301efb18de 3rdparty/googletest (release-1.8.0-3471-g33af80a8)
# 如果没有下好,带 - git submodule status -2954cb8d879886403d55343f941ae7d0216e0f6b 3rdparty/googletest
# 有更新,带 + git submodule status +33af80a883ddc33d9c0fac0a5b4578301efb18de 3rdparty/googletest (release-1.8.0-3471-g33af80a8)
|
更新
1 2 3 4 5 6 7 8 9 10
| # 更新总仓库目前的分支,引用 git remote update
# 更新到总仓库 git submodule update --recursive
# 更新子仓库到最新的提交 git submodule update --remote # 更新单个仓库 git submodule update --remote 3rdparty/googletest
|
before no use
git使用
目的:Linus为了管理Linux源码用C语言开发的分布式版本控制系统
类型:分布式
分布式 VS 集中式
- 集中式:CVS和SVN等
- 分布式:git等
- 集中式通常也有一台充当中央服务器的电脑,主要是为了方便交换大家的修改
集中式的缺点
- 版本库集中存放在中央服务器,需要联网获得最新的版本
- 只有一个中央服务器可能会宕机,导致所有人无法工作
- 中央服务器的版本库一旦出现存在问题,难以补救,不安全
let’s start:)
准备
- download git
- 设置本机的user信息,本机所有repository都会使用这个配置,对单独的仓库也可以单独设置,通过git config命令查看参数选项
1 2
| $ git config --global user.name "your name" $ git config --global user.email "email@example.com"
|
- 查看本地global参数
1
| $ git config --list --global
|
!!! 修改global参数后以前的提交还是以前的user参数,???git repository如何识别user呢
- 初始化一个仓库,会在当前目录添加一个隐藏目录(.git)
!!! git 只能跟踪文本文件的改动,图片、视频这些二进制文件,没法跟踪文件的变化
changes not staged for commit
- untracked changes -> staged changes(update what will be committed)
- discard changes in working directory(将工作区的状态回到暂存区的状态)
1
| $ git checkout -- <files>
|
要提交的变更
- staged changes -> untracked changes(将暂存区会退到到head所指的分支,并把暂存区的修改回退到工作区)
1
| $ git reset HEAD <files>
|
- 提交
查看repository的状态
- 查看过去日志
- 查看当前状态
版本控制
- 回到过去(之前的修改保存到untracked changes,暂存区也回退)
1 2
| $ git reset HEAD^ $ git reset HEAD~1
|
- 强制回退,可以通过commitID在回去,丢弃所有目前的修改,不建议用
1 2
| $ git reset --hard HEAD^ $ git reset --hard 7f1f
|
- 查看命令历史,回到未来
管理修改
- 比较工作区和HEAD指针所指向的分支
远程库
1
| $ ssh-keygen -t rsa -C "youremail@example.com"
|
- 本地仓库关联远程仓库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| $ ssh -T git@github.com
$ git remote add origin https://github.com/love1angel/work.git
$ git branch -a
$ git branch -M main
$ git push -u origin main
$ git push origin main
$ git remote -v
$ git remote rm origin
$ git clone
|
分支管理
创建合并分支
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| $ git checkout -b dev
$ git branch dev $ git checkout dev
$ git branch
$ git merge dev
$ git branch -d dev
|
冲突解决
1 2 3 4 5 6 7 8 9 10 11 12
| $ git merge fearture1
$ git add $ git commit
$ git log --graph
$ git merge --no-ff -m "merge with no-ff" dev
|
BUG分支,feature分支
1 2 3 4 5 6 7 8 9 10 11 12 13
| $ git stash
$ git stash list
$ git stash apply stash@{0}
$ git stash pop stash@{0}
$ git cherry-pick <commit>
|
多人开发
1 2 3 4 5 6 7 8
| $ git checkout -b dev origin/dev
$ git branch --set-upstream-to=origin/<branch-name> <branch-name>
$ git rebase
|
tag
tag就是某个commit,不过commitID不好记住,且无意义
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| $ git tag v1.0 $ git tag $ git tag v0.9 f52c633 $ git show <tagname>
$ git tag -a v0.1 -m "version 0.1 released" 1094adb
$ git tag -d v0.1 $ git push origin v1.0 $ git push origin --tags
$ git tag -d v0.9 $ git push origin :refs/tags/v0.9
|
git个性化定制
1
| $ git config --global color.ui true
|
创建.gitignore
gitignore配置
1 2 3 4 5
| $ git check-ignore -v App.class .gitignore:3:*.class App.class
!.gitignore !App.class
|