文章目录
- 安装与配置
- 环境配置
- 基本概念
- 版本库、工作区、暂存区
- git下文件的状态
- .gitignore
- 仓库
- 暂存区
- 版本库
- 远程仓库
- 分支
- 标签
- 命令拾遗
安装与配置
Mac上使用图形化安装工具、brew、xcode自带的都可以。
检查
git --version
环境配置
配置文件为 *gitconfig
作用域有三种:
- 系统 system (/etc/gitconfig)
- 全局(用户)global (在 ~/.gitconfig )
- 项目 (在仓库的 /project/.git/config)
- 查看
git config --list
- 配置
git config --global user.name 'jsy'
git config --global user.email '153xxxxx@qq.com'
基本概念
版本库、工作区、暂存区
- 版本库
就是那个 .git
里面有
jisongyang@SongyangJi-MacBookAir .git % ls
HEAD description info refs
config hooks objects
- 工作区
我们能看到的那个包含.git的文件夹, 比如下面的那个 repo 文件夹就是
jisongyang@SongyangJi-MacBookAir .git % cd ..
jisongyang@SongyangJi-MacBookAir repo % pwd
/Users/jisongyang/learn-git-repo/repo
- 暂存区
临时保存的修改的文件的地方
git下文件的状态
- untracked 未被git管理
- tracked
- unmodified 未修改
- modified 已修改
- staged 暂存
查看状态
git status
# 未跟踪
git status -s
举例
jisongyang@SongyangJi-MacBookAir repo % clear
# 未跟踪
jisongyang@SongyangJi-MacBookAir repo % git status -s
?? a.txt
jisongyang@SongyangJi-MacBookAir repo % git add a.txt
# 已跟踪
jisongyang@SongyangJi-MacBookAir repo % git status -s
A a.txt
jisongyang@SongyangJi-MacBookAir repo % echo hello >> a.txt
# 已修改
jisongyang@SongyangJi-MacBookAir repo % git status -s
AM a.txt
jisongyang@SongyangJi-MacBookAir repo % git reset a.txt
jisongyang@SongyangJi-MacBookAir repo % git status -s
?? a.txt
jisongyang@SongyangJi-MacBookAir repo % git commit a.txt
[master (root-commit) 5273424] init a.txt
1 file changed, 2 insertions(+)
create mode 100644 a.txt
jisongyang@SongyangJi-MacBookAir repo % git status -s
# a.txt 在状态里不可见了
jisongyang@SongyangJi-MacBookAir repo %
.gitignore
当有文件不需要git管理的时候,使用这个文件就很有必要了。
比如日志、项目编译出来的文件、IDEA的本地信息等等,就不需要git管理。
# 文件名固定
touch .gitignore
看例子就好
# 忽略所有 *.class
*.class
# 但是不忽略Hello.class
!Hello.class
# 忽略当前目录下的TODO文件夹
/TODO
# 忽略 target 下的所有文件
target/
# 忽略 doc/*.txt的所有文件夹
doc/*.txt
## 忽略 doc及其所有子目录下所有的 *.txt
doc/**/*.txt
仓库
- 在本地初始化仓库
git init
创建完会多一个 .init文件(默认不可见)
- 从远程仓库克隆
git clone repo
暂存区
从工作区到暂存区
git add ...
暂存区的目录树会被重写,被 master 分支指向的目录树所替换,但是工作区不受影响
git reset HEAD
直接从暂存区删除文件,工作区则不做出改变
git rm --cached -f <file>
版本库
- 从暂存区到版本库
git commit -m 'message'
远程仓库
查看已连接的远程仓库
git remote
git remote -v
git remote show origin
添加远程仓库
git remote add [shortname] url
从远程仓库克隆到本地
git clone repo
移除无效的远程仓库(只是移除关联关系,不会对远程仓库有影响)
git remote rm origin
抓取(fetch)
从远程仓库将最新版本获取到本地仓库
git fetch
拉取(pull)
从远程仓库将最新版本获取到本地仓库,并merge
(可能报 merge unrelated histories)
git pull [romote-name] [branch-name]
推送(push)
git push [romote-name] [branch-name]
分支
master分支是git自动创建的,但没并没有什么特殊之处。
查看分支
# 列出所有本地分支
git branch
# 列出所有远程分支
git branch -r
# 列出远程和本地分支
git branch -a
创建分支(在原先分支下创建分支,内容会初始共享)
git branch branch-name
举例(*master表示当前在master分支下)
jisongyang@SongyangJi-MacBookAir repo1 % git branch b1
jisongyang@SongyangJi-MacBookAir repo1 % git branch
b1
* master
切换分支(这个时候就自动把目标分支在本地仓库里的文件换到工作区里)
git checkout b1
将本地分支推送至远程仓库
git push origin b1
分支合并
(可能会冲突)
这里的冲突需要正确理解(这里的冲突的就像是乐观锁的加版本号一样,事实上在svn里确实有全局版本号,不允许脏写发生)
# 把b1分支合并到当前的master分支
git merge b1
删除分支
# 删除的是本地的分支,对远程的分支无关
git branch -d branch-name
# 强制删除(git的保护,以防误删)
git branch -D branch-name
jisongyang@SongyangJi-MacBookAir repo1 % git branch -d b1
error: The branch 'b1' is not fully merged.
If you are sure you want to delete it, run 'git branch -D b1'.
删除远程分支
git push origin -d branchname
标签
Git给历史中的某次提交打上标签,以示重要。
标签指的是某个分支某个特定时间节点的状态。(就像是打了一份快照)
查看已有标签
# 列出已有标签
git tag
git show tag
创建标签
git tag tagname
将标签推送到远程仓库:
# 推送某个标签
git push origin vxx
# 推送所有不在remote的标签
git push origin --tags
Git-基础-打标签
命令拾遗
# 查看工作区和暂存区的文件
git ls-files
删除
git rm file
git rm
和rm
的区别很多,网上讲的也都是其中的一个方面,不细讲了,最好自己试试。
查看日志
git log