版本控制
常用版本控制软件:
Git
SVN
版本控制分类
- 本地版本控制:适合个人使用;
- 集中版本控制:所有版本数据都储存在服务器上。
- 分布式版本控制**(Git)**:所有版本信息同步到本地的每个用户,可以离线本地提交,联网时push即可
git命令
git环境
usage: git config [<options>]
Config file location
--global use global config file
--system use system config file
--local use repository config file
--worktree use per-worktree config file
-f, --file <file> use given config file
--blob <blob-id> read config from given blob object
Action
--get get value: name [value-pattern]
--get-all get all values: key [value-pattern]
--get-regexp get values for regexp: name-regex [value-pattern]
--get-urlmatch get value specific for the URL: section[.var] URL
--replace-all replace all matching variables: name value [value-pattern]
--add add a new variable: name value
--unset remove a variable: name [value-pattern]
--unset-all remove all matches: name [value-pattern]
--rename-section rename section: old-name new-name
--remove-section remove a section: name
-l, --list list all
--fixed-value use string equality when comparing values to 'value-pattern'
-e, --edit open an editor
--get-color find the color configured: slot [default]
--get-colorbool find the color setting: slot [stdout-is-tty]
Type
-t, --type <> value is given this type
--bool value is "true" or "false"
--int value is decimal number
--bool-or-int value is --bool or --int
--bool-or-str value is --bool or string
--path value is a path (file or directory name)
--expiry-date value is an expiry date
Other
-z, --null terminate values with NUL byte
--name-only show variable names only
--includes respect include directives on lookup
--show-origin show origin of config (file, standard input, blob, command line)
--show-scope show scope of config (worktree, local, global, system, command)
--default <value> with --get, use default value when missing entry
git config --list #显示git目录下的配置
git config --global --list #显示本地git配置目录
git config --global user.name 用户名 #设置变量中的用户名(git必备)
git config --global user.email 邮箱 #设置变量中的邮箱
git原理
Git本地有三个工作区域:工作目录(Working Directory)、暂存区(Stage/Index)、资源库(Repository或Git Directory)。如果在加上远程的git仓库(Remote Directory)就可以分
为四个工作区域。文件在这四个区域之间的转换关系如下:
-
Workspace:工作区,就是你平时存放项目代码的地方
-
Index / Stage:暂存区,用于临时存放你的改动,事实上它只是一个文件,保存即将提交到文件列表信息
-
Repository:仓库区(或本地仓库),就是安全存放数据的位置,这里面有你提交到所有版本的数据。其中HEAD指向最新放入仓库的版本
-
Remote:远程仓库,托管代码的服务器,可以简单的认为是你项目组中的一台电脑用于远程数据交换
- 在工作目录中添加、修改文件;
- 将需要进行版本管理的文件放入暂存区域;
- 将暂存区域的文件提交到git仓库;
git add .
:将文件放入暂存区;
git commit
:将文件放入仓库区;
git项目搭建
- 初始化项目
git init
:初始化git,创建.git文件
git clone
:从远程仓库中克隆一个git项目。
- git文件操作
Untracked: 未跟踪, 此文件在文件夹中, 但并没有加入到git库, 不参与版本控制. 通过 git add状态变为 Staged;
Unmodify: 文件已经入库, 未修改, 即版本库中的文件快照内容与文件夹中完全一致. 这种类型的文件有两种去处, 如果它被修改, 而变为 Modified . 如果使用 git rm 移出版本
库, 则成为Untracked 文件;
Modified: 文件已修改, 仅仅是修改, 并没有进行其他的操作. 这个文件也有两个去处, 通过 git add 可进入暂存 staged 状态, 使用 git checkout 则丢弃修改过, 返回到 unmodify 状态,这个 git checkout 即从库中取出文件, 覆盖当前修改 !
Staged: 暂存状态. 执行 git commit 则将修改同步到库中, 这时库中的文件和本地文件又变为一致, 文件为 Unmodify 状态. 执行 git reset HEAD filename 取消暂存, 文件状态
为Modified;
# 查看文件状态
git status
git add . #添加所有文件到暂存区
git commit -m "message"
gitignore文件:提交时忽略项目中的哪些文件,配置信息。
#为注释
*.txt #忽略所有 .txt结尾的文件
!lib.txt #但lib.txt除外
/temp #仅忽略项目根目录下的TODO文件,不包括其它目录temp
build/ #忽略build/目录下的所有文件
doc/*.txt #会忽略 doc/notes.txt 但不包括 doc/server/arch.txt
设置ssh公钥 :
找到C:\Users\Administrator.ssh路径下的.ssh文件夹,点击Git bash
输入命令ssh-keygen -t rsa
将public公钥里的文件打开,复制添加到码云的ssh公钥里。
之后创建一个远程仓库:
克隆到本地!
IDEA操作git
创建一个IDEA项目,在git克隆下来的项目的同一个文件夹中,将克隆下来的项目全部拷贝到IDEA项目中。
git分支
# 列出所有本地分支
$ git branch
# 列出所有远程分支
$ git branch -r
# 列出所有本地分支和远程分支
$ git branch -a
# 新建一个分支,但依然停留在当前分支
$ git branch [branch-name]
# 新建一个分支,并切换到该分支
$ git checkout -b [branch]
# 新建一个分支,指向指定commit
$ git branch [branch] [commit]
# 新建一个分支,与指定的远程分支建立追踪关系
$ git branch --track [branch] [remote-branch]
# 切换到指定分支,并更新工作区
$ git checkout [branch-name]
# 切换到上一个分支
$ git checkout -
# 建立追踪关系,在现有分支与指定的远程分支之间
$ git branch --set-upstream [branch] [remote-branch]
# 合并指定分支到当前分支
$ git merge [branch]
# 选择一个commit,合并进当前分支
$ git cherry-pick [commit]
# 删除分支
$ git branch -d [branch-name]
# 删除远程分支
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]
git fetch于git merge
$ git fetch origin #从远程拉取
$ git merge origin/master #同步到对应的分支