0
点赞
收藏
分享

微信扫一扫

掌握Git了解版本控制——让开发更简单

像小强一样活着 2022-01-12 阅读 54
gitgithubssh

版本控制

常用版本控制软件:

Git

SVN

版本控制分类

  1. 本地版本控制:适合个人使用;
  2. 集中版本控制:所有版本数据都储存在服务器上。
  3. 分布式版本控制**(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)就可以分

为四个工作区域。文件在这四个区域之间的转换关系如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rIUgvOck-1642001345132)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220111203914795.png)]

  • Workspace:工作区,就是你平时存放项目代码的地方

  • Index / Stage:暂存区,用于临时存放你的改动,事实上它只是一个文件,保存即将提交到文件列表信息

  • Repository:仓库区(或本地仓库),就是安全存放数据的位置,这里面有你提交到所有版本的数据。其中HEAD指向最新放入仓库的版本

  • Remote:远程仓库,托管代码的服务器,可以简单的认为是你项目组中的一台电脑用于远程数据交换

  1. 在工作目录中添加、修改文件;
  2. 将需要进行版本管理的文件放入暂存区域;
  3. 将暂存区域的文件提交到git仓库;

git add .:将文件放入暂存区;

git commit:将文件放入仓库区;

git项目搭建

  1. 初始化项目

git init:初始化git,创建.git文件

git clone:从远程仓库中克隆一个git项目。

  1. 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公钥里。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-s20XMzrs-1642001345134)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220112112927872.png)]

之后创建一个远程仓库:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tnGAnle2-1642001345134)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220112113356913.png)]

克隆到本地!

IDEA操作git

创建一个IDEA项目,在git克隆下来的项目的同一个文件夹中,将克隆下来的项目全部拷贝到IDEA项目中。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5G1tvMDT-1642001345134)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220112114905692.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lzGMnsdD-1642001345135)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220112114935178.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aaGyEirY-1642001345135)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220112114953930.png)]

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	#同步到对应的分支
举报

相关推荐

0 条评论