GIT操作
GIT(分布式版本控制系统)
Git是一款免费、开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。
GIT流程
st=>start: 开始
e=>end: 结束
clone=>operation: git clone url
pullA=>operation: git pull origin develop:develop
checkoutA=>operation: git checkout -b feature/A
code=>operation: 编写代码/修改代码
add=>operation: git add
commit=>operation: git commit
checkoutB=>operation: git checkout develop
pullB=>operation: git pull origin develop:develop
merge=>operation: git merge feature/A
conflict=>operation: 如果有冲突,解决冲突
push=>operation: git push origin develop:develop
st->clone->pullA->checkoutA->code->add->commit->checkoutB->pullB->merge->conflict->push->e
- git clone
git clone
可以从远程服务器的git库中克隆代码到本地的当前目录
- git pull
当本地已经存在相应的git库时,可以使用 git pull
从服务器同步最新的库到本地
示例:同步服务器的develop分支到本地的develop分支。
git pull origin develop:develop
- git checkout
git checkout
切换当前分支
示例:切换当前分支到 a 分支
git checkout a
git checkout -b
创建分支,并切换到创建的分支
示例:创建分支 c ,并切换到c分支
git checkout -b c
- git add
git add
提交当前修改到本地缓存
可以使用
git add (文件名称)
提交单一文件 也可以使用git add -A
或是git add --all
提交全部修改的文件
- git commit
git commit
提交本地缓存修改的内容到本地git库
可以使用
git commit -M "提交内容的说明"
,填写提交说明 建议直接使用git commit
,然后弹出界面填写提交说明
提交commit时,必须给出完整扼要的提交信息,下面是一个范本。
Present-tense summary under 50 characters
* More information about commit (under 72 characters).
* More information about commit (under 72 characters).
第一行是不超过50个字的提要,然后空一行,罗列出改动原因、主要变动、以及需要注意的问题。
- git merge
git merge
将指定分支的内容,合并到当前分支
示例:当前分支为develop分支,我需要将分支c的内容合并到develop分支。
git merge c
在做merge和push的时候,会出现代码冲突(conflict)的问题,git会自动合并可以合并的冲突,无法合并的冲突,需要手工进行冲突合并
- git push
git push
将本地库推送提交到服务器的git库上
示例:将本地的develop分支同步到服务器的develop分支上。
git push origin develop:develop
其它常用的Git命令
- git status
git status
可以查看当前分支的状态,如代码是否被修改、新增的代码等
- git branch
git branch
查看git库分支
git branch
查看本地库有哪些分支git branch -A
查看远程库有哪些分支
- git log
git log
查看git库日志
- git reset
git reset
回退到git库的历史提交commit
- git config
git config
可以修改git的设置信息
其它
-
git库文件夹下的
.git
文件夹中是当前的git库的信息 -
.git/config
文件中存储的是当前git库的设置信息
可以通过修改config文件,修改git库的url指向 可以通过修改config文件,修改git库的分支对应 可以通过修改config文件,修改当前git库使用的用户名和邮箱
- 设置git提交ignore列表
代码中很多内容是不希望提交到服务器库中的,如target、idea个人配置信息、jar包等,可以通过设置ignore列表进行提交检查屏蔽
.git/info/exclude
文件中可以设置ignore列表 示例:ignore项目中的target文件夹和.idea文件夹
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
target/
.idea/
*.iml
图形化GIT
虽然强烈推荐使用命令行进行git操作,但是也有图形化git客户端供大家使用,满足非技术人员的需要。
使用git客户端进行版本管理,同样需要遵循git的流程
GUI Clients
官方推荐图形客户端,罗列的包括了Mac、Windows、Linux下的客户端,免费及付费的都有。Tortoise Git
Windows平台下的开源Git图形界面。GitX(L)
Mac OS X下的开源Git客户端。SourceTree
Windows和Mac下的免费Git或Mecurial界面。git-cola
一款开源Git界面。