目录
贮藏stash
1、指令
#将暂存区和工作区清空,并将内容贮藏起来
git stash
2、演示
#查看下当前Git的状态,工作区有未保存的内容
localhost:gitLearning LC$ git status
On branch temp
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
#使用stash保存当前内容,并清空暂存区和工作区
localhost:gitLearning LC$ git stash
Saved working directory and index state WIP on temp: fe9b9f9 Add index.html
#查看下当前的Git状态
localhost:gitLearning LC$ git status
On branch temp
nothing to commit, working tree clean
#查看下stash列表
localhost:gitLearning LC$ git stash list
stash@{0}: WIP on temp: fe9b9f9 Add index.html
#应用贮藏,并保留贮藏
localhost:gitLearning LC$ git stash apply
On branch temp
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
#应用最顶部的贮藏,并清除该贮藏
localhost:gitLearning LC$ git stash pop
On branch temp
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (8337ee26167e17f84035c1c018b9dc39176d6de4)
3、
使用该功能,可以让我们在工作未完成,突然遇到其它问题需要临时解决时,保存我们当前的工作。
忽略.gitignore
1、说明
如果我们不想将工程中的某些文件纳入到版本管理当中,我们可以借助.gitignore文件来实现
比如github在我们创建一个仓库时,可以指定.gitignore文件,如下图
2、演示
#创建一个doc文件夹
localhost:gitLearning LC$ mkdir doc
#创建一个readhim文件
localhost:gitLearning LC$ echo "say hello" > doc/readhim
#查看当前Git状态
localhost:gitLearning LC$ git status
On branch temp
Untracked files:
(use "git add <file>..." to include in what will be committed)
doc/
nothing added to commit but untracked files present (use "git add" to track)
#我们发现doc文件夹已被管理但未被追踪
#下面我们创建一个.gitignore文件
localhost:gitLearning LC$ echo "doc" > .gitignore
localhost:gitLearning LC$ git status
On branch temp
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git add" to track)
#整个doc文件夹已经被Git忽略,不在被Git管理
#这里需要说明
#doc:代表doc文件(文件夹在Linux系统下也是一个文件)
#doc/:代表doc文件夹下面所有的文件
说明:
.gitignore文件应该放在Git仓库的根目录下与.git文件夹平级。否则不起作用
加入.gitignore的文件或文件夹应在未追踪时加入,如果已经在暂存区是不受.gitignore文件影响的
如果我们想对已经commit的文件进行.gitignore文件管理,我们首先应该
1、备份,然后删除该文件
2、commit提交
3、重新添加该文件,添加到.gitignore文件
Git的备份
一、常用的传输协议
常⽤协议 | 语法格式 | 说明 |
本地协议(1) | /path/to/repo.git | 哑协议 |
本地协议(2) | file:///path/to/repo.git | 智能协议 |
http/https协议 | http://git-server.com:port/path/to/repo.git https://git-server.com:port/path/to/repo.git |
平时接触到的 都是智能协议 |
ssh协议 | user@git-server.com:path/to/repo.git | ⼯作中最常⽤ 的智能协议 |
二、哑协议与智能协议
直观区别:哑协议传输进度不可⻅;智能协议传输可⻅。
传输速度:智能协议⽐哑协议传输速度快。
#本地 - 哑协议演示
bogon:remoteGits LC$ git clone /Users/LC/Desktop/gitDemo/gitLearning/.git ya.git
Cloning into bare repository 'ya.git'...
done.
#本地 - 智能协议
bogon:remoteGits LC$ git clone file:///Users/LC/Desktop/gitDemo/gitLearning/.git zhineng.git
Cloning into bare repository 'zhineng.git'...
remote: Enumerating objects: 26, done.
remote: Counting objects: 100% (26/26), done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 26 (delta 4), reused 0 (delta 0)
Receiving objects: 100% (26/26), 21.99 KiB | 11.00 MiB/s, done.
Resolving deltas: 100% (4/4), done.
bogon:remoteGits LC$
三、添加远程仓库
1、指令
git remote add
2、指令演示
#添加远程仓库
bogon:gitLearning LC$ git remote add zhineng file:///Users/LC/Desktop/remoteGits/zhineng.git
bogon:gitLearning LC$ git remote -v
zhineng file:///Users/LC/Desktop/remoteGits/zhineng.git (fetch)
zhineng file:///Users/LC/Desktop/remoteGits/zhineng.git (push)
#创建并检出LC分支,查看下当前仓库的分支情况
bogon:gitLearning LC$ git branch -av
* LC 2e7d469 Readd readme file
master d984cc2 Add first commond
temp 2e7d469 Readd readme file
bogon:gitLearning LC$
#执行一次push操作
bogon:gitLearning LC$ git push zhineng
fatal: The current branch LC has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream zhineng LC
bogon:gitLearning LC$ git push --set-upstream zhineng LC
Total 0 (delta 0), reused 0 (delta 0)
To file:///Users/LC/Desktop/remoteGits/zhineng.git
* [new branch] LC -> LC
Branch 'LC' set up to track remote branch 'LC' from 'zhineng'.
bogon:gitLearning LC$
#再次查看下远程仓库的分支情况
bogon:zhineng.git LC$ git branch
LC
* master
bogon:zhineng.git LC$
四、查看远程仓库
1、指令
git remote -v
2、指令演示
bogon:gitLearning LC$ git remote -v
zhineng file:///Users/LC/Desktop/remoteGits/zhineng.git (fetch)
zhineng file:///Users/LC/Desktop/remoteGits/zhineng.git (push)
五、移除远程仓库
1、指令
git remote rm
2、指令演示
#移除远程仓库
bogon:gitLearning LC$ git remote rm zhineng
六、备份特点
行者常至,为者常成!