Git是一个分布式版本控制工具,主要用于管理开发过程中的源代码文件(Java类、xml文件、html页面等),在软件开发过程中被广泛使用。作用有代码回溯、版本切换、多人协作、远程备份等。
How to Write Better Git Commit Messages – A Step-By-Step Guide
目录
1 概述
1.1 简介
Git是一个分布式版本控制工具,通常用来对软件开发过程中的源代码文件进行管理。通过Git仓库来存储和管理这些文件,Git仓库分为两种:
- 本地仓库:开发人员自己电脑上的Git仓库
- 远程仓库:远程服务器上的Git仓库
- commit:提交,将本地文件和版本信息保存到本地仓库
- push:推送,将本地仓库文件和版本信息上传到远程仓库
- pull:拉取,将远程仓库文件和版本信息下载到本地仓库
1.2 代码托管服务
Git中存在两种类型的仓库,即本地仓库和远程仓库。可以借助互联网上提供的一些代码托管服务来实现,其中比较常用的有GitHub、码云、GitLab等。
- GitHub:最大的面向开源及私有软件项目的托管平台,因为只支持Git作为唯一的版本库格式进行托管,故名GitHub
- Gitee(码云):国内的一个代码托管平台,由于服务器在国内,所以相比于GitHub,码云速度会更快
- GitLab:一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的web服务
- BitBucket:一家源代码托管网站,采用Mercurial和Git作为分布式版本控制系统,同时提供商业计划和免费账户
1.3 基本概念
git的相关基本概念:
- 版本库:.git 隐藏文件夹就是版本库,版本库中存储了很多配置信息、日志信息和文件版本信息等
- 工作区:包含.git文件夹的目录就是工作区,也称为工作目录,主要用于存放开发的代码。工作区中的文件存在两种状态,会随着执行Git的命令发生变化
untracked
:未跟踪(未被纳入版本控制)tracked
:已跟踪(被纳入版本控制)Unmodified
:未修改状态Modified
:已修改状态Staged
:已暂存状态
- 暂存区:.git 文件夹中有很多文件,其中有一个index文件就是暂存区,也可以叫做stage。暂存区是一个临时保存修改文件的地方
2 常用命令
2.1 全局参数配置
当安装Git后首先要做的事情是设置用户名称和email地址。这非常重要,因为每次Git提交都会使用该用户信息。
# 设置用户信息(其中user.name和user.email可任意设置)
git config --global user.name "akira37"
git config --global user.email "example@foxmail.com"
# 查看配置信息
git config --list
2.2 本地初始化Git仓库
要使用Git对代码进行版本控制,首先需要获得Git仓库。获取Git仓库通常有两种方式:在本地初始化一个Git仓库
、从远程仓库克隆。
在本地初始化Git仓库的命令如下:
- 在任意目录下创建一个空目录作为本地Git仓库
- 进入这个目录中,打开命令行/终端
- 执行命令
git init
若在当前目录中看到.git
文件夹(此文件夹为隐藏文件夹)则说明Git仓库创建成功。
或通过Git提供的命令从远程仓库进行克隆,将远程仓库克隆到本地:
git clone 远程Git仓库地址
2.3 添加文件到仓库
在仓库目录下放入文件,例如新建一个test.txt
文件,再使用git add test.txt
命令将文件添加到缓存区,最后使用git commit -m "提交描述"
把文件提交到仓库。
# 添加指定文件或文件夹到缓存区,文件名需带后缀
git add <文件或文件夹名称>
git add <文件或文件夹名称1> <文件或文件夹名称n> # 可添加同时多个文件
# 将全部文件添加到缓存区
git add .
# 将文件从缓存区提交至仓库
git commit -m "提交描述"
在提交代码时输入清晰的说明有利于版本管理,建议搭配如下关键字编写提交描述:
# 新建(add)
# 删除(rm)
# 更新(update)
# 改动(change)
# 实现
# 发布
# 修复
2.4 版本管理
在Git中用HEAD
表示当前版本,上一个版本表示为HEAD^
,上上一个版本则为HEAD^^
。可用“~
+回退数量”简写,例如回退上100个版本可表示为HEAD~100
# 显示新增/删除/被改动等的文件
git status
# 查看版本记录
git log # 显示版本号、提交时间等信息
# 记录变更历史
git reflog
# 回退到上一个版本
$ git reset --hard HEAD^ # 或HEAD^、HEAD~100等
# 跳转到指定版本
git reset --hard <版本号前几位>
2.5 远程仓库操作
要使本机能关联远程仓库,首次操作时需要SSH验证,方法如下:
- 创建SSH Key:在用户根目录下执行如下命令,生成私钥
id_rsa
和公钥id_rsa.pub
# 创建SSH Key
ssh-keygen -t rsa -C "邮箱地址"
- 登陆GitHub,右上角选择
头像 > settings > SSH and GPG keys >Add SSH Key
,在key中粘贴公钥id_rsa.pub
的内容
远程仓库操作:
# 查看远程仓库
git remote [-v] # -v:查看详细信息
# 添加远程仓库
git remote add <short-name> <repository-url> # 例如git remote add origin git@github.com:hyperplasma/Ultimate-Solutions.git
# 从远程仓库拉取
git pull <short-name> <branch-name>
# 推送到远程仓库
git push <short-name> <branch-name>
注:若当前本地仓库不是从远程仓库克隆,而是本地创建的仓库,并且仓库中存在文件,此时再从远程仓库拉取文件的时候会报错(fatal:refusing to merge unrelated histories)
解决此问题可以在git pull
命令后加入参数--allow-unrelated-histories
2.6 分支操作
分支是Git使用过程中非常重要的概念。使用分支意味着可以把工作从开发主线上分离开来,以免影响开发主线。同一个仓库可以有多个分支,各个分支相互独立,互不干扰。
通过git init
命令创建本地仓库时默认会创建一个master分支。
分支的常用命令如下:
# 查看分支
# -r:查看所有远程分支
# -a:查看所有本地分支
git branch
# 创建分支
git branch <branch-name>
# 切换分支
git checkout <branch-name>
# 推送至远程仓库分支
git push <short-name> <branch-name>
# 合并本地仓库的分支
git merge <branch-name>
# 合并远程仓库的分支
git merge <repository-name>/<branch-name>
# 删除分支
git branch -d <branch-name>
(当前分支用*
表示)
同时修改本地分支名和对应的远程分支名(修改前要确保本地分支的代码是最新的,并且修改后不会影响到其他人的代码)步骤如下:
- 修改本地分支名
git branch -m oldBranchName newBranchName
- 删除远程分支
git push origin :oldBranchName # 或 git push origin --delete oldBranchName
- 改名后的本地分支推送到远程
git push --set-upstream origin newBranchName
2.7 标签操作
标签指Git中某个分支某个特定时间点的状态。通过标签可以很方便的切换到标记时的状态。比较有代表性的是人们会使用这个功能来标记发布结点(v1.0、v1.2等)。
常用的标签指令如下:
# 列出已有的标签
git tag
# 创建标签
git tag <tag-name>
# 将标签推送至远程仓库
git push <short-name> <repository-name>
# 检出标签
git checkout -b <branch-name> <tag-name>
2.8 帮助命令
对命令不清楚时,可使用git help
命令查看git命令介绍:
git help
$ git help
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
These are common Git commands used in various situations:
start a working area (see also: git help tutorial)
clone Clone a repository into a new directory
init Create an empty Git repository or reinitialize an existing one
work on the current change (see also: git help everyday)
add Add file contents to the index
mv Move or rename a file, a directory, or a symlink
reset Reset current HEAD to the specified state
rm Remove files from the working tree and from the index
examine the history and state (see also: git help revisions)
bisect Use binary search to find the commit that introduced a bug
grep Print lines matching a pattern
log Show commit logs
show Show various types of objects
status Show the working tree status
grow, mark and tweak your common history
branch List, create, or delete branches
checkout Switch branches or restore working tree files
commit Record changes to the repository
diff Show changes between commits, commit and working tree, etc
merge Join two or more development histories together
rebase Reapply commits on top of another base tip
tag Create, list, delete or verify a tag object signed with GPG
collaborate (see also: git help workflows)
fetch Download objects and refs from another repository
pull Fetch from and integrate with another repository or a local branch
push Update remote refs along with associated objects
'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
3 IDEA集成操作
3.1 配置本地仓库
- 创建本地仓库
- 文件右键操作
- 可设置 .gitignore 来排除指定文件或文件夹
- 提交
- 查看日志
3.2 远程仓库操作
- 推送(初次推送需定义远端)
- 克隆
- 各种操作