git操作
版本控制
集中式(svn)
- 代码存放在单一的服务器上,便于项目的管理
- svn因为每次存的都是差异,需要的硬盘空间会相对小一点 可是回滚的速度会很慢
- 服务器宕机:员工写的代码得不到保障
- 服务器炸了:整个项目的历史记录都会丢失
分布式(git)
- git每次存的都是项目的完整快照 需要的硬盘空间会相对大一点(git团队对代码做了极致的压缩,最终需要的时机空间比svn多不了太多 可是git的回滚速度极快 )
- 优点:完全的分布式
- 缺点:命令多
安装git之后初始化
git —version
初始化配置
cd /Users/shuchenhao/Desktop/workspace
workspace % git init
相关linux命令
ls -l
查看当前目录以及子目录下的文件包括文件夹
find ./
查看当前目录以及子目录的文件不包括文件夹
find ./ -type f
创建文件
touch file.txt
删除文件
rm file.txt
修改文件名称
mv 111/file1.txt file2.txt
给文件写入内容
echo '123'> 111/file2.txt
查看文件内容
cat file2.txt
进入文件写入模式
vim 111/file2.txt
i:进入写模式
esc:进入命令状态
q!强制退出
wq:保存退出
set nu:设置行号
区域
对象
Git对象(代表文件的一次次版本)
echo 'test content' |git hash-object -w --stdin
git cat-file -p d670460b4b4aece5915caf5c68d12f560a9fe3e4
-p 选项可指示该命令自动的判断内容的类型,并为我们显示格式友好的内容
对一个文件进行版本控制
echo 'version 1’>test.txt
git hash-object -w test.txt
问题:
记住文件的每一个版本所对应的SHA-1值并不现实
在git中,文件名并没有背保存,而是仅保存了文件的内容
解决方法:树对象
注意:当前的操作都是在对本地数据库进行操作,不涉及暂存区
树对象(代表项目的一次次版本)
git update-index --add --cacheinfo 100644 4871fd52755be519c29ae719f385bd5f2863627c test.txt
将暂存区做成快照
生成树对象
git write-tree
提交对象
高层命令
git操作最基本的流程
- git hash-object -w 文件名(修改了多少个工作目录中的文件 此命令就要背执行多少次)
- git update-index….
- git write-tree
- git commit-tree
git高层命令
git分支操作(杀手功能)
切换分支
- 在切换分支时 如果有未暂存的修改 (第一次),或者有未提交的暂存(第一次)
- 分支可以切换成功,但这种操作可能会污染其他分支
合并
存储
后悔药
- 如何撤回在工作目录中的修改
- git checkout — filename
- 如何撤回自己的暂存
- git reset HEAD filename
- 如何撤回自己的提交
- 注释写错
- git commit —amend
reset
- 第一部:soft
- git reset -soft HEAD~===(—amend)
- 只动HEAD,带着分支一起移动
- 第二部:git reset —mixed HEAD ~
- 动HEAD(带着分支一起移动)
- 动了暂存区
- 第三部:hard
- git reset —hard HEAD~
- 动HEAD(带着分支一起移动)
- 动了暂存区
- 动了工作目录
checkout
- checkout只动HEAD而且带着分支一起走
- checkout对工作目录是安全的 —hard是强制覆盖工作目录
路径reset
数据恢复
打tag
git tag -d v1.0
实战
shuchenhao@shuchenhaodeMacBook-Air workspace % allbranch
* 901ff1b (HEAD -> master) 2 commit for a.txt v2
* f2edf9e .DS_Store
* b18dcdd 1 commit for a.txt v1
shuchenhao@shuchenhaodeMacBook-Air workspace % git checkout -b #53
Switched to a new branch '#53'
shuchenhao@shuchenhaodeMacBook-Air workspace % allbranch
* 901ff1b (HEAD -> #53, master) 2 commit for a.txt v2
* f2edf9e .DS_Store
* b18dcdd 1 commit for a.txt v1
shuchenhao@shuchenhaodeMacBook-Air workspace % git checkout master
Switched to branch 'master'
shuchenhao@shuchenhaodeMacBook-Air workspace % allbranch
* 901ff1b (HEAD -> master, #53) 2 commit for a.txt v2
* f2edf9e .DS_Store
* b18dcdd 1 commit for a.txt v1
shuchenhao@shuchenhaodeMacBook-Air workspace % git branch -d #53
Deleted branch #53 (was 901ff1b).
shuchenhao@shuchenhaodeMacBook-Air workspace % git checkout -b iss53
Switched to a new branch 'iss53'
shuchenhao@shuchenhaodeMacBook-Air workspace % allbranch
* 901ff1b (HEAD -> iss53, master) 2 commit for a.txt v2
* f2edf9e .DS_Store
* b18dcdd 1 commit for a.txt v1
shuchenhao@shuchenhaodeMacBook-Air workspace % ls -l
total 8
-rw-r--r-- 1 shuchenhao staff 18 5 25 11:59 a.txt
shuchenhao@shuchenhaodeMacBook-Air workspace % echo "iss53 50%">iss53.txt
shuchenhao@shuchenhaodeMacBook-Air workspace % ls -l
total 16
-rw-r--r-- 1 shuchenhao staff 18 5 25 11:59 a.txt
-rw-r--r-- 1 shuchenhao staff 10 5 25 12:04 iss53.txt
shuchenhao@shuchenhaodeMacBook-Air workspace % git add ./
shuchenhao@shuchenhaodeMacBook-Air workspace % git commit -m "1 commit for iss53 v1"
[iss53 3af9d1f] 1 commit for iss53 v1
1 file changed, 1 insertion(+)
create mode 100644 iss53.txt
shuchenhao@shuchenhaodeMacBook-Air workspace % git status
On branch iss53
nothing to commit, working tree clean
shuchenhao@shuchenhaodeMacBook-Air workspace % allbranch
* 3af9d1f (HEAD -> iss53) 1 commit for iss53 v1
* 901ff1b (master) 2 commit for a.txt v2
* f2edf9e .DS_Store
* b18dcdd 1 commit for a.txt v1
shuchenhao@shuchenhaodeMacBook-Air workspace % git checkout master
Switched to branch 'master'
shuchenhao@shuchenhaodeMacBook-Air workspace % allbranch
* 3af9d1f (iss53) 1 commit for iss53 v1
* 901ff1b (HEAD -> master) 2 commit for a.txt v2
* f2edf9e .DS_Store
* b18dcdd 1 commit for a.txt v1
shuchenhao@shuchenhaodeMacBook-Air workspace % git checkout -b hotbug
Switched to a new branch 'hotbug'
shuchenhao@shuchenhaodeMacBook-Air workspace % allbranch
* 3af9d1f (iss53) 1 commit for iss53 v1
* 901ff1b (HEAD -> hotbug, master) 2 commit for a.txt v2
* f2edf9e .DS_Store
* b18dcdd 1 commit for a.txt v1
shuchenhao@shuchenhaodeMacBook-Air workspace % vim a.txt
shuchenhao@shuchenhaodeMacBook-Air workspace % git commit -a -m "3 commit for a.txt v3 to fix hotbug"
[hotbug a2f6693] 3 commit for a.txt v3 to fix hotbug
1 file changed, 1 insertion(+)
shuchenhao@shuchenhaodeMacBook-Air workspace % git status
On branch hotbug
nothing to commit, working tree clean
shuchenhao@shuchenhaodeMacBook-Air workspace % allbranch
* a2f6693 (HEAD -> hotbug) 3 commit for a.txt v3 to fix hotbug
| * 3af9d1f (iss53) 1 commit for iss53 v1
|/
* 901ff1b (master) 2 commit for a.txt v2
* f2edf9e .DS_Store
* b18dcdd 1 commit for a.txt v1
shuchenhao@shuchenhaodeMacBook-Air workspace % git checkout master
Switched to branch 'master'
shuchenhao@shuchenhaodeMacBook-Air workspace % allbranch
* a2f6693 (hotbug) 3 commit for a.txt v3 to fix hotbug
| * 3af9d1f (iss53) 1 commit for iss53 v1
|/
* 901ff1b (HEAD -> master) 2 commit for a.txt v2
* f2edf9e .DS_Store
* b18dcdd 1 commit for a.txt v1
shuchenhao@shuchenhaodeMacBook-Air workspace % git merge hotbug
Updating 901ff1b..a2f6693
Fast-forward
a.txt | 1 +
1 file changed, 1 insertion(+)
shuchenhao@shuchenhaodeMacBook-Air workspace % allbranch
* a2f6693 (HEAD -> master, hotbug) 3 commit for a.txt v3 to fix hotbug
| * 3af9d1f (iss53) 1 commit for iss53 v1
|/
* 901ff1b 2 commit for a.txt v2
* f2edf9e .DS_Store
* b18dcdd 1 commit for a.txt v1
shuchenhao@shuchenhaodeMacBook-Air workspace % git branch -d hotbug
Deleted branch hotbug (was a2f6693).
shuchenhao@shuchenhaodeMacBook-Air workspace % allbranch
* a2f6693 (HEAD -> master) 3 commit for a.txt v3 to fix hotbug
| * 3af9d1f (iss53) 1 commit for iss53 v1
|/
* 901ff1b 2 commit for a.txt v2
* f2edf9e .DS_Store
* b18dcdd 1 commit for a.txt v1
shuchenhao@shuchenhaodeMacBook-Air workspace % git status
On branch master
nothing to commit, working tree clean
shuchenhao@shuchenhaodeMacBook-Air workspace % git checkout iss53
Switched to branch 'iss53'
shuchenhao@shuchenhaodeMacBook-Air workspace % git status
On branch iss53
nothing to commit, working tree clean
shuchenhao@shuchenhaodeMacBook-Air workspace % vim iss53.txt
shuchenhao@shuchenhaodeMacBook-Air workspace % allbranch
* a2f6693 (master) 3 commit for a.txt v3 to fix hotbug
| * 3af9d1f (HEAD -> iss53) 1 commit for iss53 v1
|/
* 901ff1b 2 commit for a.txt v2
* f2edf9e .DS_Store
* b18dcdd 1 commit for a.txt v1
shuchenhao@shuchenhaodeMacBook-Air workspace % vim iss53.txt
shuchenhao@shuchenhaodeMacBook-Air workspace % git status
On branch iss53
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: iss53.txt
no changes added to commit (use "git add" and/or "git commit -a")
shuchenhao@shuchenhaodeMacBook-Air workspace % git commit -a -m "2 commit for iss53.txt v2 100%"
[iss53 39b1928] 2 commit for iss53.txt v2 100%
1 file changed, 1 insertion(+)
shuchenhao@shuchenhaodeMacBook-Air workspace % allbranch
* 39b1928 (HEAD -> iss53) 2 commit for iss53.txt v2 100%
* 3af9d1f 1 commit for iss53 v1
| * a2f6693 (master) 3 commit for a.txt v3 to fix hotbug
|/
* 901ff1b 2 commit for a.txt v2
* f2edf9e .DS_Store
* b18dcdd 1 commit for a.txt v1
shuchenhao@shuchenhaodeMacBook-Air workspace % git status
On branch iss53
nothing to commit, working tree clean
shuchenhao@shuchenhaodeMacBook-Air workspace % cat a.txt
a.txt v1
a.txt v2
shuchenhao@shuchenhaodeMacBook-Air workspace % vim a.txt
shuchenhao@shuchenhaodeMacBook-Air workspace % git commit -a -m "3 commit for iss53 to patch a.txt=v3 to real 100%"
[iss53 3930d75] 3 commit for iss53 to patch a.txt=v3 to real 100%
1 file changed, 1 insertion(+)
shuchenhao@shuchenhaodeMacBook-Air workspace % allbranch
* 3930d75 (HEAD -> iss53) 3 commit for iss53 to patch a.txt=v3 to real 100%
* 39b1928 2 commit for iss53.txt v2 100%
* 3af9d1f 1 commit for iss53 v1
| * a2f6693 (master) 3 commit for a.txt v3 to fix hotbug
|/
* 901ff1b 2 commit for a.txt v2
* f2edf9e .DS_Store
* b18dcdd 1 commit for a.txt v1
shuchenhao@shuchenhaodeMacBook-Air workspace % git checkout master
Switched to branch 'master'
shuchenhao@shuchenhaodeMacBook-Air workspace % allbranch
* 3930d75 (iss53) 3 commit for iss53 to patch a.txt=v3 to real 100%
* 39b1928 2 commit for iss53.txt v2 100%
* 3af9d1f 1 commit for iss53 v1
| * a2f6693 (HEAD -> master) 3 commit for a.txt v3 to fix hotbug
|/
* 901ff1b 2 commit for a.txt v2
* f2edf9e .DS_Store
* b18dcdd 1 commit for a.txt v1
shuchenhao@shuchenhaodeMacBook-Air workspace % git merge iss53
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
Automatic merge failed; fix conflicts and then commit the result.
shuchenhao@shuchenhaodeMacBook-Air workspace % cat a.txt
a.txt v1
a.txt v2
<<<<<<< HEAD
a.txt v3
=======
a.txt v3 for iss53
>>>>>>> iss53
shuchenhao@shuchenhaodeMacBook-Air workspace % vim a.txt
shuchenhao@shuchenhaodeMacBook-Air workspace % cat a.txt
a.txt v1
a.txt v2
a.txt v3 for hotbug
a.txt v3 for iss53
shuchenhao@shuchenhaodeMacBook-Air workspace % git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Changes to be committed:
new file: iss53.txt
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: a.txt
shuchenhao@shuchenhaodeMacBook-Air workspace % git add./
git: 'add./' is not a git command. See 'git --help'.
shuchenhao@shuchenhaodeMacBook-Air workspace % git add ./
shuchenhao@shuchenhaodeMacBook-Air workspace % git status
On branch master
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
Changes to be committed:
modified: a.txt
new file: iss53.txt
shuchenhao@shuchenhaodeMacBook-Air workspace % git commit -m "4 commit for fix cliect"
[master 3712fd1] 4 commit for fix cliect
shuchenhao@shuchenhaodeMacBook-Air workspace % allbranch
* 3712fd1 (HEAD -> master) 4 commit for fix cliect
|\
| * 3930d75 (iss53) 3 commit for iss53 to patch a.txt=v3 to real 100%
| * 39b1928 2 commit for iss53.txt v2 100%
| * 3af9d1f 1 commit for iss53 v1
* | a2f6693 3 commit for a.txt v3 to fix hotbug
|/
* 901ff1b 2 commit for a.txt v2
* f2edf9e .DS_Store
* b18dcdd 1 commit for a.txt v1
shuchenhao@shuchenhaodeMacBook-Air workspace % git branch -d iss53
Deleted branch iss53 (was 3930d75).
shuchenhao@shuchenhaodeMacBook-Air workspace % allbranch
* 3712fd1 (HEAD -> master) 4 commit for fix cliect
|\
| * 3930d75 3 commit for iss53 to patch a.txt=v3 to real 100%
| * 39b1928 2 commit for iss53.txt v2 100%
| * 3af9d1f 1 commit for iss53 v1
* | a2f6693 3 commit for a.txt v3 to fix hotbug
|/
* 901ff1b 2 commit for a.txt v2
* f2edf9e .DS_Store
* b18dcdd 1 commit for a.txt v1
shuchenhao@shuchenhaodeMacBook-Air workspace %
一个本地分支怎么去跟踪一个远程跟踪分支
团队协作
- 项目经理初始化远程仓库
- 一定要初始化一个空的仓库,在github上操作
- 项目经理创建本地仓库
- git init
- 将源码复制进来
- 修改用户名和邮箱
- git add
- git commit
- 项目经理推送本地仓库到远程仓库
- git push 别名 分支 (输入用户名和密码,推完之后生成一个远程跟踪分支)
- 项目经理邀请成员,成员接收邀请
- 在github上操作
- 成员克隆远程仓库
- git clone 仓库地址(在本地生成.git文件 默认为远程仓库配置了别名origin)
- 默认主分支有对应的远程跟踪分支
- 只有在克隆时 本地分支master和远程跟踪分支 别名/master 是有同步关系的
- 成员做出贡献
- 修改源码文件
- git add
- git commit
- git push 别名 分支名(输入用户名和密码)
- 项目经理更新修改
- git fetch 别名(将修改同步到远程跟踪分支上)
- git merge 远程跟踪分支
冲突
远程协作
- 让第三方人员使用仓库