“有了远程仓库,妈妈再也不用担心我的硬盘了。”——Git点读机
概述
工作区有一个隐藏目录.git,这个不算工作区,而是Git的暂存区和版本库。
前面讲了我们把文件往Git版本库里添加的时候,是分两步执行的:
第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;
第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支。
git commit只负责把暂存区的修改提交.因为我们创建Git版本库时,Git自动为我们创建了唯一一个master分支,所以,现在,git commit就是往master分支上提交更改。
配置用户信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
git config --global user.name "Your Name" git config --global user.email "email@example.com" git config --global color.ui true
git config --global alias.st status git config --global alias.co checkout git config --global alias.ci commit git config --global alias.br branch git config --global alias.unstage 'reset HEAD' git config --global alias.last 'log -1' git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
$ cat .git/config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true ignorecase = true precomposeunicode = true [remote "origin"] url = git@github.com:michaelliao/learngit.git fetch = +refs/heads/*:refs/remotes/origin/* [branch "master"] remote = origin merge = refs/heads/master [alias] last = log -1
$ cat .gitconfig [alias] co = checkout ci = commit br = branch st = status [user] name = Your Name email = your@email.com
|
常用操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| git status
git diff readme.txt
git log --pretty=oneline
git reset --hard HEAD^
git reset --hard (commit-hash-id)
git reflog
git checkout -- fileName
git checkout
git reset HEAD fileName
|
分支管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
git checkout -b dev
git branch -d dev
git merge dev
git merge --no-ff -m "merge with no-ff" dev
git log --graph --pretty=oneline --abbrev-commit
git stash
git stash list
git stash apply [stash@{0}]
git stash drop [stash@{0}]
git stash pop
git remote -v
git checkout -b dev origin/dev
git push origin/dev
git branch --set-upstream dev origin/dev
git pull
git tag v1.0 [-m "version 0.1 released" 3628164]
git show v0.9
git tag -d v0.1
git push origin v1.0
git push origin :refs/tags/v0.9
|
管理策略
master分支应该是非常稳定的,也就是仅用来发布新版本,平时不能在上面干活;
干活都在dev分支上,也就是说,dev分支是不稳定的,到某个时候,比如1.0版本发布时,再把dev分支合并到master上;
如果是多人协同开发,则每个人开发时都需要创建自己的分支,开发者完成一个任务时,只合并到dev分支上,–no-ff参数可以看出合并历史;
一些命名规范:
1 2 3 4 5 6 7 8 9 10
|
git checkout -b issue-101
git checkout -b feature-vulcan
git branch -D feature-vulcan
|
一些远程同步的规范:
- master分支是主分支,因此要时刻与远程同步;
- dev分支是开发分支,团队所有成员都需要在上面工作,所以也需要与远程同步;
- bug分支只用于在本地修复bug,就没必要推到远程了,除非老板要看看你每周到底修复了几个bug;
- feature分支是否推到远程,取决于你是否和你的小伙伴合作在上面开发。
多人协作的工作模式:
- 首先,可以试图用git push origin branch-name推送自己的修改;
- 如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并;
- 如果合并有冲突,则解决冲突,并在本地提交;
- 没有冲突或者解决掉冲突后,再用git push origin branch-name推送就能成功!
复制当前Git仓库创建一个完全独立的Git仓库,完成了之后,可以发起pull request来为官方Git库贡献代码
添加忽略
根目录下添加.gitignore文件,可以参考或历史项目的此文件,在项目开始时就要添加:
- 忽略操作系统自动生成的文件,比如缩略图等;
- 忽略编译生成的中间文件、可执行文件等,也就是如果一个文件是通过另一个文件自动生成的,那自动生成的文件就没必要进版本库,比如Java编译产生的.class文件;
- 忽略你自己的带有敏感信息的配置文件,比如存放口令的配置文件。
相关命令:
1 2 3 4
| git check-ignore -v App.class
git add -f App.class
|
参考资料
廖雪峰的Git教程
https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000