(1)Git存储
Git使用40个16进制SHA-1 Hash来唯一标识对象
(2)Git对象类型
blob
tree
commit
tag
tag-----commit-----tree-----blob
(3)Git存库
获取Git仓库的2种方式
进入Git仓库文件夹
(3.1)git init 子仓库名称
(3.2)git clone
新建仓库 |
git init helloworld
会在helloworld下建立
.git文件夹
|
新建仓库 |
进入文件夹 git init
zhuyinbai@zhuyb
MINGW64 /e/git_repo
$ cd second/ zhuyinbai@zhuyb MINGW64 /e/git_repo/second $ git init Initialized empty Git repository in E:/git_repo/second/.git/ |
新建裸仓库
|
zhuyinbai@zhuyb MINGW64 /e/git_repo
$ git init --bare first Initialized empty Git repository in E:/git_repo/first/ |
获取仓库 | 需要一个远程裸仓库的地址 git clone zhuyinbai@zhuyb MINGW64 /e/git_repo $ git clone first/ first_clone Cloning into 'first_clone'... warning: You appear to have cloned an empty repository. done. 如果不加自定义名称,则使用远程仓库的名称 |
|
|
|
|
(4)Git工作区域
working directory
|
工作区,日常编辑代码的地方 |
staging area
|
暂存区
|
history repository |
历史仓库,commit
|
|
|
工作区-----暂存区-----历史
工作区-----暂存区
git add
暂存区-----历史记录
git commit
历史记录-----工作区
如何查看暂存区和工作区的区别
git status
暂存区删除文件
git rm
工作区重命名文件(移动文件)-----暂存区
git mv
工作区-----暂存区(忽略文件)
gitignore
(5)练习
git init first
|
新建仓库first
|
cd first
|
进入仓库 |
touch a
touch b
|
新建文件a,b
|
$ git add a b
|
将文件从工作区提交到暂存区 |
git status |
查看暂存区和工作区的区别 有2个新文件 |
zhuyinbai@zhuyb MINGW64 /e/git_repo/first (master)
$ git commit -m "this zhuyinbai's first commit" [master (root-commit) b16a2cd] this zhuyinbai's first commit 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 a create mode 100644 b |
将暂存区的文件提交到历史记录 并且增加相关的描述 |
vim a
|
编辑a文件
|
git status
|
查看暂存区和工作区的区别
一个文件被修改
Changes not staged for commit:
|
git add a
|
将文件添加到暂存区 Changes to be committed: 注意上面一个状态是还没有添加到暂存区 下面的是 待提交 |
git rm a
|
删除工作区以及暂存区下的a
|
git reset HEAD a
|
还原一下
Unstaged changes after reset:
复位状态
|
git checkout a | 将文件从暂存区检索到工作区域 |
$ git rm --cached a | 仅仅删除暂存区下的文件 |
git mv a c |
重名为C
renamed: a -> c
git mv 其实是一组操作
mv a c
git add a c
|
git rm --cached a git rm --cached b |
在暂存区中删除a
在暂存区中删除b
|
git add
-A
|
将工作区下的
所有文件都提交到暂存区
|
vim file.a vim file.b |
新建file.a
新建file.b
|
vim .gitignore |
新建gitignore文件
|
*.[ab]
|
忽略所有以a或者b结尾的文件
【.gitignore】文件中
|
! \! / ** |
这个文件不能被忽略
【.gitignore】文件中
转译
目录
所有的目录
|
git add .gitignore |
将
.gitignore文件也添加到暂存区
|
$ git commit -m "zhuyinbai add .gitignore file"
|
将
.gitignore文件也添加到历史记录
|
|
|
Git常用操作命令 2
1) 远程仓库相关命令
检出仓库:$ git clone git://github.com/jquery/jquery.git
查看远程仓库:$ git remote -v
添加远程仓库:$ git remote add [name] [url]
删除远程仓库:$ git remote rm [name]
拉取远程仓库:$ git pull [remoteName] [localBranchName]
推送远程仓库:$ git push [remoteName] [localBranchName]
2)分支(branch)操作相关命令
查看本地分支:$ git branch
查看远程分支:$ git branch -r
创建本地分支:$ git branch [name] ----注意新分支创建后不会自动切换为当前分支
切换分支:$ git checkout [name]
创建新分支并立即切换到新分支:$ git checkout -b [name]
删除分支:$ git branch -d [name] ---- -d选项只能删除已经参与了合并的分支,对于未有合并的分支是无法删除的。如果想强制删除一个分支,可以使用-D选项
合并分支:$ git merge [name] ----将名称为[name]的分支与当前分支合并
创建远程分支(本地分支push到远程):$ git push origin [name]
删除远程分支:$ git push origin :heads/[name]